value owners can be lists

This commit is contained in:
gwen 2012-07-09 17:34:39 +02:00
parent 9f184d54c3
commit 10df0e1cd1
3 changed files with 29 additions and 23 deletions

View file

@ -104,7 +104,6 @@ class Config(object):
if homeconfig._cfgimpl_value_owners[name] in special_owners: if homeconfig._cfgimpl_value_owners[name] in special_owners:
raise SpecialOwnersError("cannot override option: {0} because " raise SpecialOwnersError("cannot override option: {0} because "
"of its special owner".format(name)) "of its special owner".format(name))
# FIXME and ['default'] if is_multi
homeconfig.setoption(name, value, 'default') homeconfig.setoption(name, value, 'default')
def cfgimpl_set_owner(self, owner): def cfgimpl_set_owner(self, owner):
@ -152,18 +151,9 @@ class Config(object):
if self._cfgimpl_frozen and getattr(self, name) != value: if self._cfgimpl_frozen and getattr(self, name) != value:
raise TypeError("trying to change a value in a frozen config" raise TypeError("trying to change a value in a frozen config"
": {0} {1}".format(name, value)) ": {0} {1}".format(name, value))
opt = getattr(self._cfgimpl_descr, name) if type(getattr(self._cfgimpl_descr, name)) != SymLinkOption:
if type(opt) != SymLinkOption:
self._validate(name, getattr(self._cfgimpl_descr, name)) self._validate(name, getattr(self._cfgimpl_descr, name))
if not opt.is_multi(): self.setoption(name, value, self._cfgimpl_owner)
self.setoption(name, value, self._cfgimpl_owner)
else:
if type(value) != list:
raise ConfigError("invalid value for multi "
"with option: {0}".format(name))
self.setoption(name, value, [self._cfgimpl_owner for i in range(len(value))])
else:
self.setoption(name, value, self._cfgimpl_owner)
def _validate(self, name, opt_or_descr): def _validate(self, name, opt_or_descr):
if not type(opt_or_descr) == OptionDescription: if not type(opt_or_descr) == OptionDescription:
@ -294,12 +284,22 @@ class Config(object):
self._cfgimpl_values[name] = getattr(opt, 'default', None) self._cfgimpl_values[name] = getattr(opt, 'default', None)
def setoption(self, name, value, who=None): def setoption(self, name, value, who=None):
"who is **not necessarily** a owner, because it cannot be a list"
child = getattr(self._cfgimpl_descr, name) child = getattr(self._cfgimpl_descr, name)
if who == None: if who == None:
if child.is_multi(): if child.is_multi():
who = [self._cfgimpl_owner for i in range(len(value))] newowner = [self._cfgimpl_owner for i in range(len(value))]
else: else:
who == self._cfgimpl_owner newowner = self._cfgimpl_owner
else:
if type(child) != SymLinkOption:
if child.is_multi():
if type(value) != list:
raise ConfigError("invalid value for option:"
" {0} that is set to multi".format(name))
newowner = [who for i in range(len(value))]
else:
newowner = who
if type(child) != SymLinkOption: if type(child) != SymLinkOption:
if name not in self._cfgimpl_values: if name not in self._cfgimpl_values:
raise AttributeError('unknown option %s' % (name,)) raise AttributeError('unknown option %s' % (name,))
@ -318,7 +318,7 @@ class Config(object):
if who == 'auto': if who == 'auto':
if not child._is_hidden(): if not child._is_hidden():
child.hide() child.hide()
self._cfgimpl_value_owners[name] = who self._cfgimpl_value_owners[name] = newowner
else: else:
homeconfig = self._cfgimpl_get_toplevel() homeconfig = self._cfgimpl_get_toplevel()
child.setoption(homeconfig, value, who) child.setoption(homeconfig, value, who)

View file

@ -95,35 +95,39 @@ class Option(HiddenBaseType, DisabledBaseType, ModeBaseType):
def getcallback_params(self): def getcallback_params(self):
return self.callback_params return self.callback_params
def setowner(self, config, who): def setowner(self, config, owner):
# owner is a **real* owner, a list is actually allowable here
name = self._name name = self._name
if self._frozen: if self._frozen:
raise TypeError("trying to change a frozen option's owner: %s" % name) raise TypeError("trying to change a frozen option's owner: %s" % name)
if who in special_owners: if owner in special_owners:
if self.callback == None: if self.callback == None:
raise SpecialOwnersError("no callback specified for" raise SpecialOwnersError("no callback specified for"
"option {0}".format(name)) "option {0}".format(name))
if self.is_multi(): if self.is_multi():
if not type(who) == list: if not type(owner) == list:
raise ConfigError("invalid owner for multi " raise ConfigError("invalid owner for multi "
"option: {0}".format(self._name)) "option: {0}".format(self._name))
config._cfgimpl_value_owners[name] = who config._cfgimpl_value_owners[name] = owner
def setoption(self, config, value, who): def setoption(self, config, value, who):
"who is **not necessarily** a owner because it cannot be a list"
name = self._name name = self._name
if self._frozen: if self._frozen:
raise TypeError('trying to change a frozen option object: %s' % name) raise TypeError('trying to change a frozen option object: %s' % name)
# we want the possibility to reset everything # we want the possibility to reset everything
if (not self.multi and who == "default" or self.multi and "default" in who)\ # if (not self.multi and who == "default" or self.multi and "default" in who)\
and value is None: # and value is None:
if who == "default" and value is None:
self.default = None self.default = None
return return
if not self.validate(value): if not self.validate(value):
raise ConfigError('invalid value %s for option %s' % (value, name)) raise ConfigError('invalid value %s for option %s' % (value, name))
if (self.multi and "default" in who) or \ # if (self.multi and "default" in who) or \
(not self.multi and who == "default"): # (not self.multi and who == "default"):
if who == "default":
# changes the default value (and therefore resets the previous value) # changes the default value (and therefore resets the previous value)
if self._validate(value): if self._validate(value):
self.default = value self.default = value

View file

@ -68,6 +68,8 @@ def test_access_with_multi_default():
config.string = ["foo", "bar"] config.string = ["foo", "bar"]
assert config.string == ["foo", "bar"] assert config.string == ["foo", "bar"]
assert config._cfgimpl_value_owners["string"] == ['user', 'user'] assert config._cfgimpl_value_owners["string"] == ['user', 'user']
print config._cfgimpl_value_owners["string"]
# FIXME gwen haha
# assert config.string[ # assert config.string[
#def test_attribute_access_with_multi2(): #def test_attribute_access_with_multi2():