merge
authorgwen <gremond@cadoles.com>
Mon, 2 Sep 2013 13:11:38 +0000 (15:11 +0200)
committergwen <gremond@cadoles.com>
Mon, 2 Sep 2013 13:11:38 +0000 (15:11 +0200)
1  2 
tiramisu/option.py

@@@ -92,76 -92,81 +92,93 @@@ class BaseInformation(object)
  
  
  class BaseOption(BaseInformation):
-     __slots__ = ('_readonly',)
 +    """This abstract base class stands for attribute access
 +    in options that have to be set only once, it is of course done in the
 +    __setattr__ method
 +    """
+     __slots__ = ('_readonly', '_state_consistencies', '_state_requires')
  
      def __setattr__(self, name, value):
-         is_readonly = False
-         # never change _name
-         if name == '_name':
 +        """set once and only once some attributes in the option,
 +        like `_name`. `_name` cannot be changed one the option and
 +        pushed in the :class:`tiramisu.option.OptionDescription`.
 +
 +        if the attribute `_readonly` is set to `True`, the option is
 +        "frozen" (which has noting to do with the high level "freeze"
 +        propertie or "read_only" property)
 +        """
+         if not name.startswith('_state'):
+             is_readonly = False
+             # never change _name
+             if name == '_name':
+                 try:
+                     self._name
+                     #so _name is already set
+                     is_readonly = True
+                 except:
+                     pass
              try:
-                 self._name
-                 #so _name is already set
-                 is_readonly = True
+                 if self._readonly is True:
+                     if value is True:
+                         # already readonly and try to re set readonly
+                         # don't raise, just exit
+                         return
+                     is_readonly = True
              except AttributeError:
                  pass
-         try:
-             if self._readonly is True:
-                 if value is True:
-                     # already readonly and try to re set readonly
-                     # don't raise, just exit
-                     return
-                 is_readonly = True
-         except AttributeError:
-             pass
-         if is_readonly:
-             raise AttributeError(_("'{0}' ({1}) object attribute '{2}' is"
-                                    " read-only").format(
-                                        self.__class__.__name__, self._name,
-                                        name))
+             if is_readonly:
+                 raise AttributeError(_("'{0}' ({1}) object attribute '{2}' is"
+                                        " read-only").format(
+                                            self.__class__.__name__,
+                                            self._name,
+                                            name))
          object.__setattr__(self, name, value)
  
-     def _impl_convert_consistencies(self, value, cache):
+     def _impl_convert_consistencies(self, cache):
          # cache is a dico in import/not a dico in export
-         new_value = []
-         for consistency in value:
+         if self._consistencies is None:
+             self._state_consistencies = None
+         else:
+             new_value = []
+             for consistency in self._consistencies:
+                 if isinstance(cache, dict):
+                     new_value.append((consistency[0], cache[consistency[1]]))
+                 else:
+                     new_value.append((consistency[0],
+                                       cache.impl_get_path_by_opt(
+                                           consistency[1])))
              if isinstance(cache, dict):
-                 new_value = (consistency[0], cache[consistency[1]])
+                 pass
              else:
-                 new_value = (consistency[0], cache.impl_get_path_by_opt(
-                     consistency[1]))
-         return tuple(new_value)
+                 self._state_consistencies = tuple(new_value)
  
-     def _impl_convert_requires(self, value, cache):
+     def _impl_convert_requires(self, cache):
          # cache is a dico in import/not a dico in export
-         new_value = []
-         for requires in value:
-             new_requires = []
-             for require in requires:
-                 if isinstance(cache, dict):
-                     new_require = [cache[require[0]]]
-                 else:
-                     new_require = [cache.impl_get_path_by_opt(require[0])]
-                 new_require.extend(require[1:])
-                 new_requires.append(tuple(new_require))
-             new_value.append(tuple(new_requires))
-         return tuple(new_value)
-     def impl_export(self, descr):
-         descr.impl_build_cache()
-         # add _opt_type (not in __slots__)
-         slots = set(['_opt_type'])
+         if self._requires is None:
+             self._state_requires = None
+         else:
+             new_value = []
+             for requires in self._requires:
+                 new_requires = []
+                 for require in requires:
+                     if isinstance(cache, dict):
+                         new_require = [cache[require[0]]]
+                     else:
+                         new_require = [cache.impl_get_path_by_opt(require[0])]
+                     new_require.extend(require[1:])
+                     new_requires.append(tuple(new_require))
+                 new_value.append(tuple(new_requires))
+             if isinstance(cache, dict):
+                 pass
+             else:
+                 self._state_requires = new_value
+     def _impl_getstate(self, descr):
+         self._impl_convert_consistencies(descr)
+         self._impl_convert_requires(descr)
+     def __getstate__(self, export=False):
+         slots = set()
          for subclass in self.__class__.__mro__:
              if subclass is not object:
                  slots.update(subclass.__slots__)
@@@ -992,26 -997,23 +1009,26 @@@ class OptionDescription(BaseOption)
                      return False
          return True
  
-     def _impl_convert_group_type(self, value, cache):
-         if isinstance(cache, dict):
-             value = str(value)
-         else:
-             value = getattr(groups, value)
-         return value
-     def impl_export(self, descr=None):
+     def _impl_getstate(self, descr=None):
 +        """enables us to export into a dict
 +        :param descr: parent :class:`tiramisu.option.OptionDescription`
 +        """
          if descr is None:
+             self.impl_build_cache()
              descr = self
-         export = super(OptionDescription, self).impl_export(descr)
-         export['_group_type'] = self._impl_convert_group_type(
-             export['_group_type'], descr)
-         export['options'] = []
+         super(OptionDescription, self)._impl_getstate(descr)
+         self._state_group_type = str(self._group_type)
+         for option in self.impl_getchildren():
+             option._impl_getstate(descr)
+     def __getstate__(self, export=False):
+         if not export:
+             self._impl_getstate()
+         states = super(OptionDescription, self).__getstate__(True)
+         states['_state_children'] = []
          for option in self.impl_getchildren():
-             export['options'].append(option.impl_export(descr))
-         return export
+             states['_state_children'].append(option.__getstate__(True))
+         return states
  
  
  def validate_requires_arg(requires, name):