From f3a9a9671425d88a653727d3f2ff101b4fc91563 Mon Sep 17 00:00:00 2001 From: gwen Date: Fri, 29 Jun 2012 11:48:03 +0200 Subject: [PATCH] possibility to have a default as multi --- option.py | 125 ++++++++++++++---------------------- test/test_option_setting.py | 20 +++--- 2 files changed, 57 insertions(+), 88 deletions(-) diff --git a/option.py b/option.py index bbc471d..88f2537 100644 --- a/option.py +++ b/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): diff --git a/test/test_option_setting.py b/test/test_option_setting.py index dad5e20..c7b7459 100644 --- a/test/test_option_setting.py +++ b/test/test_option_setting.py @@ -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"]