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 # ____________________________________________________________
25 # automatic Option object
26 #def special_owner_factory(name, owner, value,
27 # callback, callback_params=None, config=None):
28 # # in case of an 'auto' and a 'fill' without a value,
29 # # we have to carry out a calculation
30 # return calc_factory(name, callback, callback_params, config)
33 def carry_out_calculation(name, config, callback, callback_params, index=None):
34 "a function that carries out a calculation for an option's value"
35 #callback, callback_params = option.getcallback()
36 #if callback_params is None:
37 # callback_params = {}
42 for key, values in callback_params.items():
44 if type(value) == tuple:
45 path, check_disabled = value
49 raise ConfigError(_('no config specified but needed'))
51 opt_value = config._getattr(path, force_permissive=True)
52 opt = config.unwrap_from_path(path)
53 except PropertiesOptionError, err:
56 raise ConfigError(_('unable to carry out a calculation, '
57 'option {0} has properties: {1} for: '
58 '{2}').format(opt._name, err.proptype,
60 is_multi = opt.impl_is_multi()
62 if opt_value is not None:
63 len_value = len(opt_value)
64 if len_multi != 0 and len_multi != len_value:
65 raise ConfigError(_('unable to carry out a '
66 'calculation, option values with'
67 ' multi types must have same '
68 'length for: {0}').format(name))
71 tcparams.setdefault(key, []).append((opt_value, is_multi))
73 tcparams.setdefault(key, []).append((value, False))
80 range_ = range(len_multi)
84 for key, couples in tcparams.items():
85 for couple in couples:
86 value, ismulti = couple
87 if ismulti and value is not None:
89 params.append(value[incr])
92 tcp[key] = value[incr]
100 calc = calculate(name, callback, params, tcp)
104 if isinstance(calc, list):
113 for key, couples in tcparams.items():
114 for couple in couples:
120 return calculate(name, callback, params, tcp)
123 def calculate(name, callback, params, tcparams):
124 """wrapper that launches the 'callback'
126 :param callback: callback name
127 :param params: in the callback's arity, the unnamed parameters
128 :param tcparams: in the callback's arity, the named parameters
131 return callback(*params, **tcparams)