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 # ____________________________________________________________
27 def carry_out_calculation(name, config, callback, callback_params,
28 index=None, max_len=None):
29 """a function that carries out a calculation for an option's value
31 :param name: the option name (`opt._name`)
32 :param config: the context config in order to have
33 the whole options available
34 :param callback: the name of the callback function
36 :param callback_params: the callback's parameters
37 (only keyword parameters are allowed)
38 :type callback_params: dict
39 :param index: if an option is multi, only calculates the nth value
41 :param max_len: max length for a multi
44 * if no callback_params:
47 * if callback_params={'': ('yes',)}
50 * if callback_params={'value': ('yes',)}
51 => calculate(value='yes')
53 * if callback_params={'': ('yes', 'no')}
54 => calculate('yes', 'no')
56 * if callback_params={'value': ('yes', 'no')}
59 * if callback_params={'': ((opt1, False),)}
71 * if callback_params={'value': ((opt1, False),)}
75 => calculate(value=11)
83 * if callback_params={'': ((opt1, False), (opt2, False))}
85 - a multi option with a simple option
92 - a multi option with an other multi option but with same length
95 callback_params={'': ((opt1, False), (opt2, False))}
100 - a multi option with an other multi option but with different length
103 callback_params={'': ((opt1, False), (opt2, False))}
106 * if callback_params={'value': ((opt1, False), (opt2, False))}
109 If index is not None, return a value, otherwise return:
111 * a list if one parameters have multi option
114 If calculate return list, this list is extend to return value.
120 for key, callbacks in callback_params.items():
121 for callbk in callbacks:
122 if isinstance(callbk, tuple):
123 option, force_permissive = callbk
126 path = config.cfgimpl_get_description().impl_get_path_by_opt(option)
127 value = config._getattr(path, force_permissive=True)
128 except PropertiesOptionError as err:
131 raise ConfigError(_('unable to carry out a calculation, '
132 'option {0} has properties: {1} for: '
133 '{2}').format(option._name, err.proptype,
135 is_multi = option.impl_is_multi()
137 if value is not None:
138 len_value = len(value)
139 if len_multi != 0 and len_multi != len_value:
140 raise ConfigError(_('unable to carry out a '
141 'calculation, option value with'
142 ' multi types must have same '
143 'length for: {0}').format(name))
144 len_multi = len_value
146 tcparams.setdefault(key, []).append((value, is_multi))
148 tcparams.setdefault(key, []).append((callbk, False))
153 if index < len_multi:
159 if max_len and max_len < len_multi:
160 range_ = range(max_len)
162 range_ = range(len_multi)
166 for key, couples in tcparams.items():
167 for couple in couples:
168 value, ismulti = couple
169 if ismulti and value is not None:
171 params.append(value[incr])
173 tcp[key] = value[incr]
176 calc = calculate(name, callback, params, tcp)
180 if isinstance(calc, list):
188 for key, couples in tcparams.items():
189 for couple in couples:
195 return calculate(name, callback, params, tcp)
198 def calculate(name, callback, params, tcparams):
199 """wrapper that launches the 'callback'
201 :param callback: callback name
202 :param params: in the callback's arity, the unnamed parameters
203 :param tcparams: in the callback's arity, the named parameters
206 return callback(*params, **tcparams)