master len without getattr

This commit is contained in:
gwen 2012-12-04 16:22:39 +01:00
parent 6538231817
commit 0db34bbeba
2 changed files with 34 additions and 25 deletions

View File

@ -161,13 +161,13 @@ class Config(object):
def _get_master_len(self, slave_name): def _get_master_len(self, slave_name):
try: try:
parent_cfg = self._cfgimpl_parent parent_cfg = self
if parent_cfg is None: if parent_cfg is None:
return None return None
master_name = parent_cfg._cfgimpl_descr.get_master_name() master_name = parent_cfg._cfgimpl_descr.get_master_name()
master_value = getattr(parent_cfg, master_name) master_value = parent_cfg._cfgimpl_values[master_name]
return len(master_value) return len(master_value)
except TypeError: 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
return None return None
@ -176,8 +176,8 @@ 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):
raise ValueError("invalid len for the group {0}" raise ValueError("invalid len for the group of"
"in the option {1} ".format(master_name, slave_name)) " the option {0}".format(slave_name))
def fill_multi(self, name, result, default_multi=None): def fill_multi(self, name, result, default_multi=None):
"""fills a multi option with default and calculated values """fills a multi option with default and calculated values

View File

@ -63,14 +63,18 @@ class Multi(list):
if master != self.opt._name: if master != self.opt._name:
raise IndexError("in a group with a master, you mustn't add " raise IndexError("in a group with a master, you mustn't add "
"a value in a slave's Multi value") "a value in a slave's Multi value")
for name, multi in self.config:
if master == multi.opt._name:
multi._setvalue(value, who=settings.owner)
else:
default_value = multi.opt.getdefault_multi()
multi._setvalue(default_value)
except TypeError: except TypeError:
self._setvalue(value, who=settings.owner) return self._setvalue(value, who=settings.owner)
multis = []
for name, multi in self.config:
multis.append(multi)
for multi in multis:
if master == multi.opt._name:
ret = multi._setvalue(value, who=settings.owner)
else:
default_value = multi.opt.getdefault_multi()
multi._setvalue(default_value)
return ret
def _setvalue(self, value, key=None, who=None): def _setvalue(self, value, key=None, who=None):
if value != None: if value != None:
@ -96,19 +100,24 @@ class Multi(list):
if master != self.opt._name: if master != self.opt._name:
raise IndexError("in a group with a master, you mustn't remove " raise IndexError("in a group with a master, you mustn't remove "
"a value in a slave's Multi value") "a value in a slave's Multi value")
for name, multi in self.config:
if master == multi.opt._name:
multi._pop(key)
else:
change_who = False
# the value owner has to be updated because
# the default value doesn't have the same length
# of the new value
if len(multi.opt.getdefault()) >= len(multi):
change_who = True
multi._pop(key, change_who=change_who)
except TypeError: except TypeError:
self._pop(key) return self._pop(key)
multis = []
for name, multi in self.config:
multis.append(multi)
for multi in multis:
if master == multi.opt._name:
ret = multi._pop(key)
else:
change_who = False
# the value owner has to be updated because
# the default value doesn't have the same length
# of the new value
if len(multi.opt.getdefault()) >= len(multi):
change_who = True
multi._pop(key, change_who=change_who)
return ret
def _pop(self, key, change_who=True): def _pop(self, key, change_who=True):
oldvalue = list(self) oldvalue = list(self)
@ -527,7 +536,7 @@ class OptionDescription(HiddenBaseType, DisabledBaseType):
def get_master_name(self): def get_master_name(self):
if self.master is None: if self.master is None:
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
# ____________________________________________________________ # ____________________________________________________________