possibility to have a default as multi
This commit is contained in:
parent
97b000bf1d
commit
f3a9a96714
125
option.py
125
option.py
@ -46,13 +46,31 @@ class Option(HiddenBaseType, DisabledBaseType, ModeBaseType):
|
||||
raise ConfigError("mode {0} not available".format(mode))
|
||||
self.mode = mode
|
||||
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} "
|
||||
"for option {1}".format(default, name))
|
||||
self.default = default
|
||||
|
||||
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):
|
||||
return self.default
|
||||
@ -81,8 +99,10 @@ class Option(HiddenBaseType, DisabledBaseType, ModeBaseType):
|
||||
if who == "default" and value is None:
|
||||
self.default = None
|
||||
return
|
||||
if value != None and not self.validate(value):
|
||||
|
||||
if not self.validate(value):
|
||||
raise ConfigError('invalid value %s for option %s' % (value, name))
|
||||
|
||||
if who == "default":
|
||||
# changes the default value (and therefore resets the previous value)
|
||||
self.default = value
|
||||
@ -135,14 +155,8 @@ class ChoiceOption(Option):
|
||||
name = self._name
|
||||
super(ChoiceOption, self).setoption(config, value, who)
|
||||
|
||||
def validate(self, value):
|
||||
if self.multi == False:
|
||||
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
|
||||
def _validate(self, value):
|
||||
return value is None or value in self.values
|
||||
|
||||
class BoolOption(Option):
|
||||
opt_type = 'bool'
|
||||
@ -155,17 +169,9 @@ class BoolOption(Option):
|
||||
# requires=requires, multi=multi, mandatory=mandatory)
|
||||
#self._validator = validator
|
||||
|
||||
def validate(self, value):
|
||||
if self.multi == False:
|
||||
return isinstance(value, bool)
|
||||
else:
|
||||
try:
|
||||
for val in value:
|
||||
if not isinstance(val, bool):
|
||||
return False
|
||||
except Exception:
|
||||
return False
|
||||
return True
|
||||
def _validate(self, value):
|
||||
return isinstance(value, bool)
|
||||
|
||||
# FIXME config level validator
|
||||
# def setoption(self, config, value, who):
|
||||
# name = self._name
|
||||
@ -180,20 +186,12 @@ class IntOption(Option):
|
||||
def __init__(self, *args, **kwargs):
|
||||
super(IntOption, self).__init__(*args, **kwargs)
|
||||
|
||||
def validate(self, value):
|
||||
if self.multi == False:
|
||||
try:
|
||||
int(value)
|
||||
except TypeError:
|
||||
return False
|
||||
return True
|
||||
else:
|
||||
for val in value:
|
||||
try:
|
||||
int(val)
|
||||
except TypeError:
|
||||
return False
|
||||
return True
|
||||
def _validate(self, value):
|
||||
try:
|
||||
int(value)
|
||||
except TypeError:
|
||||
return False
|
||||
return True
|
||||
|
||||
def setoption(self, config, value, who):
|
||||
try:
|
||||
@ -207,20 +205,12 @@ class FloatOption(Option):
|
||||
def __init__(self, *args, **kwargs):
|
||||
super(FloatOption, self).__init__(*args, **kwargs)
|
||||
|
||||
def validate(self, value):
|
||||
if self.multi == False:
|
||||
try:
|
||||
float(value)
|
||||
except TypeError:
|
||||
return False
|
||||
return True
|
||||
else:
|
||||
for val in value:
|
||||
try:
|
||||
float(val)
|
||||
except TypeError:
|
||||
return False
|
||||
return True
|
||||
def _validate(self, value):
|
||||
try:
|
||||
float(value)
|
||||
except TypeError:
|
||||
return False
|
||||
return True
|
||||
|
||||
def setoption(self, config, value, who):
|
||||
try:
|
||||
@ -234,15 +224,8 @@ class StrOption(Option):
|
||||
def __init__(self, *args, **kwargs):
|
||||
super(StrOption, self).__init__(*args, **kwargs)
|
||||
|
||||
def validate(self, value):
|
||||
if self.multi == False:
|
||||
return isinstance(value, str)
|
||||
else:
|
||||
for val in value:
|
||||
if not isinstance(val, str):
|
||||
return False
|
||||
else:
|
||||
return True
|
||||
def _validate(self, value):
|
||||
return isinstance(value, str)
|
||||
|
||||
def setoption(self, config, value, who):
|
||||
try:
|
||||
@ -269,16 +252,9 @@ class IPOption(Option):
|
||||
def __init__(self, *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
|
||||
if self.multi == False:
|
||||
return isinstance(value, str)
|
||||
else:
|
||||
for val in value:
|
||||
if not isinstance(val, str):
|
||||
return False
|
||||
else:
|
||||
return True
|
||||
return isinstance(value, str)
|
||||
|
||||
def setoption(self, config, value, who):
|
||||
try:
|
||||
@ -292,16 +268,9 @@ class NetmaskOption(Option):
|
||||
def __init__(self, *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
|
||||
if self.multi == False:
|
||||
return isinstance(value, str)
|
||||
else:
|
||||
for val in value:
|
||||
if not isinstance(val, str):
|
||||
return False
|
||||
else:
|
||||
return True
|
||||
return isinstance(value, str)
|
||||
|
||||
def setoption(self, config, value, who):
|
||||
try:
|
||||
@ -318,7 +287,7 @@ class ArbitraryOption(Option):
|
||||
if defaultfactory is not None:
|
||||
assert default is None
|
||||
|
||||
def validate(self, value):
|
||||
def _validate(self, value):
|
||||
return True
|
||||
|
||||
def getdefault(self):
|
||||
|
@ -50,12 +50,12 @@ def test_attribute_access_with_multi():
|
||||
config.string = ["foo", "bar"]
|
||||
assert config.string == ["foo", "bar"]
|
||||
|
||||
def test_attribute_access_with_multi():
|
||||
s = StrOption("string", "", default="string", multi=True)
|
||||
descr = OptionDescription("options", "", [s])
|
||||
config = Config(descr)
|
||||
config.string = ["foo", "bar"]
|
||||
assert config.string == ["foo", "bar"]
|
||||
#def test_attribute_access_with_multi2():
|
||||
# s = StrOption("string", "", default="string", multi=True)
|
||||
# descr = OptionDescription("options", "", [s])
|
||||
# config = Config(descr)
|
||||
# config.string = ["foo", "bar"]
|
||||
# assert config.string == ["foo", "bar"]
|
||||
|
||||
def test_multi_with_requires():
|
||||
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():
|
||||
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",
|
||||
requires=[('int', [1, 1], 'hide')], multi=True)
|
||||
descr = OptionDescription("options", "", [s, intoption, stroption])
|
||||
@ -156,7 +156,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
|
||||
@ -165,14 +165,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", "a", "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"]
|
||||
|
Loading…
Reference in New Issue
Block a user