refactoring, the values are in an OptionValues object
[tiramisu.git] / tiramisu / option.py
index 2dc6221..ca949af 100644 (file)
@@ -27,6 +27,7 @@ from tiramisu.error import (ConfigError, ConflictConfigError, NotFoundError,
     PropertiesOptionError)
 from tiramisu.autolib import carry_out_calculation
 from tiramisu.setting import groups, owners
+from tiramisu.value import Multi
 
 requires_actions = [('hide', 'show'), ('enable', 'disable'), ('freeze', 'unfreeze')]
 
@@ -37,62 +38,6 @@ for act1, act2 in requires_actions:
     reverse_actions[act1] = act2
     reverse_actions[act2] = act1
 # ____________________________________________________________
-# multi types
-
-class Multi(list):
-    """multi options values container
-    that support item notation for the values of multi options"""
-    def __init__(self, lst, config, opt):
-        """
-        :param lst: the Multi wraps a list value
-        :param config: the parent config
-        :param opt: the option object that have this Multi value
-        """
-        self.config = config
-        self.opt = opt
-        super(Multi, self).__init__(lst)
-
-    def __setitem__(self, key, value):
-        self._setvalue(value, key,
-                who=self.config._cfgimpl_context._cfgimpl_settings.get_owner())
-
-    def append(self, value):
-        """the list value can be updated (appened)
-        only if the option is a master
-        """
-        self._setvalue(value,
-                who=self.config._cfgimpl_context._cfgimpl_settings.get_owner())
-
-    def _setvalue(self, value, key=None, who=None):
-        if value != None:
-            if not self.opt._validate(value):
-                raise ConfigError("invalid value {0} "
-                    "for option {1}".format(str(value), self.opt._name))
-        oldvalue = list(self)
-        if key is None:
-            super(Multi, self).append(value)
-        else:
-            super(Multi, self).__setitem__(key, value)
-        if who != None:
-            if not isinstance(who, owners.Owner):
-                raise TypeError("invalid owner {0} for the value {1}".format(
-                                str(who), str(value)))
-            self.opt.setowner(self.config, getattr(owners, who))
-        self.config._cfgimpl_context._cfgimpl_values.previous_values[self.opt] = oldvalue
-
-    def pop(self, key):
-        """the list value can be updated (poped)
-        only if the option is a master
-
-        :param key: index of the element to pop
-        :return: the requested element
-
-        """
-        self.opt.setowner(self.config,
-                    self.config._cfgimpl_context._cfgimpl_settings.get_owner())
-        self.config._cfgimpl_context._cfgimpl_values.previous_values[self.opt] = list(self)
-        return super(Multi, self).pop(key)
-# ____________________________________________________________
 #
 class Option(HiddenBaseType, DisabledBaseType):
     """
@@ -267,7 +212,7 @@ class Option(HiddenBaseType, DisabledBaseType):
 
     def getowner(self, config):
         "config *must* be only the **parent** config (not the toplevel config)"
-        return config._cfgimpl_context._cfgimpl_values.owners[self]
+        return config._cfgimpl_context._cfgimpl_values.getowner(self)
 
     def reset(self, config):
         """resets the default value and owner
@@ -297,13 +242,14 @@ class Option(HiddenBaseType, DisabledBaseType):
             if not self.is_multi() and value == '':
                 value = None
             if self.is_multi() and '' in value:
-                value = Multi([{'': None}.get(i, i) for i in value], config, self)
+                value = Multi([{'': None}.get(i, i) for i in value],
+                                config._cfgimpl_context, self)
             if config._cfgimpl_context._cfgimpl_settings.is_mandatory() \
                 and ((self.is_multi() and value == []) or \
                 (not self.is_multi() and value is None)):
                 raise MandatoryError('cannot change the value to %s for '
               'option %s' % (value, name))
-        if self not in config._cfgimpl_context._cfgimpl_values:
+        if self not in config._cfgimpl_descr._children:
             raise AttributeError('unknown option %s' % (name))
 
         if config._cfgimpl_context._cfgimpl_settings.is_frozen_for_everything():
@@ -315,10 +261,10 @@ class Option(HiddenBaseType, DisabledBaseType):
             raise TypeError('cannot change the value to %s for '
                'option %s this option is frozen' % (str(value), name))
         apply_requires(self, config)
-        if type(config._cfgimpl_context._cfgimpl_values[self]) == Multi:
-            config._cfgimpl_context._cfgimpl_values.previous_values[self] = list(config._cfgimpl_context._cfgimpl_values[self])
-        else:
-            config._cfgimpl_context._cfgimpl_values.previous_values[self] = config._cfgimpl_context._cfgimpl_values[self]
+#        if type(config._cfgimpl_context._cfgimpl_values[self]) == Multi:
+#            config._cfgimpl_context._cfgimpl_values.previous_values[self] = list(config._cfgimpl_context._cfgimpl_values[self])
+#        else:
+#            config._cfgimpl_context._cfgimpl_values.previous_values[self] = config._cfgimpl_context._cfgimpl_values[self]
         config._cfgimpl_context._cfgimpl_values[self] = value
 
     def getkey(self, value):