possibility to have a default as multi

This commit is contained in:
gwen 2012-06-29 11:48:03 +02:00
parent 97b000bf1d
commit f3a9a96714
2 changed files with 57 additions and 88 deletions

View File

@ -46,13 +46,31 @@ class Option(HiddenBaseType, DisabledBaseType, ModeBaseType):
raise ConfigError("mode {0} not available".format(mode)) raise ConfigError("mode {0} not available".format(mode))
self.mode = mode self.mode = mode
if default != None: if default != None:
if not self.validate(default): if self.multi == True and type(default) != list:
_default = [default]
else:
_default = default
if not self.validate(_default):
raise ConfigError("invalid default value {0} " raise ConfigError("invalid default value {0} "
"for option {1}".format(default, name)) "for option {1}".format(default, name))
self.default = default self.default = default
def validate(self, value): def validate(self, value):
raise NotImplementedError('abstract base class') if self.multi == False:
# None allows the reset of the value
if value != None:
return self._validate(value)
else:
if type(value) != list:
raise ConfigError("invalid value {0} "
"for option {1} which must be a list".format(value,
self._name))
for val in value:
if val != None:
# None allows the reset of the value
return self._validate(val)
return True
def getdefault(self): def getdefault(self):
return self.default return self.default
@ -81,8 +99,10 @@ class Option(HiddenBaseType, DisabledBaseType, ModeBaseType):
if who == "default" and value is None: if who == "default" and value is None:
self.default = None self.default = None
return return
if value != None and 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 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)
self.default = value self.default = value
@ -135,14 +155,8 @@ class ChoiceOption(Option):
name = self._name name = self._name
super(ChoiceOption, self).setoption(config, value, who) super(ChoiceOption, self).setoption(config, value, who)
def validate(self, value): def _validate(self, value):
if self.multi == False:
return value is None or value in self.values return value is None or value in self.values
else:
for val in value:
if not (val is None or val in self.values):
return False
return True
class BoolOption(Option): class BoolOption(Option):
opt_type = 'bool' opt_type = 'bool'
@ -155,17 +169,9 @@ class BoolOption(Option):
# requires=requires, multi=multi, mandatory=mandatory) # requires=requires, multi=multi, mandatory=mandatory)
#self._validator = validator #self._validator = validator
def validate(self, value): def _validate(self, value):
if self.multi == False:
return isinstance(value, bool) return isinstance(value, bool)
else:
try:
for val in value:
if not isinstance(val, bool):
return False
except Exception:
return False
return True
# FIXME config level validator # FIXME config level validator
# def setoption(self, config, value, who): # def setoption(self, config, value, who):
# name = self._name # name = self._name
@ -180,20 +186,12 @@ class IntOption(Option):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super(IntOption, self).__init__(*args, **kwargs) super(IntOption, self).__init__(*args, **kwargs)
def validate(self, value): def _validate(self, value):
if self.multi == False:
try: try:
int(value) int(value)
except TypeError: except TypeError:
return False return False
return True return True
else:
for val in value:
try:
int(val)
except TypeError:
return False
return True
def setoption(self, config, value, who): def setoption(self, config, value, who):
try: try:
@ -207,20 +205,12 @@ class FloatOption(Option):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super(FloatOption, self).__init__(*args, **kwargs) super(FloatOption, self).__init__(*args, **kwargs)
def validate(self, value): def _validate(self, value):
if self.multi == False:
try: try:
float(value) float(value)
except TypeError: except TypeError:
return False return False
return True return True
else:
for val in value:
try:
float(val)
except TypeError:
return False
return True
def setoption(self, config, value, who): def setoption(self, config, value, who):
try: try:
@ -234,15 +224,8 @@ class StrOption(Option):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super(StrOption, self).__init__(*args, **kwargs) super(StrOption, self).__init__(*args, **kwargs)
def validate(self, value): def _validate(self, value):
if self.multi == False:
return isinstance(value, str) return isinstance(value, str)
else:
for val in value:
if not isinstance(val, str):
return False
else:
return True
def setoption(self, config, value, who): def setoption(self, config, value, who):
try: try:
@ -269,16 +252,9 @@ class IPOption(Option):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super(IPOption, self).__init__(*args, **kwargs) super(IPOption, self).__init__(*args, **kwargs)
def validate(self, value): def _validate(self, value):
# by now the validation is nothing but a string, use IPy instead # by now the validation is nothing but a string, use IPy instead
if self.multi == False:
return isinstance(value, str) return isinstance(value, str)
else:
for val in value:
if not isinstance(val, str):
return False
else:
return True
def setoption(self, config, value, who): def setoption(self, config, value, who):
try: try:
@ -292,16 +268,9 @@ class NetmaskOption(Option):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super(NetmaskOption, self).__init__(*args, **kwargs) super(NetmaskOption, self).__init__(*args, **kwargs)
def validate(self, value): def _validate(self, value):
# by now the validation is nothing but a string, use IPy instead # by now the validation is nothing but a string, use IPy instead
if self.multi == False:
return isinstance(value, str) return isinstance(value, str)
else:
for val in value:
if not isinstance(val, str):
return False
else:
return True
def setoption(self, config, value, who): def setoption(self, config, value, who):
try: try:
@ -318,7 +287,7 @@ class ArbitraryOption(Option):
if defaultfactory is not None: if defaultfactory is not None:
assert default is None assert default is None
def validate(self, value): def _validate(self, value):
return True return True
def getdefault(self): def getdefault(self):

View File

@ -50,12 +50,12 @@ def test_attribute_access_with_multi():
config.string = ["foo", "bar"] config.string = ["foo", "bar"]
assert config.string == ["foo", "bar"] assert config.string == ["foo", "bar"]
def test_attribute_access_with_multi(): #def test_attribute_access_with_multi2():
s = StrOption("string", "", default="string", multi=True) # s = StrOption("string", "", default="string", multi=True)
descr = OptionDescription("options", "", [s]) # descr = OptionDescription("options", "", [s])
config = Config(descr) # config = Config(descr)
config.string = ["foo", "bar"] # config.string = ["foo", "bar"]
assert config.string == ["foo", "bar"] # assert config.string == ["foo", "bar"]
def test_multi_with_requires(): def test_multi_with_requires():
s = StrOption("string", "", default="string", multi=True) s = StrOption("string", "", default="string", multi=True)
@ -145,7 +145,7 @@ def test_multi_with_requires_with_disabled_in_another_group():
def test_multi_with_requires_that_is_multi(): def test_multi_with_requires_that_is_multi():
s = StrOption("string", "", default="string", multi=True) s = StrOption("string", "", default="string", multi=True)
intoption = IntOption('int', 'Test int option', default=[0, 0], multi=True) intoption = IntOption('int', 'Test int option', default=0, multi=True)
stroption = StrOption('str', 'Test string option', default="abc", stroption = StrOption('str', 'Test string option', default="abc",
requires=[('int', [1, 1], 'hide')], multi=True) requires=[('int', [1, 1], 'hide')], multi=True)
descr = OptionDescription("options", "", [s, intoption, stroption]) descr = OptionDescription("options", "", [s, intoption, stroption])
@ -156,7 +156,7 @@ def test_multi_with_requires_that_is_multi():
assert stroption._is_hidden() assert stroption._is_hidden()
def test_multi_with_bool(): def test_multi_with_bool():
s = BoolOption("bool", "", default=[False], multi=True) s = BoolOption("bool", "", default=False, multi=True)
descr = OptionDescription("options", "", [s]) descr = OptionDescription("options", "", [s])
config = Config(descr) config = Config(descr)
assert descr.bool.multi == True assert descr.bool.multi == True
@ -165,14 +165,14 @@ def test_multi_with_bool():
assert config.bool == [True, False] assert config.bool == [True, False]
def test_multi_with_bool_two(): def test_multi_with_bool_two():
s = BoolOption("bool", "", default=[False], multi=True) s = BoolOption("bool", "", default=False, multi=True)
descr = OptionDescription("options", "", [s]) descr = OptionDescription("options", "", [s])
config = Config(descr) config = Config(descr)
assert descr.bool.multi == True assert descr.bool.multi == True
raises(ConfigError, "config.bool = True") raises(ConfigError, "config.bool = True")
def test_choice_access_with_multi(): def test_choice_access_with_multi():
ch = ChoiceOption("t1", "", ["a", "b"], default=["a", "a", "a"], multi=True) ch = ChoiceOption("t1", "", ["a", "b"], default="a", multi=True)
descr = OptionDescription("options", "", [ch]) descr = OptionDescription("options", "", [ch])
config = Config(descr) config = Config(descr)
config.t1 = ["a", "b", "a", "b"] config.t1 = ["a", "b", "a", "b"]