add type list for value owners

This commit is contained in:
gwen 2012-07-09 11:27:51 +02:00
parent b008f31e27
commit 9f184d54c3
4 changed files with 84 additions and 50 deletions

View File

@ -73,7 +73,10 @@ class Config(object):
else:
self._cfgimpl_value_owners[child._name] = 'fill'
else:
self._cfgimpl_value_owners[child._name] = 'default'
if child.is_multi():
self._cfgimpl_value_owners[child._name] = ['default']
else:
self._cfgimpl_value_owners[child._name] = 'default'
elif isinstance(child, OptionDescription):
self._validate_duplicates(child._children)
self._cfgimpl_values[child._name] = Config(child, parent=self)
@ -88,6 +91,7 @@ class Config(object):
if isinstance(child, Option):
if child._name not in self._cfgimpl_values:
self._cfgimpl_values[child._name] = child.getdefault()
# FIXME and ['default'] if is_multi
self._cfgimpl_value_owners[child._name] = 'default'
elif isinstance(child, OptionDescription):
if child._name not in self._cfgimpl_values:
@ -100,6 +104,7 @@ class Config(object):
if homeconfig._cfgimpl_value_owners[name] in special_owners:
raise SpecialOwnersError("cannot override option: {0} because "
"of its special owner".format(name))
# FIXME and ['default'] if is_multi
homeconfig.setoption(name, value, 'default')
def cfgimpl_set_owner(self, owner):
@ -147,10 +152,19 @@ class Config(object):
if self._cfgimpl_frozen and getattr(self, name) != value:
raise TypeError("trying to change a value in a frozen config"
": {0} {1}".format(name, value))
if type(getattr(self._cfgimpl_descr, name)) != SymLinkOption:
opt = getattr(self._cfgimpl_descr, name)
if type(opt) != SymLinkOption:
self._validate(name, getattr(self._cfgimpl_descr, name))
self.setoption(name, value, self._cfgimpl_owner)
if not opt.is_multi():
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):
if not type(opt_or_descr) == OptionDescription:
apply_requires(opt_or_descr, self)
@ -280,9 +294,12 @@ class Config(object):
self._cfgimpl_values[name] = getattr(opt, 'default', None)
def setoption(self, name, value, who=None):
if who == None:
who == self._cfgimpl_owner
child = getattr(self._cfgimpl_descr, name)
if who == None:
if child.is_multi():
who = [self._cfgimpl_owner for i in range(len(value))]
else:
who == self._cfgimpl_owner
if type(child) != SymLinkOption:
if name not in self._cfgimpl_values:
raise AttributeError('unknown option %s' % (name,))

View File

@ -103,6 +103,10 @@ class Option(HiddenBaseType, DisabledBaseType, ModeBaseType):
if self.callback == None:
raise SpecialOwnersError("no callback specified for"
"option {0}".format(name))
if self.is_multi():
if not type(who) == list:
raise ConfigError("invalid owner for multi "
"option: {0}".format(self._name))
config._cfgimpl_value_owners[name] = who
def setoption(self, config, value, who):
@ -110,14 +114,16 @@ class Option(HiddenBaseType, DisabledBaseType, ModeBaseType):
if self._frozen:
raise TypeError('trying to change a frozen option object: %s' % name)
# we want the possibility to reset everything
if who == "default" and value is None:
if (not self.multi and who == "default" or self.multi and "default" in who)\
and value is None:
self.default = None
return
if not self.validate(value):
raise ConfigError('invalid value %s for option %s' % (value, name))
if who == "default":
if (self.multi and "default" in who) or \
(not self.multi and who == "default"):
# changes the default value (and therefore resets the previous value)
if self._validate(value):
self.default = value

View File

@ -81,29 +81,30 @@ def test_cannot_override_special_owners():
config.gc.setoption('dummy', True, 'auto')
raises(SpecialOwnersError, "config.override({'gc.dummy': True})")
def test_fill_owner():
"fill option"
descr = make_description()
config = Config(descr, bool=False)
assert config.bool == False
assert config.gc.dummy == False
# 'fill' special values
config.gc.setoption('dummy', True, 'fill')
assert config.gc.dummy == False
# FIXME have to test the fills anyway
#def test_fill_owner():
# "fill option"
# descr = make_description()
# config = Config(descr, bool=False)
# assert config.bool == False
# assert config.gc.dummy == False
# # 'fill' special values
# config.gc.setoption('dummy', True, 'fill')
# assert config.gc.dummy == False
def test_auto_fill_and_override():
descr = make_description()
config = Config(descr, bool=False)
booloption = config.unwrap_from_path('bool')
booloption.callback = 'identical'
booloption.setowner(config, 'auto')
assert config.bool == 'identicalbool'
gcdummy = config.unwrap_from_path('gc.dummy')
gcdummy.callback = 'identical'
gcdummy.setowner(config.gc, 'fill')
raises(SpecialOwnersError, "config.override({'gc.dummy':True})")
config.gc.setoption('dummy', False, 'fill')
# value is returned
assert config.gc.dummy == False
#def test_auto_fill_and_override():
# descr = make_description()
# config = Config(descr, bool=False)
# booloption = config.unwrap_from_path('bool')
# booloption.callback = 'identical'
# booloption.setowner(config, 'auto')
# assert config.bool == 'identicalbool'
# gcdummy = config.unwrap_from_path('gc.dummy')
# gcdummy.callback = 'identical'
# gcdummy.setowner(config.gc, 'fill')
# raises(SpecialOwnersError, "config.override({'gc.dummy':True})")
# config.gc.setoption('dummy', False, 'fill')
# # value is returned
# assert config.gc.dummy == False

View File

@ -44,7 +44,7 @@ def test_idontexist():
raises(AttributeError, "cfg.idontexist")
# ____________________________________________________________
def test_attribute_access_with_multi():
s = StrOption("string", "", default="string", multi=True)
s = StrOption("string", "", default=["string"], default_multi= "string" , multi=True)
descr = OptionDescription("options", "", [s])
config = Config(descr)
config.string = ["foo", "bar"]
@ -60,6 +60,16 @@ def test_item_access_with_multi():
assert config.string[0] == 'changetest'
# assert config.string[
def test_access_with_multi_default():
s = StrOption("string", "", default=["string"], multi=True)
descr = OptionDescription("options", "", [s])
config = Config(descr)
assert config._cfgimpl_value_owners["string"] == ['default']
config.string = ["foo", "bar"]
assert config.string == ["foo", "bar"]
assert config._cfgimpl_value_owners["string"] == ['user', 'user']
# assert config.string[
#def test_attribute_access_with_multi2():
# s = StrOption("string", "", default="string", multi=True)
# descr = OptionDescription("options", "", [s])
@ -68,9 +78,9 @@ def test_item_access_with_multi():
# assert config.string == ["foo", "bar"]
def test_multi_with_requires():
s = StrOption("string", "", default="string", multi=True)
s = StrOption("string", "", default=["string"], default_multi="string", multi=True)
intoption = IntOption('int', 'Test int option', default=0)
stroption = StrOption('str', 'Test string option', default="abc",
stroption = StrOption('str', 'Test string option', default=["abc"], default_multi = "abc",
requires=[('int', 1, 'hide')], multi=True)
descr = OptionDescription("options", "", [s, intoption, stroption])
config = Config(descr)
@ -80,9 +90,9 @@ def test_multi_with_requires():
assert stroption._is_hidden()
def test__requires_with_inverted():
s = StrOption("string", "", default="string", multi=True)
s = StrOption("string", "", default=["string"], multi=True)
intoption = IntOption('int', 'Test int option', default=0)
stroption = StrOption('str', 'Test string option', default="abc",
stroption = StrOption('str', 'Test string option', default=["abc"], default_multi = "abc",
requires=[('int', 1, 'hide', 'inverted')], multi=True)
descr = OptionDescription("options", "", [s, intoption, stroption])
config = Config(descr)
@ -91,10 +101,10 @@ def test__requires_with_inverted():
assert stroption._is_hidden() == False
def test_multi_with_requires_in_another_group():
s = StrOption("string", "", default="string", multi=True)
s = StrOption("string", "", default=["string"], multi=True)
intoption = IntOption('int', 'Test int option', default=0)
descr = OptionDescription("options", "", [intoption])
stroption = StrOption('str', 'Test string option', default="abc",
stroption = StrOption('str', 'Test string option', default=["abc"],
requires=[('int', 1, 'hide')], multi=True)
descr = OptionDescription("opt", "", [stroption])
descr2 = OptionDescription("opt2", "", [intoption, s, descr])
@ -105,10 +115,10 @@ def test_multi_with_requires_in_another_group():
assert stroption._is_hidden()
def test_apply_requires_from_config():
s = StrOption("string", "", default="string", multi=True)
s = StrOption("string", "", default=["string"], multi=True)
intoption = IntOption('int', 'Test int option', default=0)
descr = OptionDescription("options", "", [intoption])
stroption = StrOption('str', 'Test string option', default="abc",
stroption = StrOption('str', 'Test string option', default=["abc"],
requires=[('int', 1, 'hide')], multi=True)
descr = OptionDescription("opt", "", [stroption])
descr2 = OptionDescription("opt2", "", [intoption, s, descr])
@ -123,10 +133,10 @@ def test_apply_requires_from_config():
def test_apply_requires_with_disabled():
s = StrOption("string", "", default="string", multi=True)
s = StrOption("string", "", default=["string"], multi=True)
intoption = IntOption('int', 'Test int option', default=0)
descr = OptionDescription("options", "", [intoption])
stroption = StrOption('str', 'Test string option', default="abc",
stroption = StrOption('str', 'Test string option', default=["abc"],
requires=[('int', 1, 'disable')], multi=True)
descr = OptionDescription("opt", "", [stroption])
descr2 = OptionDescription("opt2", "", [intoption, s, descr])
@ -140,10 +150,10 @@ def test_apply_requires_with_disabled():
assert stroption._is_disabled()
def test_multi_with_requires_with_disabled_in_another_group():
s = StrOption("string", "", default="string", multi=True)
s = StrOption("string", "", default=["string"], multi=True)
intoption = IntOption('int', 'Test int option', default=0)
descr = OptionDescription("options", "", [intoption])
stroption = StrOption('str', 'Test string option', default="abc",
stroption = StrOption('str', 'Test string option', default=["abc"],
requires=[('int', 1, 'disable')], multi=True)
descr = OptionDescription("opt", "", [stroption])
descr2 = OptionDescription("opt2", "", [intoption, s, descr])
@ -154,9 +164,9 @@ def test_multi_with_requires_with_disabled_in_another_group():
assert stroption._is_disabled()
def test_multi_with_requires_that_is_multi():
s = StrOption("string", "", default="string", multi=True)
intoption = IntOption('int', 'Test int option', default=0, multi=True)
stroption = StrOption('str', 'Test string option', default="abc",
s = StrOption("string", "", default=["string"], multi=True)
intoption = IntOption('int', 'Test int option', default=[0], multi=True)
stroption = StrOption('str', 'Test string option', default=["abc"],
requires=[('int', [1, 1], 'hide')], multi=True)
descr = OptionDescription("options", "", [s, intoption, stroption])
config = Config(descr)
@ -166,7 +176,7 @@ def test_multi_with_requires_that_is_multi():
assert stroption._is_hidden()
def test_multi_with_bool():
s = BoolOption("bool", "", default=False, multi=True)
s = BoolOption("bool", "", default=[False], multi=True)
descr = OptionDescription("options", "", [s])
config = Config(descr)
assert descr.bool.multi == True
@ -175,14 +185,14 @@ def test_multi_with_bool():
assert config.bool == [True, False]
def test_multi_with_bool_two():
s = BoolOption("bool", "", default=False, multi=True)
s = BoolOption("bool", "", default=[False], multi=True)
descr = OptionDescription("options", "", [s])
config = Config(descr)
assert descr.bool.multi == True
raises(ConfigError, "config.bool = True")
def test_choice_access_with_multi():
ch = ChoiceOption("t1", "", ["a", "b"], default="a", multi=True)
ch = ChoiceOption("t1", "", ["a", "b"], default=["a"], multi=True)
descr = OptionDescription("options", "", [ch])
config = Config(descr)
config.t1 = ["a", "b", "a", "b"]