From bd9d98fcc62d5ee6b0572da4371f6103c465dd17 Mon Sep 17 00:00:00 2001 From: gwen Date: Tue, 10 Jul 2012 16:46:30 +0200 Subject: [PATCH] None and [] are both possible --- config.py | 21 +++++++-------------- option.py | 3 +-- test/test_option_setting.py | 23 +++++++++++++++++++++-- 3 files changed, 29 insertions(+), 18 deletions(-) diff --git a/config.py b/config.py index dab6dfc..4fcfa5d 100644 --- a/config.py +++ b/config.py @@ -17,7 +17,7 @@ # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # # The original `Config` design model is unproudly borrowed from -# the rough gus of pypy: pypy: http://codespeak.net/svn/pypy/dist/pypy/config/ +# the rough pypy's guys: http://codespeak.net/svn/pypy/dist/pypy/config/ # the whole pypy projet is under MIT licence # ____________________________________________________________ from error import (HiddenOptionError, ConfigError, NotFoundError, @@ -154,14 +154,7 @@ class Config(object): if type(getattr(self._cfgimpl_descr, name)) != SymLinkOption: 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) @@ -325,11 +318,11 @@ class Config(object): if who == 'auto': if not child._is_hidden(): child.hide() - 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') + if (value is None and who != 'default' and not child.is_multi()): + child.setowner(self, 'default') + self._cfgimpl_values[name] = child.getdefault() + elif (value == [] and who != 'default' and child.is_multi()): + child.setowner(self, ['default' for i in range(len(child.getdefault()))]) self._cfgimpl_values[name] = child.getdefault() else: child.setowner(self, newowner) diff --git a/option.py b/option.py index b9b41e5..b96dccb 100644 --- a/option.py +++ b/option.py @@ -17,7 +17,7 @@ # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # # The original `Config` design model is unproudly borrowed from -# the rough gus of pypy: pypy: http://codespeak.net/svn/pypy/dist/pypy/config/ +# the rough pypy's guys: http://codespeak.net/svn/pypy/dist/pypy/config/ # the whole pypy projet is under MIT licence # ____________________________________________________________ from autolib import special_owners @@ -80,7 +80,6 @@ class Option(HiddenBaseType, DisabledBaseType, ModeBaseType): if val != None: # None allows the reset of the value return self._validate(val) - return True def getdefault(self): diff --git a/test/test_option_setting.py b/test/test_option_setting.py index ec39fad..1858083 100644 --- a/test/test_option_setting.py +++ b/test/test_option_setting.py @@ -25,7 +25,6 @@ def make_description(): wantframework_option, intoption, boolop]) return descr - #____________________________________________________________ # change with __setattr__ def test_attribute_access(): @@ -36,7 +35,13 @@ def test_attribute_access(): # let's try to change it again config.string = "foo" assert config.string == "foo" -# raises(ConflictConfigError, 'config.string = "bar"') + +def test_setitem(): + s = StrOption("string", "", default=["string"], multi=True) + descr = OptionDescription("options", "", [s]) + config = Config(descr) + config.string = ["foo", "eggs"] + def test_reset(): "if value is None, resets to default owner" @@ -50,6 +55,20 @@ def test_reset(): assert config.string == 'string' assert config._cfgimpl_value_owners['string'] == 'default' +def test_reset_with_multi(): + s = StrOption("string", "", default=["string"], default_multi="string" , multi=True) + descr = OptionDescription("options", "", [s]) + config = Config(descr) + config.string = [] + assert config.string == ["string"] + assert config._cfgimpl_value_owners['string'] == ['default'] + config.string = ["eggs", "spam", "foo"] + assert config._cfgimpl_value_owners['string'] == ['user', 'user', 'user'] + config.string = [] + assert config.string == ["string"] + assert config._cfgimpl_value_owners['string'] == ['default'] + raises(ConfigError, "config.string = None") + def test_idontexist(): descr = make_description() cfg = Config(descr)