1 # Copyright (C) 2012 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, ConflictConfigError
23 # ____________________________________________________________
24 # automatic Option object
25 #def special_owner_factory(name, owner, value,
26 # callback, callback_params=None, config=None):
27 # # in case of an 'auto' and a 'fill' without a value,
28 # # we have to carry out a calculation
29 # return calc_factory(name, callback, callback_params, config)
30 def carry_out_calculation(name, option, config):
31 # FIXME we have to know the exact status of the config
34 callback=option.getcallback()
35 callback_params=option.getcallback_params()
36 if callback_params is None:
42 for key, values in callback_params.items():
44 if type(value) == tuple:
45 path, check_disabled = value
47 opt_value = config._getattr(path, permissive=True)
48 opt = config.unwrap_from_path(path)
49 except PropertiesOptionError, err:
52 raise PropertiesOptionError(err, err.proptype)
53 is_multi = opt.is_multi()
56 len_value = len(opt_value)
57 if len_multi != 0 and len_multi != len_value:
58 raise ConflictConfigError('unable to carry out a calculation, '
59 'option values with multi types must have same length for: '
63 tcparams.setdefault(key, []).append((opt_value, is_multi))
65 tcparams.setdefault(key, []).append((value, False))
69 for incr in range(len_multi):
72 for key, couples in tcparams.items():
73 for couple in couples:
74 value, ismulti = couple
75 if ismulti and value != None:
77 params.append(value[incr])
80 tcp[key] = value[incr]
88 calc = calculate(name, callback, params, tcp)
89 if isinstance(calc, list):
98 for key, couples in tcparams.items():
99 for couple in couples:
105 return calculate(name, callback, params, tcp)
107 def calculate(name, callback, params, tcparams):
109 # XXX not only creole...
110 from creole import eosfunc
111 return getattr(eosfunc, callback)(*params, **tcparams)
112 except AttributeError, err:
114 traceback.print_exc()
115 raise ConflictConfigError("callback: {0} return error {1} for "
116 "option: {2}".format(callback, str(err), name))