require works well in sqlalchemy storage
[tiramisu.git] / tiramisu / option.py
index 014be90..587772d 100644 (file)
@@ -67,11 +67,9 @@ class Base(StorageBase):
             raise ValueError(_("invalid name: {0} for option").format(name))
         self._name = name
         self.impl_set_information('doc', doc)
-        requires = validate_requires_arg(requires, self._name)
         if requires is not None:
-            for values in requires.values():
-                for require in values.values():
-                    self._add_require(require)
+            self._calc_properties, self._requires = validate_requires_arg(
+                requires, self._name)
         if not multi and default_multi is not None:
             raise ValueError(_("a default_multi is set whereas multi is False"
                              " in option: {0}").format(name))
@@ -118,8 +116,8 @@ class Base(StorageBase):
             if callback_params is not None:
                 for key, values in callback_params.items():
                     self._add_callback(key, values)
-        if requires is not None and properties is not tuple():
-            set_forbidden_properties = set(properties) & set(requires.keys())
+        if self._calc_properties != frozenset([]) and properties is not tuple():
+            set_forbidden_properties = self._calc_properties & set(properties)
             if set_forbidden_properties != frozenset():
                 raise ValueError('conflict: properties already set in '
                                  'requirement {0}'.format(
@@ -133,8 +131,9 @@ class Base(StorageBase):
             self._default = []
         else:
             self._default = default
-        for prop in properties:
-            self._properties.append(self._get_property_object(prop))
+        self._properties = properties
+        #for prop in properties:
+            #self._properties.append(self._get_property_object(prop))
         self._warnings_only = warnings_only
         return super(Base, self).__init__()
 
@@ -148,6 +147,29 @@ class BaseOption(Base):
     #             '_calc_properties', '_impl_informations',
     #             '_state_readonly', '_state_requires', '_stated')
 
+    # information
+    def impl_set_information(self, key, value):
+        """updates the information's attribute
+        (which is a dictionary)
+
+        :param key: information's key (ex: "help", "doc"
+        :param value: information's value (ex: "the help string")
+        """
+        self._informations[key] = value
+
+    def impl_get_information(self, key, default=None):
+        """retrieves one information's item
+
+        :param key: the item string (ex: "help")
+        """
+        if key in self._informations:
+            return self._informations[key]
+        elif default is not None:
+            return default
+        else:
+            raise ValueError(_("information's item not found: {0}").format(
+                key))
+
     # ____________________________________________________________
     # serialize object
     def _impl_convert_requires(self, descr, load=False):
@@ -349,9 +371,9 @@ class Option(BaseOption):
     #                                       name))
     #    object.__setattr__(self, name, value)
 
-    def impl_getproperties(self):
-        for prop in self._properties:
-            yield(prop.name)
+    #def impl_getproperties(self):
+    #    for prop in self._properties:
+    #        yield(prop.name)
 
     def impl_getrequires(self):
         return self._requires
@@ -1161,10 +1183,10 @@ class OptionDescription(BaseOption, StorageOptionDescription):
         # the group_type is useful for filtering OptionDescriptions in a config
         self._optiondescription_group_type = groups.default
 
-    def impl_getproperties(self):
-        #FIXME
-        for prop in self._properties:
-            yield(prop.name)
+    #def impl_getproperties(self):
+    #    #FIXME
+    #    for prop in self._properties:
+    #        yield(prop.name)
 
     def impl_getrequires(self):
         #FIXME
@@ -1223,14 +1245,16 @@ class OptionDescription(BaseOption, StorageOptionDescription):
         #for child in self._children:
         #    yield(session.query(child._type).filter_by(id=child.id).first())
 
-    def impl_build_cache_consistency(self, _consistencies=None):
+    def impl_build_cache_consistency(self, _consistencies=None, cache_option=None):
         #FIXME cache_option !
         if _consistencies is None:
             init = True
             _consistencies = {}
+            cache_option = []
         else:
             init = False
         for option in self.impl_getchildren():
+            cache_option.append(option.id)
             if not isinstance(option, OptionDescription):
                 for consistency in option._consistencies:
                     func = consistency.func
@@ -1240,15 +1264,15 @@ class OptionDescription(BaseOption, StorageOptionDescription):
                                                   []).append((func,
                                                              all_cons_opts))
             else:
-                option.impl_build_cache_consistency(_consistencies)
+                option.impl_build_cache_consistency(_consistencies, cache_option)
         if init and _consistencies != {}:
             self._cache_consistencies = {}
             for opt, cons in _consistencies.items():
                 #FIXME dans le cache ...
-                #if opt.id not in cache_option:
-                #    raise ConfigError(_('consistency with option {0} '
-                #                        'which is not in Config').format(
-                #                            opt.impl_getname()))
+                if opt.id not in cache_option:
+                    raise ConfigError(_('consistency with option {0} '
+                                        'which is not in Config').format(
+                                            opt.impl_getname()))
                 self._cache_consistencies[opt] = tuple(cons)
 
     def impl_validate_options(self, cache_option=None):
@@ -1509,15 +1533,15 @@ def validate_requires_arg(requires, name):
                                             inverse, transitive, same_action)
         else:
             ret_requires[action][option][1].append(expected)
-    ## transform dict to tuple
-    #ret = []
-    #for opt_requires in ret_requires.values():
-    #    ret_action = []
-    #    for require in opt_requires.values():
-    #        ret_action.append((require[0], tuple(require[1]), require[2],
-    #                           require[3], require[4], require[5]))
-    #    ret.append(tuple(ret_action))
-    return ret_requires
+    # transform dict to tuple
+    ret = []
+    for opt_requires in ret_requires.values():
+        ret_action = []
+        for require in opt_requires.values():
+            ret_action.append((require[0], tuple(require[1]), require[2],
+                               require[3], require[4], require[5]))
+        ret.append(tuple(ret_action))
+    return frozenset(config_action.keys()), tuple(ret)
 
 
 def validate_callback(callback, callback_params, type_):