validator in sqlalchemy
authorEmmanuel Garette <egarette@cadoles.com>
Mon, 27 Jan 2014 22:28:22 +0000 (23:28 +0100)
committerEmmanuel Garette <egarette@cadoles.com>
Mon, 27 Jan 2014 22:28:22 +0000 (23:28 +0100)
tiramisu/autolib.py
tiramisu/option.py
tiramisu/storage/sqlalchemy/option.py

index f61cf18..726d53f 100644 (file)
@@ -143,13 +143,12 @@ def carry_out_calculation(option, config, callback, callback_params,
     one_is_multi = False
     # multi's option should have same value for all option
     len_multi = None
-    for callbacks in callback_params:
-        key = callbacks.name
-        for callbk in callbacks.params:
-            if callbk.option is not None:
+
+    for key, callbacks in callback_params.items():
+        for callbk in callbacks:
+            if isinstance(callbk, tuple):
                 # callbk is something link (opt, True|False)
-                opt = callbk.option
-                force_permissive = callbk.force_permissive
+                opt, force_permissive = callbk
                 path = config.cfgimpl_get_description().impl_get_path_by_opt(
                     opt)
                 # get value
@@ -180,7 +179,7 @@ def carry_out_calculation(option, config, callback, callback_params,
                 tcparams.setdefault(key, []).append((value, is_multi))
             else:
                 # callbk is a value and not a multi
-                tcparams.setdefault(key, []).append((callbk.value, False))
+                tcparams.setdefault(key, []).append((callbk, False))
 
     # if one value is a multi, launch several time calculate
     # if index is set, return a value
index 587772d..8556e92 100644 (file)
@@ -22,7 +22,7 @@
 # ____________________________________________________________
 import re
 import sys
-from copy import copy
+from copy import copy  # , deepcopy
 from types import FunctionType
 from IPy import IP
 import warnings
@@ -104,8 +104,7 @@ class Base(StorageBase):
             validate_callback(validator, validator_params, 'validator')
             self._validator = validator
             if validator_params is not None:
-                for key, values in validator_params.items():
-                    self._add_validator(key, values)
+                self._validator_params = validator_params
         if callback is None and callback_params is not None:
             raise ValueError(_("params defined for a callback function but "
                              "no callback defined"
@@ -114,8 +113,7 @@ class Base(StorageBase):
             validate_callback(callback, callback_params, 'callback')
             self._callback = callback
             if callback_params is not None:
-                for key, values in callback_params.items():
-                    self._add_callback(key, values)
+                self._callback_params = callback_params
         if self._calc_properties != frozenset([]) and properties is not tuple():
             set_forbidden_properties = self._calc_properties & set(properties)
             if set_forbidden_properties != frozenset():
@@ -443,33 +441,21 @@ class Option(BaseOption):
 
         def val_validator(val):
             if self._validator is not None:
-                class FakeCallbackParamOption(object):
-                    def __init__(self, option=None,
-                                 force_permissive=None,
-                                 value=None):
-                        self.option = option
-                        self.force_permissive = force_permissive
-                        self.value = value
-
-                class FakeCallbackParam(object):
-                    def __init__(self, key, params):
-                        self.name = key
-                        self.params = params
-                if self._validator_params != []:
-                    validator_params = []
-                    validator_params_option = [FakeCallbackParamOption(value=val)]
-                    #if '' in self._validator_params:
-                    #    for param in self._validator_params['']:
-                    #        if isinstance(param, tuple):
-                    #            validator_params_option.append(FakeCallbackParamOption(option=param[0], force_permissive=param[1]))
-                    #        else:
-                    #            validator_params_option.append(FakeCallbackParamOption(value=param))
-                    validator_params.append(FakeCallbackParam('', validator_params_option))
-                    for key in self._validator_params:
-                        if key.name != '':
-                            validator_params.append(key)
+                if self._validator_params is not None:
+                    validator_params = {}
+                    #FIXME toujours utile ce deepcopy ?
+                    #validator_params = deepcopy(self._validator_params)
+                    for val_param, values in self._validator_params.items():
+                        validator_params[val_param] = values
+                    #FIXME ... ca sert à quoi ...
+                    if '' in validator_params:
+                        lst = list(validator_params[''])
+                        lst.insert(0, val)
+                        validator_params[''] = tuple(lst)
+                    else:
+                        validator_params[''] = (val,)
                 else:
-                    validator_params = (FakeCallbackParam('', (FakeCallbackParamOption(value=val),)),)
+                    validator_params = {'': (val,)}
                 # Raise ValueError if not valid
                 carry_out_calculation(self, config=context,
                                       callback=self._validator,
index 1b7fbca..dc0bc7d 100644 (file)
@@ -149,6 +149,25 @@ class _Information(SqlAlchemyBase):
 #____________________________________________________________
 #
 # callback
+def load_callback_parm(collection_type, proxy):
+    def getter(obj):
+        if obj is None:
+            return None
+        ret = []
+        requires = getattr(obj, proxy.value_attr)
+        for require in requires:
+            if require.value is not None:
+                ret.append(require.value)
+            else:
+                option = session.query(_Base).filter_by(id=require.option).first()
+                ret.append((option, require.force_permissive))
+        return tuple(ret)
+
+    def setter(obj, value):
+        setattr(obj, proxy.value_attr, value)
+    return getter, setter
+
+
 class _CallbackParamOption(SqlAlchemyBase):
     __tablename__ = 'callback_param_option'
     id = Column(Integer, primary_key=True)
@@ -169,11 +188,11 @@ class _CallbackParam(SqlAlchemyBase):
     __tablename__ = 'callback_param'
     id = Column(Integer, primary_key=True)
     callback = Column(Integer, ForeignKey('baseoption.id'))
-    name = Column(String)
+    key = Column(String)
     params = relationship('_CallbackParamOption')
 
-    def __init__(self, name, params):
-        self.name = name
+    def __init__(self, key, params):
+        self.key = key
         for param in params:
             if isinstance(param, tuple):
                 self.params.append(_CallbackParamOption(option=param[0],
@@ -220,10 +239,20 @@ class _Base(SqlAlchemyBase):
     _requires = association_proxy("_reqs", "requires", getset_factory=load_requires)
     _multi = Column(Boolean)
     _multitype = Column(String)
+    ######
     _callback = Column(PickleType)
-    _callback_params = relationship('_CallbackParam')
+    _call_params = relationship('_CallbackParam',
+                                collection_class=
+                                attribute_mapped_collection('key'))
+    _callback_params = association_proxy("_call_params", "params",
+                                         getset_factory=load_callback_parm)
     _validator = Column(PickleType)
-    _validator_params = relationship('_CallbackParam')
+    _val_params = relationship('_CallbackParam',
+                               collection_class=
+                               attribute_mapped_collection('key'))
+    _validator_params = association_proxy("_call_params", "params",
+                                          getset_factory=load_callback_parm)
+    ######
     _parent = Column(Integer, ForeignKey('baseoption.id'))
     _children = relationship('BaseOption', enable_typechecks=False)
     #FIXME pas 2 fois la meme properties dans la base ...
@@ -261,15 +290,6 @@ class _Base(SqlAlchemyBase):
             prop_obj = _PropertyOption(propname)
         return prop_obj
 
-    #def _add_require(self, require):
-    #    self._requires.append(_RequireOption(*require))
-
-    def _add_callback(self, key, values):
-        self._callback_params.append(_CallbackParam(key, values))
-
-    def _add_validator(self, key, values):
-        self._validator_params.append(_CallbackParam(key, values))
-
     def _add_consistency(self, func, all_cons_opts):
         _Consistency(func, all_cons_opts)