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
147 for key, callbacks in callback_params.items():
148 for callbk in callbacks:
149 if isinstance(callbk, tuple):
150 # callbk is something link (opt, True|False)
151 opt, force_permissive = callbk
152 path = config.cfgimpl_get_description().impl_get_path_by_opt(
156 value = config._getattr(path, force_permissive=True)
157 except PropertiesOptionError as err:
160 raise ConfigError(_('unable to carry out a calculation, '
161 'option {0} has properties: {1} for: '
162 '{2}').format(opt._name,
167 if opt.impl_is_multi():
168 #opt is master, search if option is a slave
169 if opt.impl_get_multitype() == multitypes.master:
170 if option in opt.impl_get_master_slaves():
172 #opt is slave, search if option is an other slaves
173 elif opt.impl_get_multitype() == multitypes.slave:
174 if option in opt.impl_get_master_slaves().impl_get_master_slaves():
177 len_multi = len(value)
179 tcparams.setdefault(key, []).append((value, is_multi))
181 # callbk is a value and not a multi
182 tcparams.setdefault(key, []).append((callbk, False))
184 # if one value is a multi, launch several time calculate
185 # if index is set, return a value
186 # if no index, return a list
192 range_ = range(len_multi)
196 for key, couples in tcparams.items():
197 for couple in couples:
198 value, ismulti = couple
207 calc = calculate(callback, args, kwargs)
215 # return a single value
218 for key, couples in tcparams.items():
219 for couple in couples:
220 # couple[1] (ismulti) is always False
222 args.append(couple[0])
224 kwargs[key] = couple[0]
225 ret = calculate(callback, args, kwargs)
226 if callback_params != {}:
227 if isinstance(ret, list) and max_len:
229 if len(ret) < max_len:
230 ret = ret + [None] * (max_len - len(ret))
231 if isinstance(ret, list) and index:
232 if len(ret) < index + 1:
239 def calculate(callback, args, kwargs):
240 """wrapper that launches the 'callback'
242 :param callback: callback function
243 :param args: in the callback's arity, the unnamed parameters
244 :param kwargs: in the callback's arity, the named parameters
247 return callback(*args, **kwargs)