polymorphism in autolib functions

This commit is contained in:
gwen 2012-07-06 09:57:35 +02:00
parent ca277657e5
commit 2569cc5e59
1 changed files with 34 additions and 10 deletions

View File

@ -19,7 +19,8 @@
# the whole pypy projet is under MIT licence
# ____________________________________________________________
"enables us to carry out a calculation and return an option's value"
from tiramisu.error import DisabledOptionError, SpecialOwnersError
from tiramisu.error import DisabledOptionError, SpecialOwnersError, ConfigError
from creole import eosfunc
# ____________________________________________________________
# automatic Option object
special_owners = ['auto', 'fill']
@ -30,8 +31,6 @@ def special_owner_factory(name, owner, value,
# we have to carry out a calculation
return calc_factory(name, callback, callback_params, config)
#g = globals()
def calc_factory(name, callback, callback_params, config):
# FIXME we have to know the exact status of the config
# not to disrupt it
@ -39,25 +38,50 @@ def calc_factory(name, callback, callback_params, config):
if callback_params is None:
callback_params = {}
tcparams = {}
one_is_multi = False
len_multi = 0
for key, value in callback_params.items():
if type(value) == tuple:
path, check_disabled = value
try:
opt_value = getattr(config, path)
opt = config.unwrap_from_path(path)
except DisabledOptionError, e:
if chek_disabled:
continue
raise DisabledOptionError(e)
tcparams[key] = opt_value
is_multi = opt.is_multi()
if is_multi:
len_value = len(opt_value)
if len_multi != 0 and len_multi != len_value:
raise ConfigError('unable to carry out a calculation, '
'option values with multi types must have same length for: '
+ name)
len_multi = len_value
one_is_multi = True
tcparams[key] = (opt_value, is_multi)
else:
tcparams[key] = value
tcparams[key] = (value, False)
if one_is_multi:
ret = []
for incr in range(len_multi):
tcp = {}
for key, couple in tcparams.items():
value, ismulti = couple
if ismulti:
tcp[key] = value[incr]
else:
tcp[key] = value
ret.append(calculate(callback, tcp))
return ret
else:
return calculate(callback, tcparams)
def calculate(callback, tcparams):
try:
#return getattr(autolib, callback)(name, config)
#return g[callback](name, config, **callback_params)
from creole import eosfunc
return getattr(eosfunc, callback)(**tcparams)
except AttributeError:
import traceback
traceback.print_exc()
raise SpecialOwnersError("callback: {0} not found for "
"option: {1}".format(callback, name))