From cda5d8c0bd1b1edc8b382d9464546fd835aeae10 Mon Sep 17 00:00:00 2001 From: gwen Date: Tue, 11 Dec 2012 11:18:53 +0100 Subject: [PATCH] generic owners shall live in settings --- tiramisu/config.py | 4 ++-- tiramisu/option.py | 15 +++++++++------ tiramisu/setting.py | 12 ++++++++++-- 3 files changed, 21 insertions(+), 10 deletions(-) diff --git a/tiramisu/config.py b/tiramisu/config.py index ff8b9cc..548ef94 100644 --- a/tiramisu/config.py +++ b/tiramisu/config.py @@ -117,7 +117,7 @@ class Config(object): 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, owners.user) + self.setoption(name, value, settings.get_owner()) def _validate(self, name, opt_or_descr, permissive=False): "validation for the setattr and the getattr" @@ -355,7 +355,7 @@ class Config(object): pass except Exception, e: raise e # HiddenOptionError or DisabledOptionError - homeconfig.setoption(name, value, owners.user) + homeconfig.setoption(name, value, settings.get_owner()) elif len(candidates) > 1: raise AmbigousOptionError( 'more than one option that ends with %s' % (key, )) diff --git a/tiramisu/option.py b/tiramisu/option.py index 989d4ba..9dea2e9 100644 --- a/tiramisu/option.py +++ b/tiramisu/option.py @@ -65,7 +65,7 @@ class Multi(list): super(Multi, self).__init__(lst) def __setitem__(self, key, value): - return self._setvalue(value, key, who=owners.user) + return self._setvalue(value, key, who=settings.get_owner()) def append(self, value, add_master=True): """the list value can be updated (appened) @@ -79,14 +79,14 @@ class Multi(list): raise IndexError("in a group with a master, you mustn't add " "a value in a slave's Multi value") except TypeError: - return self._setvalue(value, who=settings.owner) + return self._setvalue(value, who=settings.get_owner()) multis = [] for name, multi in self.config: multis.append(multi) for multi in multis: if master == multi.opt._name: if add_master: - ret = multi._setvalue(value, who=settings.owner) + ret = multi._setvalue(value, who=settings.get_owner()) else: ret = value else: @@ -99,13 +99,16 @@ class Multi(list): if not self.opt._validate(value): raise ConfigError("invalid value {0} " "for option {1}".format(str(value), self.opt._name)) + if not isinstance(who, owners.Owner): + raise TypeError("invalid owner {0} for the value {1}".format( + str(who), str(value))) oldvalue = list(self) if key is None: ret = super(Multi, self).append(value) else: ret = super(Multi, self).__setitem__(key, value) if who != None: - self.opt.setowner(self.config, who) + self.opt.setowner(self.config, getattr(owners, who)) self.config._cfgimpl_previous_values[self.opt._name] = oldvalue return ret @@ -140,7 +143,7 @@ class Multi(list): def _pop(self, key, change_who=True): oldvalue = list(self) if change_who: - self.opt.setowner(self.config, settings.owner) + self.opt.setowner(self.config, settings.get_owner()) self.config._cfgimpl_previous_values[self.opt._name] = oldvalue return super(Multi, self).pop(key) # ____________________________________________________________ @@ -331,7 +334,7 @@ class Option(HiddenBaseType, DisabledBaseType): (not the toplevel config) :return: boolean """ - return self.getowner(config) == 'default' + return self.getowner(config) == owners.default def setoption(self, config, value): """changes the option's value with the value_owner's who diff --git a/tiramisu/setting.py b/tiramisu/setting.py index dedf27a..431589d 100644 --- a/tiramisu/setting.py +++ b/tiramisu/setting.py @@ -114,7 +114,10 @@ class Setting(): frozen = True # enables validation function for options if set validator = False - # ____________________________________________________________ + # generic owner + owner = owners.user + + #____________________________________________________________ # properties methods def has_properties(self): "has properties means the Config's properties attribute is not empty" @@ -134,7 +137,7 @@ class Setting(): "deletes property propname in the Config's properties attribute" if self.has_property(propname): self.properties.remove(propname) - + #____________________________________________________________ def set_permissive(self, permissive): if not isinstance(permissive, list): raise TypeError('permissive must be a list') @@ -183,7 +186,12 @@ class Setting(): def set_owner(self, owner): ":param owner: sets the default value for owner at the Config level" + if not isinstance(owner, owners.Owner): + raise TypeError("invalid generic owner {0}".format(str(owner))) self.owner = owner + def get_owner(self): + return self.owner + # Setting is actually a singleton settings = Setting()