From 13c4819b14707b1fc43bad29d8d5c388a27ec5b4 Mon Sep 17 00:00:00 2001 From: gwen Date: Fri, 6 Jul 2012 11:46:08 +0200 Subject: [PATCH] multi types with None --- autolib.py | 37 ++++++++++++++++++++++--------------- config.py | 4 +--- option.py | 10 ++++++++-- 3 files changed, 31 insertions(+), 20 deletions(-) diff --git a/autolib.py b/autolib.py index c620344..febc172 100644 --- a/autolib.py +++ b/autolib.py @@ -19,8 +19,7 @@ # 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, ConfigError -from creole import eosfunc +from tiramisu.error import DisabledOptionError, SpecialOwnersError # ____________________________________________________________ # automatic Option object special_owners = ['auto', 'fill'] @@ -52,12 +51,13 @@ def calc_factory(name, callback, callback_params, config): raise DisabledOptionError(e) 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 + if opt_value != None: + len_value = len(opt_value) + if len_multi != 0 and len_multi != len_value: + raise SpecialOwnersError('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: @@ -69,19 +69,26 @@ def calc_factory(name, callback, callback_params, config): tcp = {} for key, couple in tcparams.items(): value, ismulti = couple - if ismulti: + if ismulti and value != None: tcp[key] = value[incr] else: tcp[key] = value - ret.append(calculate(callback, tcp)) + ret.append(calculate(name, callback, tcp)) return ret else: - return calculate(callback, tcparams) + tcp = {} + for key, couple in tcparams.items(): + tcp[key] = couple[0] + return calculate(name, callback, tcp) -def calculate(callback, tcparams): +def calculate(name, callback, tcparams): try: + # XXX not only creole... + from creole import eosfunc return getattr(eosfunc, callback)(**tcparams) - except AttributeError: - raise SpecialOwnersError("callback: {0} not found for " - "option: {1}".format(callback, name)) + except AttributeError, err: + import traceback + traceback.print_exc() + raise SpecialOwnersError("callback: {0} return error {1} for " + "option: {2}".format(callback, str(err), name)) diff --git a/config.py b/config.py index df570d2..b821993 100644 --- a/config.py +++ b/config.py @@ -203,9 +203,8 @@ class Config(object): if owner == 'fill' and None not in value: return value else: - if owner == 'fill': + if owner == 'fill' and value != None: return value - # ______________________________________________________________ result = special_owner_factory(name, owner, value=value, callback=opt_or_descr.getcallback(), @@ -233,7 +232,6 @@ class Config(object): else: _result = result return _result - # ______________________________________________________________ # mandatory options if not isinstance(opt_or_descr, OptionDescription): homeconfig = self._cfgimpl_get_toplevel() diff --git a/option.py b/option.py index 3848850..0a3b9d1 100644 --- a/option.py +++ b/option.py @@ -34,14 +34,20 @@ group_types = ['default', 'family', 'group', 'master'] class Option(HiddenBaseType, DisabledBaseType, ModeBaseType): #reminder: an Option object is **not** a container for the value _frozen = False - def __init__(self, name, doc, default=None, requires=None, - mandatory=False, multi=False, callback=None, + def __init__(self, name, doc, default=None, default_multi=None, + requires=None, mandatory=False, multi=False, callback=None, callback_params=None, mode='normal'): self._name = name self.doc = doc self._requires = requires self._mandatory = mandatory self.multi = multi + if not self.multi and default_multi is not None: + raise ConfigError("a default_multi is set whereas multi is False" + " in option: {0}".format(name)) + self.default_multi = default_multi + #if self.multi and default_multi is None: + # _cfgimpl_warnings[name] = DefaultMultiWarning self.callback = callback if self.callback is None and callback_params is not None: raise ConfigError("params defined for a callback function but"