Multi()'s and append

This commit is contained in:
gwen 2012-12-05 10:54:32 +01:00
parent 7784a82cde
commit 7574ac43aa
2 changed files with 45 additions and 16 deletions

View File

@ -161,11 +161,10 @@ class Config(object):
def _get_master_len(self, slave_name): def _get_master_len(self, slave_name):
try: try:
parent_cfg = self master_name = self._cfgimpl_descr.get_master_name()
if parent_cfg is None: if master_name == slave_name:
return None return None
master_name = parent_cfg._cfgimpl_descr.get_master_name() master_value = self._cfgimpl_values[master_name]
master_value = parent_cfg._cfgimpl_values[master_name]
return len(master_value) return len(master_value)
except TypeError, err: except TypeError, err:
# in this case we just don't care about the len # in this case we just don't care about the len
@ -176,10 +175,11 @@ class Config(object):
if master_len == None: if master_len == None:
return True return True
if master_len != len(slave_value): if master_len != len(slave_value):
master_name = self._cfgimpl_descr.get_master_name()
raise ValueError("invalid len for the group of" raise ValueError("invalid len for the group of"
" the option {0}".format(slave_name)) " the option {0}".format(slave_name))
def fill_multi(self, name, result, default_multi=None): def fill_multi(self, name, result, use_default_multi=False, default_multi=None):
"""fills a multi option with default and calculated values """fills a multi option with default and calculated values
""" """
value = self._cfgimpl_values[name] value = self._cfgimpl_values[name]
@ -191,7 +191,8 @@ class Config(object):
_result = [] _result = []
for i in range(master_len): for i in range(master_len):
_result.append(result) _result.append(result)
elif default_multi != None: elif use_default_multi != False:
_result = result
if master_len != None: if master_len != None:
slave_len = len(result) slave_len = len(result)
if slave_len > master_len: if slave_len > master_len:
@ -267,7 +268,9 @@ class Config(object):
if not opt_or_descr.has_callback() and opt_or_descr.is_forced_on_freeze(): if not opt_or_descr.has_callback() and opt_or_descr.is_forced_on_freeze():
value = opt_or_descr.getdefault() value = opt_or_descr.getdefault()
if opt_or_descr.is_multi(): if opt_or_descr.is_multi():
value = self.fill_multi(name, result, opt_or_descr.getdefault_multi()) value = self.fill_multi(name, value,
use_default_multi=True,
default_multi=opt_or_descr.getdefault_multi())
self._cfgimpl_values[name] = value self._cfgimpl_values[name] = value
opt_or_descr.setowner(self, 'default') opt_or_descr.setowner(self, 'default')
self._test_mandatory(name, opt_or_descr) self._test_mandatory(name, opt_or_descr)
@ -314,12 +317,18 @@ class Config(object):
if who == None: if who == None:
who = settings.owner who = settings.owner
if child.is_multi(): if child.is_multi():
if who != 'default':
if type(value) != Multi: if type(value) != Multi:
if type(value) == list: if type(value) == list:
value = Multi(value, self, child) value = Multi(value, self, child)
else: else:
raise ConfigError("invalid value for option:" raise ConfigError("invalid value for option:"
" {0} that is set to multi".format(name)) " {0} that is set to multi".format(name))
else:
value = self.fill_multi(name, child.getdefault(),
use_default_multi=True,
default_multi=child.getdefault_multi())
self._valid_len(name, value)
child.setoption(self, value, who) child.setoption(self, value, who)
child.setowner(self, who) child.setowner(self, who)
else: else:

View File

@ -50,12 +50,21 @@ class Multi(list):
""" """
self.config = config self.config = config
self.opt = opt self.opt = opt
if self.opt.is_master(config):
super(Multi, self).__init__(lst)
for l in lst:
try:
self.append(l, add_master=False)
except Exception, err:
print err
else:
self.config._valid_len(self.opt._name, lst)
super(Multi, self).__init__(lst) super(Multi, self).__init__(lst)
def __setitem__(self, key, value): def __setitem__(self, key, value):
return self._setvalue(value, key, who=settings.owner) return self._setvalue(value, key, who=settings.owner)
def append(self, value): def append(self, value, add_master=True):
"""the list value can be updated (appened) """the list value can be updated (appened)
only if the option is a master""" only if the option is a master"""
try: try:
@ -70,7 +79,10 @@ class Multi(list):
multis.append(multi) multis.append(multi)
for multi in multis: for multi in multis:
if master == multi.opt._name: if master == multi.opt._name:
if add_master:
ret = multi._setvalue(value, who=settings.owner) ret = multi._setvalue(value, who=settings.owner)
else:
ret = value
else: else:
default_value = multi.opt.getdefault_multi() default_value = multi.opt.getdefault_multi()
multi._setvalue(default_value) multi._setvalue(default_value)
@ -350,6 +362,13 @@ class Option(HiddenBaseType, DisabledBaseType):
def getkey(self, value): def getkey(self, value):
return value return value
def is_master(self, config):
try:
self.master = config._cfgimpl_descr.get_master_name()
except TypeError:
return False
return self.master is not None and self.master == self._name
# ____________________________________________________________ # ____________________________________________________________
"freeze utility" "freeze utility"
def freeze(self): def freeze(self):
@ -539,6 +558,7 @@ class OptionDescription(HiddenBaseType, DisabledBaseType):
raise TypeError('get_master_name() shall not be called in case of ' raise TypeError('get_master_name() shall not be called in case of '
'non-master OptionDescription') 'non-master OptionDescription')
return self.master return self.master
# ____________________________________________________________ # ____________________________________________________________
"actions API" "actions API"
def hide(self): def hide(self):