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.setting import multitypes
24 from tiramisu.i18n import _
25 # ____________________________________________________________
28 def carry_out_calculation(option, config, callback, callback_params,
29 index=None, max_len=None):
30 """a function that carries out a calculation for an option's value
32 :param name: the option name (`opt.impl_getname()`)
33 :param name: the option
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
43 :param max_len: max length for a multi
46 The callback_params is a dict. Key is used to build args (if key is '')
47 and kwargs (otherwise). Values are tuple of:
49 - tuple with option and boolean's force_permissive (True when don't raise
50 if PropertiesOptionError)
51 Values could have multiple values only when key is ''.
53 * if no callback_params:
54 => calculate(<function func at 0x2092320>, [], {})
56 * if callback_params={'': ('yes',)}
57 => calculate(<function func at 0x2092320>, ['yes'], {})
59 * if callback_params={'value': ('yes',)}
60 => calculate(<function func at 0x165b320>, [], {'value': 'yes'})
62 * if callback_params={'': ('yes', 'no')}
63 => calculate('yes', 'no')
65 * if callback_params={'value': ('yes', 'no')}
68 * if callback_params={'': (['yes', 'no'],)}
69 => calculate(<function func at 0x176b320>, ['yes', 'no'], {})
71 * if callback_params={'value': ('yes', 'no')}
72 => raises ValueError()
74 * if callback_params={'': ((opt1, False),)}
78 => calculate(<function func at 0x1cea320>, [11], {})
80 - a multi option and not master/slave:
82 => calculate(<function func at 0x223c320>, [[1, 2, 3]], {})
84 - option is master or slave of opt1:
86 => calculate(<function func at 0x223c320>, [1], {})
87 => calculate(<function func at 0x223c320>, [2], {})
88 => calculate(<function func at 0x223c320>, [3], {})
90 - opt is a master or slave but not related to option:
92 => calculate(<function func at 0x11b0320>, [[1, 2, 3]], {})
94 * if callback_params={'value': ((opt1, False),)}
98 => calculate(<function func at 0x17ff320>, [], {'value': 11})
102 => calculate(<function func at 0x1262320>, [], {'value': [1, 2, 3]})
104 * if callback_params={'': ((opt1, False), (opt2, False))}
109 => calculate(<function func at 0x217a320>, [11, 12], {})
111 - a multi option with a simple option
114 => calculate(<function func at 0x2153320>, [[1, 2, 3], 12], {})
116 - a multi option with an other multi option but with same length
119 => calculate(<function func at 0x1981320>, [[1, 2, 3], [11, 12, 13]], {})
121 - a multi option with an other multi option but with different length
124 => calculate(<function func at 0x2384320>, [[1, 2, 3], [11, 12]], {})
126 - a multi option without value with a simple option
129 => calculate(<function func at 0xb65320>, [[], 12], {})
131 * if callback_params={'value': ((opt1, False), (opt2, False))}
132 => raises ValueError()
134 If index is not None, return a value, otherwise return:
136 * a list if one parameters have multi option
139 If calculate return list, this list is extend to return value.
142 # if callback_params has a callback, launch several time calculate()
144 # multi's option should have same value for all option
146 for callbacks in callback_params:
148 for callbk in callbacks.params:
149 if callbk.option is not None:
150 # callbk is something link (opt, True|False)
151 opt = callbk.get_option(config)
152 force_permissive = callbk.force_permissive
153 path = config.cfgimpl_get_description().impl_get_path_by_opt(
157 value = config._getattr(path, force_permissive=True)
158 except PropertiesOptionError as err:
161 raise ConfigError(_('unable to carry out a calculation, '
162 'option {0} has properties: {1} for: '
163 '{2}').format(option.impl_getname(),
168 if opt.impl_is_multi():
169 #opt is master, search if option is a slave
170 if opt.impl_get_multitype() == multitypes.master:
171 if option in opt.impl_get_master_slaves():
173 #opt is slave, search if option is an other slaves
174 elif opt.impl_get_multitype() == multitypes.slave:
175 if option in opt.impl_get_master_slaves().impl_get_master_slaves():
178 len_multi = len(value)
180 tcparams.setdefault(key, []).append((value, is_multi))
182 # callbk is a value and not a multi
183 tcparams.setdefault(key, []).append((callbk.value, False))
185 # if one value is a multi, launch several time calculate
186 # if index is set, return a value
187 # if no index, return a list
193 range_ = range(len_multi)
197 for key, couples in tcparams.items():
198 for couple in couples:
199 value, ismulti = couple
208 calc = calculate(callback, args, kwargs)
216 # return a single value
219 for key, couples in tcparams.items():
220 for couple in couples:
221 # couple[1] (ismulti) is always False
223 args.append(couple[0])
225 kwargs[key] = couple[0]
226 ret = calculate(callback, args, kwargs)
227 if callback_params != {}:
228 if isinstance(ret, list) and max_len:
230 if len(ret) < max_len:
231 ret = ret + [None] * (max_len - len(ret))
232 if isinstance(ret, list) and index:
233 if len(ret) < index + 1:
240 def calculate(callback, args, kwargs):
241 """wrapper that launches the 'callback'
243 :param callback: callback function
244 :param args: in the callback's arity, the unnamed parameters
245 :param kwargs: in the callback's arity, the named parameters
248 return callback(*args, **kwargs)