From b2e97573bd4d41a0a601faac094dc3e931c8e3c6 Mon Sep 17 00:00:00 2001 From: gwen Date: Wed, 19 Sep 2012 09:31:02 +0200 Subject: [PATCH] tests on frozen and None value --- test/test_option_owner.py | 4 ++- test/test_option_type.py | 5 ++- tiramisu/config.py | 26 +++----------- tiramisu/option.py | 72 +++++---------------------------------- 4 files changed, 21 insertions(+), 86 deletions(-) diff --git a/test/test_option_owner.py b/test/test_option_owner.py index bf29f92..247bc21 100644 --- a/test/test_option_owner.py +++ b/test/test_option_owner.py @@ -72,13 +72,15 @@ def test_has_callback(): config = Config(descr, bool=False) # because dummy has a callback dummy = config.unwrap_from_path('gc.dummy') + config.cfgimpl_freeze() dummy.freeze() raises(TypeError, "config.gc.dummy = True") #____________________________________________________________ -def test_has_callback_with_setoption(): +def test_freeze_and_has_callback_with_setoption(): descr = make_description() config = Config(descr, bool=False) + config.cfgimpl_freeze() dummy = config.unwrap_from_path('gc.dummy') dummy.freeze() raises(TypeError, "config.gc.setoption('dummy', True, 'gen_config')") diff --git a/test/test_option_type.py b/test/test_option_type.py index 6a67b50..d19920f 100644 --- a/test/test_option_type.py +++ b/test/test_option_type.py @@ -74,6 +74,7 @@ def test_frozen_value(): s = StrOption("string", "", default="string") descr = OptionDescription("options", "", [s]) config = Config(descr) + config.cfgimpl_freeze() s.freeze() raises(TypeError, 'config.string = "egg"') @@ -82,7 +83,9 @@ def test_freeze(): descr = make_description() conf = Config(descr) conf.cfgimpl_freeze() - raises(TypeError, "conf.gc.name = 'try to modify'") + name = conf.unwrap_from_path("gc.name") + name.freeze() + raises(TypeError, "conf.gc.name = 'framework'") # ____________________________________________________________ def test_is_hidden(): descr = make_description() diff --git a/tiramisu/config.py b/tiramisu/config.py index 1a749e9..b98b33f 100644 --- a/tiramisu/config.py +++ b/tiramisu/config.py @@ -27,17 +27,15 @@ from tiramisu.error import (PropertiesOptionError, ConfigError, NotFoundError, from tiramisu.option import (OptionDescription, Option, SymLinkOption, group_types, Multi, apply_requires) from tiramisu.autolib import carry_out_calculation -import traceback # ______________________________________________________________________ # generic owner. 'default' is the general config owner after init time default_owner = 'user' -DEBUG = False # ____________________________________________________________ class Config(object): _cfgimpl_properties = ['hidden', 'disabled'] _cfgimpl_mandatory = True - _cfgimpl_frozen = False + _cfgimpl_frozen = True _cfgimpl_owner = default_owner _cfgimpl_toplevel = None # TODO implement unicity by name @@ -167,20 +165,12 @@ class Config(object): # ____________________________________________________________ def __setattr__(self, name, value): - if '.' in name: - homeconfig, name = self._cfgimpl_get_home_by_path(name) - return setattr(homeconfig, name, value) - if name.startswith('_cfgimpl_'): self.__dict__[name] = value return - if self.is_frozen() and getattr(self, name) != value: - raise TypeError("trying to change a value in a frozen config" - ": {0} {1}".format(name, value)) -# if self.is_mandatory() and value == None: -# raise MandatoryError("trying to reset option: {0} wich lives in a" -# " mandatory group: {1}".format(name, -# self._cfgimpl_descr._name)) + if '.' in name: + homeconfig, name = self._cfgimpl_get_home_by_path(name) + return setattr(homeconfig, name, value) if type(getattr(self._cfgimpl_descr, name)) != SymLinkOption: self._validate(name, getattr(self._cfgimpl_descr, name)) self.setoption(name, value, self._cfgimpl_owner) @@ -189,8 +179,6 @@ class Config(object): apply_requires(opt_or_descr, self) if not isinstance(opt_or_descr, Option) and \ not isinstance(opt_or_descr, OptionDescription): - if DEBUG: - traceback.print_exc() raise TypeError('Unexpected object: {0}'.format(repr(opt_or_descr))) properties = opt_or_descr.properties for proper in properties: @@ -340,10 +328,6 @@ class Config(object): if child.has_callback() and who=='default': raise TypeError("trying to set a value to an option " "wich has a callback: {0}".format(name)) -# if oldowner == who: -# oldvalue = getattr(self, name) -# if oldvalue == value: -# return child.setoption(self, value, who) if (value is None and who != 'default' and not child.is_multi()): child.setowner(self, 'default') @@ -484,7 +468,7 @@ class Config(object): try: yield child._name, getattr(self, child._name) except: - pass # hidden, disabled option group + pass # option with properties def iter_groups(self, group_type=None): "iteration on OptionDescriptions" diff --git a/tiramisu/option.py b/tiramisu/option.py index bb1dbaf..277bc2b 100644 --- a/tiramisu/option.py +++ b/tiramisu/option.py @@ -157,9 +157,6 @@ class Option(HiddenBaseType, DisabledBaseType): def is_forced_on_freeze(self): return self._frozen and self._force_default_on_freeze - def is_frozen(self): - return self._frozen - def getdoc(self): return self.doc @@ -179,12 +176,10 @@ class Option(HiddenBaseType, DisabledBaseType): # config *must* be only the **parent** config (not the toplevel config) # owner is a **real* owner, a list is actually allowable here name = self._name - if self.is_frozen() and config.is_frozen(): - raise TypeError("trying to change a frozen option's owner: %s" % name) if self.is_multi(): if not type(owner) == list: raise ConfigError("invalid owner for multi " - "option: {0}".format(self._name)) + "option: {0}".format(name)) config._cfgimpl_value_owners[name] = owner def getowner(self, config): @@ -194,11 +189,6 @@ class Option(HiddenBaseType, DisabledBaseType): def setoption(self, config, value, who): "who is **not necessarily** a owner because it cannot be a list" name = self._name - - # we want the possibility to reset everything - if not (who == "default" and value is None) and not self.validate(value): - self.default = None - return if not self.validate(value): raise ConfigError('invalid value %s for option %s' % (value, name)) if self.is_mandatory(): @@ -210,13 +200,14 @@ class Option(HiddenBaseType, DisabledBaseType): value = Multi([{'': None}.get(i, i) for i in value], config, self) if config.is_mandatory() and ((self.is_multi() and value == []) or \ (not self.is_multi() and value is None)): - raise MandatoryError('cannot override value to %s for ' + raise MandatoryError('cannot change the value to %s for ' 'option %s' % (value, name)) if name not in config._cfgimpl_values: raise AttributeError('unknown option %s' % (name)) - if config.is_frozen() and self.isfrozen(): - raise ConflictConfigError('cannot override value to %s for ' - 'option %s' % (value, name)) + + if config.is_frozen() and self.is_frozen(): + raise TypeError('cannot change the value to %s for ' + 'option %s' % (str(value), name)) if who == "default": # changes the default value (and therefore resets the previous value) if self._validate(value): @@ -280,10 +271,6 @@ class ChoiceOption(Option): callback=callback, callback_params=callback_params, requires=requires, multi=multi, mandatory=mandatory) - def setoption(self, config, value, who): - name = self._name - super(ChoiceOption, self).setoption(config, value, who) - def _validate(self, value): if not self.open_values: return value is None or value in self.values @@ -308,33 +295,13 @@ class IntOption(Option): opt_type = 'int' def _validate(self, value): - try: - int(value) - except TypeError: - return False - return True - - def setoption(self, config, value, who): - try: - super(IntOption, self).setoption(config, value, who) - except TypeError, e: - raise ConfigError(*e.args) + return isinstance(value, int) class FloatOption(Option): opt_type = 'float' def _validate(self, value): - try: - float(value) - except TypeError: - return False - return True - - def setoption(self, config, value, who): - try: - super(FloatOption, self).setoption(config, float(value), who) - except TypeError, e: - raise ConfigError(*e.args) + return isinstance(value, float) class StrOption(Option): opt_type = 'string' @@ -342,12 +309,6 @@ class StrOption(Option): def _validate(self, value): return isinstance(value, str) - def setoption(self, config, value, who): - try: - super(StrOption, self).setoption(config, value, who) - except TypeError, e: - raise ConfigError(*e.args) - class SymLinkOption(object): opt_type = 'symlink' @@ -356,10 +317,7 @@ class SymLinkOption(object): self.path = path def setoption(self, config, value, who): - try: - setattr(config, self.path, value) # .setoption(self.path, value, who) - except TypeError, e: - raise ConfigError(*e.args) + setattr(config, self.path, value) # .setoption(self.path, value, who) class IPOption(Option): opt_type = 'ip' @@ -368,12 +326,6 @@ class IPOption(Option): # by now the validation is nothing but a string, use IPy instead return isinstance(value, str) - def setoption(self, config, value, who): - try: - super(IPOption, self).setoption(config, value, who) - except TypeError, e: - raise ConfigError(*e.args) - class NetmaskOption(Option): opt_type = 'netmask' @@ -381,12 +333,6 @@ class NetmaskOption(Option): # by now the validation is nothing but a string, use IPy instead return isinstance(value, str) - def setoption(self, config, value, who): - try: - super(NetmaskOption, self).setoption(config, value, who) - except TypeError, e: - raise ConfigError(*e.args) - class ArbitraryOption(Option): def __init__(self, name, doc, default=None, defaultfactory=None, requires=None, multi=False, mandatory=False):