default value if option is set to None

This commit is contained in:
gwen 2012-07-10 14:36:09 +02:00
parent 4106440cf4
commit 166ffc72f7
3 changed files with 37 additions and 19 deletions

View file

@ -42,7 +42,7 @@ class Config(object):
self._cfgimpl_descr = descr
self._cfgimpl_value_owners = {}
self._cfgimpl_parent = parent
# `Config()` indeed supports the configuration `Option()`'s values...
# `Config()` indeed takes care of the `Option()`'s values
self._cfgimpl_values = {}
self._cfgimpl_previous_values = {}
# XXX warnings are a great idea, let's make up a better use of it
@ -50,7 +50,6 @@ class Config(object):
self._cfgimpl_toplevel = self._cfgimpl_get_toplevel()
# `freeze()` allows us to carry out this calculation again if necessary
self._cfgimpl_frozen = self._cfgimpl_toplevel._cfgimpl_frozen
#
self._cfgimpl_build(overrides)
def _validate_duplicates(self, children):
@ -59,14 +58,15 @@ class Config(object):
if dup._name not in duplicates:
duplicates.append(dup._name)
else:
raise ConflictConfigError('duplicate option name: <%s>' % \
dup._name)
raise ConflictConfigError('duplicate option name: '
'{0}'.format(dup._name))
def _cfgimpl_build(self, overrides):
self._validate_duplicates(self._cfgimpl_descr._children)
for child in self._cfgimpl_descr._children:
if isinstance(child, Option):
self._cfgimpl_values[child._name] = child.getdefault()
self._cfgimpl_previous_values[child._name] = child.getdefault()
if child.getcallback() is not None:
if child._is_hidden():
self._cfgimpl_value_owners[child._name] = 'auto'
@ -91,7 +91,7 @@ class Config(object):
if isinstance(child, Option):
if child._name not in self._cfgimpl_values:
self._cfgimpl_values[child._name] = child.getdefault()
# FIXME and ['default'] if is_multi
# FIXME and ['default', ...] if is_multi() ?
self._cfgimpl_value_owners[child._name] = 'default'
elif isinstance(child, OptionDescription):
if child._name not in self._cfgimpl_values:
@ -155,6 +155,13 @@ class Config(object):
self._validate(name, getattr(self._cfgimpl_descr, name))
self.setoption(name, value, self._cfgimpl_owner)
# def __setitem__(self, key, value):
# print "entering __setitem__"
# if '.' in name:
# homeconfig, name = self._cfgimpl_get_home_by_path(name)
# return "hello" #setattr(homeconfig, name, value)
# return "titi"
def _validate(self, name, opt_or_descr):
if not type(opt_or_descr) == OptionDescription:
apply_requires(opt_or_descr, self)
@ -318,7 +325,13 @@ class Config(object):
if who == 'auto':
if not child._is_hidden():
child.hide()
self._cfgimpl_value_owners[name] = newowner
if value is None and who != 'default':
if child.is_multi():
child.setowner(self, ['default' for i in range(len(child.getdefault()))])
else:
child.setowner(self, 'default')
else:
child.setowner(self, newowner)
else:
homeconfig = self._cfgimpl_get_toplevel()
child.setoption(homeconfig, value, who)
@ -377,7 +390,7 @@ class Config(object):
return home._cfgimpl_previous_values[name]
def get_previous_value(self, name):
return home._cfgimpl_previous_values[name]
return self._cfgimpl_previous_values[name]
def add_warning(self, warning):
self._cfgimpl_get_toplevel()._cfgimpl_warnings.append(warning)

View file

@ -23,7 +23,7 @@
from autolib import special_owners
from basetype import HiddenBaseType, DisabledBaseType, ModeBaseType, modes
from error import (ConfigError, ConflictConfigError, NotFoundError,
RequiresError)
SpecialOwnersError, RequiresError)
available_actions = ['hide', 'show', 'enable', 'disable']
reverse_actions = {'hide': 'show', 'show': 'hide',
'disable':'enable', 'enable': 'disable'}
@ -116,17 +116,11 @@ class Option(HiddenBaseType, DisabledBaseType, ModeBaseType):
if self._frozen:
raise TypeError('trying to change a frozen option object: %s' % name)
# we want the possibility to reset everything
# if (not self.multi and who == "default" or self.multi and "default" in who)\
# and value is None:
if who == "default" and value is None:
self.default = None
return
if not self.validate(value):
raise ConfigError('invalid value %s for option %s' % (value, name))
# if (self.multi and "default" in who) or \
# (not self.multi and who == "default"):
if who == "default":
# changes the default value (and therefore resets the previous value)
if self._validate(value):

View file

@ -38,6 +38,18 @@ def test_attribute_access():
assert config.string == "foo"
# raises(ConflictConfigError, 'config.string = "bar"')
def test_reset():
"if value is None, resets to default owner"
s = StrOption("string", "", default="string")
descr = OptionDescription("options", "", [s])
config = Config(descr)
config.string = "foo"
assert config.string == "foo"
assert config._cfgimpl_value_owners['string'] == 'user'
config.string = None
assert config.string == None
assert config._cfgimpl_value_owners['string'] == 'default'
def test_idontexist():
descr = make_description()
cfg = Config(descr)
@ -56,8 +68,10 @@ def test_item_access_with_multi():
config = Config(descr)
config.string = ["foo", "bar"]
assert config.string == ["foo", "bar"]
assert config.string[0] == "foo"
# FIXME
config.string[0] = 'changetest'
assert config.string[0] == 'changetest'
# assert config.string[0] == 'changetest'
# assert config.string[
def test_access_with_multi_default():
@ -68,9 +82,6 @@ def test_access_with_multi_default():
config.string = ["foo", "bar"]
assert config.string == ["foo", "bar"]
assert config._cfgimpl_value_owners["string"] == ['user', 'user']
print config._cfgimpl_value_owners["string"]
# FIXME gwen haha
# assert config.string[
#def test_attribute_access_with_multi2():
# s = StrOption("string", "", default="string", multi=True)