pop and append in multi values
This commit is contained in:
parent
4393da13ab
commit
6538231817
2 changed files with 59 additions and 16 deletions
|
@ -74,7 +74,7 @@ class Config(object):
|
||||||
if isinstance(child, Option):
|
if isinstance(child, Option):
|
||||||
if child.is_multi():
|
if child.is_multi():
|
||||||
childdef = Multi(copy(child.getdefault()), config=self,
|
childdef = Multi(copy(child.getdefault()), config=self,
|
||||||
child=child)
|
opt=child)
|
||||||
self._cfgimpl_values[child._name] = childdef
|
self._cfgimpl_values[child._name] = childdef
|
||||||
self._cfgimpl_previous_values[child._name] = list(childdef)
|
self._cfgimpl_previous_values[child._name] = list(childdef)
|
||||||
else:
|
else:
|
||||||
|
@ -203,7 +203,7 @@ class Config(object):
|
||||||
_result.append(default_multi)
|
_result.append(default_multi)
|
||||||
else:
|
else:
|
||||||
_result = result
|
_result = result
|
||||||
return Multi(_result, value.config, value.child)
|
return Multi(_result, value.config, opt=value.opt)
|
||||||
|
|
||||||
def _getattr(self, name, permissive=False):
|
def _getattr(self, name, permissive=False):
|
||||||
"""
|
"""
|
||||||
|
|
|
@ -40,38 +40,81 @@ for act1, act2 in requires_actions:
|
||||||
# multi types
|
# multi types
|
||||||
|
|
||||||
class Multi(list):
|
class Multi(list):
|
||||||
"container that support items for the values of list (multi) options"
|
"""multi options values container
|
||||||
def __init__(self, lst, config, child):
|
that support item notation for the values of multi options"""
|
||||||
|
def __init__(self, lst, config, opt):
|
||||||
|
"""
|
||||||
|
:lst: the Multi wraps a list value
|
||||||
|
:param config: the parent config
|
||||||
|
:param opt: the option object that have this Multi value
|
||||||
|
"""
|
||||||
self.config = config
|
self.config = config
|
||||||
self.child = child
|
self.opt = opt
|
||||||
super(Multi, self).__init__(lst)
|
super(Multi, self).__init__(lst)
|
||||||
|
|
||||||
def __setitem__(self, key, value):
|
def __setitem__(self, key, value):
|
||||||
return self.setoption(value, key)
|
return self._setvalue(value, key, who=settings.owner)
|
||||||
|
|
||||||
def append(self, value):
|
def append(self, value):
|
||||||
self.setoption(value)
|
"""the list value can be updated (appened)
|
||||||
|
only if the option is a master"""
|
||||||
|
try:
|
||||||
|
master = self.config._cfgimpl_descr.get_master_name()
|
||||||
|
if master != self.opt._name:
|
||||||
|
raise IndexError("in a group with a master, you mustn't add "
|
||||||
|
"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:
|
||||||
|
self._setvalue(value, who=settings.owner)
|
||||||
|
|
||||||
def setoption(self, value, key=None, who=None):
|
def _setvalue(self, value, key=None, who=None):
|
||||||
if who is None:
|
|
||||||
who = settings.owner
|
|
||||||
if value != None:
|
if value != None:
|
||||||
if not self.child._validate(value):
|
if not self.opt._validate(value):
|
||||||
raise ConfigError("invalid value {0} "
|
raise ConfigError("invalid value {0} "
|
||||||
"for option {1}".format(str(value), self.child._name))
|
"for option {1}".format(str(value), self.opt._name))
|
||||||
oldvalue = list(self)
|
oldvalue = list(self)
|
||||||
if key is None:
|
if key is None:
|
||||||
ret = super(Multi, self).append(value)
|
ret = super(Multi, self).append(value)
|
||||||
else:
|
else:
|
||||||
ret = super(Multi, self).__setitem__(key, value)
|
ret = super(Multi, self).__setitem__(key, value)
|
||||||
self.child.setowner(self.config, who)
|
if who != None:
|
||||||
self.config._cfgimpl_previous_values[self.child._name] = oldvalue
|
self.opt.setowner(self.config, who)
|
||||||
|
self.config._cfgimpl_previous_values[self.opt._name] = oldvalue
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
def pop(self, key):
|
def pop(self, key):
|
||||||
|
"""the list value can be updated (poped)
|
||||||
|
only if the option is a master
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
master = self.config._cfgimpl_descr.get_master_name()
|
||||||
|
if master != self.opt._name:
|
||||||
|
raise IndexError("in a group with a master, you mustn't remove "
|
||||||
|
"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:
|
||||||
|
self._pop(key)
|
||||||
|
|
||||||
|
def _pop(self, key, change_who=True):
|
||||||
oldvalue = list(self)
|
oldvalue = list(self)
|
||||||
self.child.setowner(self.config, settings.owner)
|
if change_who:
|
||||||
self.config._cfgimpl_previous_values[self.child._name] = oldvalue
|
self.opt.setowner(self.config, settings.owner)
|
||||||
|
self.config._cfgimpl_previous_values[self.opt._name] = oldvalue
|
||||||
return super(Multi, self).pop(key)
|
return super(Multi, self).pop(key)
|
||||||
# ____________________________________________________________
|
# ____________________________________________________________
|
||||||
#
|
#
|
||||||
|
|
Loading…
Reference in a new issue