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,
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)
31 def carry_out_calculation(name, callback, callback_params, config):
32 # FIXME we have to know the exact status of the config
35 if callback_params is None:
40 for key, value in callback_params.items():
41 if type(value) == tuple:
42 path, check_disabled = value
44 opt_value = getattr(config, path)
45 opt = config.unwrap_from_path(path)
46 except PropertiesOptionError, e:
49 raise PropertiesOptionError(e)
50 is_multi = opt.is_multi()
53 len_value = len(opt_value)
54 if len_multi != 0 and len_multi != len_value:
55 raise ConflictConfigError('unable to carry out a calculation, '
56 'option values with multi types must have same length for: '
60 tcparams[key] = (opt_value, is_multi)
62 tcparams[key] = (value, False)
66 for incr in range(len_multi):
68 for key, couple in tcparams.items():
69 value, ismulti = couple
70 if ismulti and value != None:
72 params.append(value[incr])
74 tcp[key] = value[incr]
80 ret.append(calculate(name, callback, tcp))
85 for key, couple in tcparams.items():
87 params.append(couple[0])
90 return calculate(name, callback, params, tcp)
92 def calculate(name, callback, params, tcparams):
94 # XXX not only creole...
95 from creole import eosfunc
96 return getattr(eosfunc, callback)(*params, **tcparams)
97 except NoValueReturned, err:
99 except AttributeError, err:
101 traceback.print_exc()
102 raise ConflictConfigError("callback: {0} return error {1} for "
103 "option: {2}".format(callback, str(err), name))