tests on frozen and None value

This commit is contained in:
gwen 2012-09-19 09:31:02 +02:00
parent f50935523f
commit b2e97573bd
4 changed files with 21 additions and 86 deletions

View File

@ -72,13 +72,15 @@ def test_has_callback():
config = Config(descr, bool=False) config = Config(descr, bool=False)
# because dummy has a callback # because dummy has a callback
dummy = config.unwrap_from_path('gc.dummy') dummy = config.unwrap_from_path('gc.dummy')
config.cfgimpl_freeze()
dummy.freeze() dummy.freeze()
raises(TypeError, "config.gc.dummy = True") raises(TypeError, "config.gc.dummy = True")
#____________________________________________________________ #____________________________________________________________
def test_has_callback_with_setoption(): def test_freeze_and_has_callback_with_setoption():
descr = make_description() descr = make_description()
config = Config(descr, bool=False) config = Config(descr, bool=False)
config.cfgimpl_freeze()
dummy = config.unwrap_from_path('gc.dummy') dummy = config.unwrap_from_path('gc.dummy')
dummy.freeze() dummy.freeze()
raises(TypeError, "config.gc.setoption('dummy', True, 'gen_config')") raises(TypeError, "config.gc.setoption('dummy', True, 'gen_config')")

View File

@ -74,6 +74,7 @@ def test_frozen_value():
s = StrOption("string", "", default="string") s = StrOption("string", "", default="string")
descr = OptionDescription("options", "", [s]) descr = OptionDescription("options", "", [s])
config = Config(descr) config = Config(descr)
config.cfgimpl_freeze()
s.freeze() s.freeze()
raises(TypeError, 'config.string = "egg"') raises(TypeError, 'config.string = "egg"')
@ -82,7 +83,9 @@ def test_freeze():
descr = make_description() descr = make_description()
conf = Config(descr) conf = Config(descr)
conf.cfgimpl_freeze() 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(): def test_is_hidden():
descr = make_description() descr = make_description()

View File

@ -27,17 +27,15 @@ from tiramisu.error import (PropertiesOptionError, ConfigError, NotFoundError,
from tiramisu.option import (OptionDescription, Option, SymLinkOption, from tiramisu.option import (OptionDescription, Option, SymLinkOption,
group_types, Multi, apply_requires) group_types, Multi, apply_requires)
from tiramisu.autolib import carry_out_calculation from tiramisu.autolib import carry_out_calculation
import traceback
# ______________________________________________________________________ # ______________________________________________________________________
# generic owner. 'default' is the general config owner after init time # generic owner. 'default' is the general config owner after init time
default_owner = 'user' default_owner = 'user'
DEBUG = False
# ____________________________________________________________ # ____________________________________________________________
class Config(object): class Config(object):
_cfgimpl_properties = ['hidden', 'disabled'] _cfgimpl_properties = ['hidden', 'disabled']
_cfgimpl_mandatory = True _cfgimpl_mandatory = True
_cfgimpl_frozen = False _cfgimpl_frozen = True
_cfgimpl_owner = default_owner _cfgimpl_owner = default_owner
_cfgimpl_toplevel = None _cfgimpl_toplevel = None
# TODO implement unicity by name # TODO implement unicity by name
@ -167,20 +165,12 @@ class Config(object):
# ____________________________________________________________ # ____________________________________________________________
def __setattr__(self, name, value): 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_'): if name.startswith('_cfgimpl_'):
self.__dict__[name] = value self.__dict__[name] = value
return return
if self.is_frozen() and getattr(self, name) != value: if '.' in name:
raise TypeError("trying to change a value in a frozen config" homeconfig, name = self._cfgimpl_get_home_by_path(name)
": {0} {1}".format(name, value)) return setattr(homeconfig, 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 type(getattr(self._cfgimpl_descr, name)) != SymLinkOption: if type(getattr(self._cfgimpl_descr, name)) != SymLinkOption:
self._validate(name, getattr(self._cfgimpl_descr, name)) self._validate(name, getattr(self._cfgimpl_descr, name))
self.setoption(name, value, self._cfgimpl_owner) self.setoption(name, value, self._cfgimpl_owner)
@ -189,8 +179,6 @@ class Config(object):
apply_requires(opt_or_descr, self) apply_requires(opt_or_descr, self)
if not isinstance(opt_or_descr, Option) and \ if not isinstance(opt_or_descr, Option) and \
not isinstance(opt_or_descr, OptionDescription): not isinstance(opt_or_descr, OptionDescription):
if DEBUG:
traceback.print_exc()
raise TypeError('Unexpected object: {0}'.format(repr(opt_or_descr))) raise TypeError('Unexpected object: {0}'.format(repr(opt_or_descr)))
properties = opt_or_descr.properties properties = opt_or_descr.properties
for proper in properties: for proper in properties:
@ -340,10 +328,6 @@ class Config(object):
if child.has_callback() and who=='default': if child.has_callback() and who=='default':
raise TypeError("trying to set a value to an option " raise TypeError("trying to set a value to an option "
"wich has a callback: {0}".format(name)) "wich has a callback: {0}".format(name))
# if oldowner == who:
# oldvalue = getattr(self, name)
# if oldvalue == value:
# return
child.setoption(self, value, who) child.setoption(self, value, who)
if (value is None and who != 'default' and not child.is_multi()): if (value is None and who != 'default' and not child.is_multi()):
child.setowner(self, 'default') child.setowner(self, 'default')
@ -484,7 +468,7 @@ class Config(object):
try: try:
yield child._name, getattr(self, child._name) yield child._name, getattr(self, child._name)
except: except:
pass # hidden, disabled option group pass # option with properties
def iter_groups(self, group_type=None): def iter_groups(self, group_type=None):
"iteration on OptionDescriptions" "iteration on OptionDescriptions"

View File

@ -157,9 +157,6 @@ class Option(HiddenBaseType, DisabledBaseType):
def is_forced_on_freeze(self): def is_forced_on_freeze(self):
return self._frozen and self._force_default_on_freeze return self._frozen and self._force_default_on_freeze
def is_frozen(self):
return self._frozen
def getdoc(self): def getdoc(self):
return self.doc return self.doc
@ -179,12 +176,10 @@ class Option(HiddenBaseType, DisabledBaseType):
# config *must* be only the **parent** config (not the toplevel config) # config *must* be only the **parent** config (not the toplevel config)
# owner is a **real* owner, a list is actually allowable here # owner is a **real* owner, a list is actually allowable here
name = self._name 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 self.is_multi():
if not type(owner) == list: if not type(owner) == list:
raise ConfigError("invalid owner for multi " raise ConfigError("invalid owner for multi "
"option: {0}".format(self._name)) "option: {0}".format(name))
config._cfgimpl_value_owners[name] = owner config._cfgimpl_value_owners[name] = owner
def getowner(self, config): def getowner(self, config):
@ -194,11 +189,6 @@ class Option(HiddenBaseType, DisabledBaseType):
def setoption(self, config, value, who): def setoption(self, config, value, who):
"who is **not necessarily** a owner because it cannot be a list" "who is **not necessarily** a owner because it cannot be a list"
name = self._name 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): 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 self.is_mandatory(): if self.is_mandatory():
@ -210,13 +200,14 @@ class Option(HiddenBaseType, DisabledBaseType):
value = Multi([{'': None}.get(i, i) for i in value], config, self) value = Multi([{'': None}.get(i, i) for i in value], config, self)
if config.is_mandatory() and ((self.is_multi() and value == []) or \ if config.is_mandatory() and ((self.is_multi() and value == []) or \
(not self.is_multi() and value is None)): (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)) 'option %s' % (value, name))
if name not in config._cfgimpl_values: if name not in config._cfgimpl_values:
raise AttributeError('unknown option %s' % (name)) raise AttributeError('unknown option %s' % (name))
if config.is_frozen() and self.isfrozen():
raise ConflictConfigError('cannot override value to %s for ' if config.is_frozen() and self.is_frozen():
'option %s' % (value, name)) raise TypeError('cannot change the value to %s for '
'option %s' % (str(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)
if self._validate(value): if self._validate(value):
@ -280,10 +271,6 @@ class ChoiceOption(Option):
callback=callback, callback_params=callback_params, callback=callback, callback_params=callback_params,
requires=requires, multi=multi, mandatory=mandatory) 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): def _validate(self, value):
if not self.open_values: if not self.open_values:
return value is None or value in self.values return value is None or value in self.values
@ -308,33 +295,13 @@ class IntOption(Option):
opt_type = 'int' opt_type = 'int'
def _validate(self, value): def _validate(self, value):
try: return isinstance(value, int)
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)
class FloatOption(Option): class FloatOption(Option):
opt_type = 'float' opt_type = 'float'
def _validate(self, value): def _validate(self, value):
try: return isinstance(value, float)
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)
class StrOption(Option): class StrOption(Option):
opt_type = 'string' opt_type = 'string'
@ -342,12 +309,6 @@ class StrOption(Option):
def _validate(self, value): def _validate(self, value):
return isinstance(value, str) 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): class SymLinkOption(object):
opt_type = 'symlink' opt_type = 'symlink'
@ -356,10 +317,7 @@ class SymLinkOption(object):
self.path = path self.path = path
def setoption(self, config, value, who): def setoption(self, config, value, who):
try: setattr(config, self.path, value) # .setoption(self.path, value, who)
setattr(config, self.path, value) # .setoption(self.path, value, who)
except TypeError, e:
raise ConfigError(*e.args)
class IPOption(Option): class IPOption(Option):
opt_type = 'ip' opt_type = 'ip'
@ -368,12 +326,6 @@ class IPOption(Option):
# by now the validation is nothing but a string, use IPy instead # by now the validation is nothing but a string, use IPy instead
return isinstance(value, str) 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): class NetmaskOption(Option):
opt_type = 'netmask' opt_type = 'netmask'
@ -381,12 +333,6 @@ class NetmaskOption(Option):
# by now the validation is nothing but a string, use IPy instead # by now the validation is nothing but a string, use IPy instead
return isinstance(value, str) 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): class ArbitraryOption(Option):
def __init__(self, name, doc, default=None, defaultfactory=None, def __init__(self, name, doc, default=None, defaultfactory=None,
requires=None, multi=False, mandatory=False): requires=None, multi=False, mandatory=False):