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 # the whole pypy projet is under MIT licence
# ____________________________________________________________ # ____________________________________________________________
"enables us to carry out a calculation and return an option's value" "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 # automatic Option object
special_owners = ['auto', 'fill'] special_owners = ['auto', 'fill']
@ -30,8 +31,6 @@ def special_owner_factory(name, owner, value,
# we have to carry out a calculation # we have to carry out a calculation
return calc_factory(name, callback, callback_params, config) return calc_factory(name, callback, callback_params, config)
#g = globals()
def calc_factory(name, callback, callback_params, config): def calc_factory(name, callback, callback_params, config):
# FIXME we have to know the exact status of the config # FIXME we have to know the exact status of the config
# not to disrupt it # not to disrupt it
@ -39,25 +38,50 @@ def calc_factory(name, callback, callback_params, config):
if callback_params is None: if callback_params is None:
callback_params = {} callback_params = {}
tcparams = {} tcparams = {}
one_is_multi = False
len_multi = 0
for key, value in callback_params.items(): for key, value in callback_params.items():
if type(value) == tuple: if type(value) == tuple:
path, check_disabled = value path, check_disabled = value
try: try:
opt_value = getattr(config, path) opt_value = getattr(config, path)
opt = config.unwrap_from_path(path)
except DisabledOptionError, e: except DisabledOptionError, e:
if chek_disabled: if chek_disabled:
continue continue
raise DisabledOptionError(e) 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: 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: try:
#return getattr(autolib, callback)(name, config)
#return g[callback](name, config, **callback_params)
from creole import eosfunc
return getattr(eosfunc, callback)(**tcparams) return getattr(eosfunc, callback)(**tcparams)
except AttributeError: except AttributeError:
import traceback
traceback.print_exc()
raise SpecialOwnersError("callback: {0} not found for " raise SpecialOwnersError("callback: {0} not found for "
"option: {1}".format(callback, name)) "option: {1}".format(callback, name))