1 # Copyright (C) 2012-2013 Team tiramisu (see AUTHORS for all contributors)
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 # The original `Config` design model is unproudly borrowed from
18 # the rough gus of pypy: pypy: http://codespeak.net/svn/pypy/dist/pypy/config/
19 # the whole pypy projet is under MIT licence
20 # ____________________________________________________________
21 "enables us to carry out a calculation and return an option's value"
22 from tiramisu.error import PropertiesOptionError, ConfigError
23 from tiramisu.i18n import _
24 # ____________________________________________________________
26 def carry_out_calculation(name,
31 """a function that carries out a calculation for an option's value
33 :param name: the option name (`opt._name`)
34 :param config: the context config in order to have
35 the whole options available
36 :param callback: the name of the callback function
38 :param callback_params: the callback's parameters
39 (only keyword parameters are allowed)
40 :type callback_params: dict
41 :param index: if an option is multi, only calculates the nth value
44 #callback, callback_params = option.getcallback()
45 #if callback_params is None:
46 # callback_params = {}
51 for key, values in callback_params.items():
53 if type(value) == tuple:
54 path, check_disabled = value
58 raise ConfigError(_('no config specified but needed'))
60 opt_value = config._getattr(path, force_permissive=True)
61 opt = config.unwrap_from_path(path, force_permissive=True)
62 except PropertiesOptionError, err:
65 raise ConfigError(_('unable to carry out a calculation, '
66 'option {0} has properties: {1} for: '
67 '{2}').format(path, err.proptype,
69 is_multi = opt.impl_is_multi()
71 if opt_value is not None:
72 len_value = len(opt_value)
73 if len_multi != 0 and len_multi != len_value:
74 raise ConfigError(_('unable to carry out a '
75 'calculation, option value with'
76 ' multi types must have same '
77 'length for: {0}').format(name))
80 tcparams.setdefault(key, []).append((opt_value, is_multi))
82 tcparams.setdefault(key, []).append((value, False))
89 range_ = range(len_multi)
93 for key, couples in tcparams.items():
94 for couple in couples:
95 value, ismulti = couple
96 if ismulti and value is not None:
98 params.append(value[incr])
100 if len(value) > incr:
101 tcp[key] = value[incr]
109 calc = calculate(name, callback, params, tcp)
113 if isinstance(calc, list):
122 for key, couples in tcparams.items():
123 for couple in couples:
129 return calculate(name, callback, params, tcp)
132 def calculate(name, callback, params, tcparams):
133 # FIXME we don't need the option's name down there.
134 """wrapper that launches the 'callback'
136 :param callback: callback name
137 :param params: in the callback's arity, the unnamed parameters
138 :param tcparams: in the callback's arity, the named parameters
141 return callback(*params, **tcparams)