validator in sqlalchemy

This commit is contained in:
Emmanuel Garette 2014-01-27 23:28:22 +01:00
parent a1dd2cfce7
commit 217a1989c7
3 changed files with 57 additions and 52 deletions

View File

@ -143,13 +143,12 @@ def carry_out_calculation(option, config, callback, callback_params,
one_is_multi = False one_is_multi = False
# multi's option should have same value for all option # multi's option should have same value for all option
len_multi = None len_multi = None
for callbacks in callback_params:
key = callbacks.name for key, callbacks in callback_params.items():
for callbk in callbacks.params: for callbk in callbacks:
if callbk.option is not None: if isinstance(callbk, tuple):
# callbk is something link (opt, True|False) # callbk is something link (opt, True|False)
opt = callbk.option opt, force_permissive = callbk
force_permissive = callbk.force_permissive
path = config.cfgimpl_get_description().impl_get_path_by_opt( path = config.cfgimpl_get_description().impl_get_path_by_opt(
opt) opt)
# get value # get value
@ -180,7 +179,7 @@ def carry_out_calculation(option, config, callback, callback_params,
tcparams.setdefault(key, []).append((value, is_multi)) tcparams.setdefault(key, []).append((value, is_multi))
else: else:
# callbk is a value and not a multi # 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 one value is a multi, launch several time calculate
# if index is set, return a value # if index is set, return a value

View File

@ -22,7 +22,7 @@
# ____________________________________________________________ # ____________________________________________________________
import re import re
import sys import sys
from copy import copy from copy import copy # , deepcopy
from types import FunctionType from types import FunctionType
from IPy import IP from IPy import IP
import warnings import warnings
@ -104,8 +104,7 @@ class Base(StorageBase):
validate_callback(validator, validator_params, 'validator') validate_callback(validator, validator_params, 'validator')
self._validator = validator self._validator = validator
if validator_params is not None: if validator_params is not None:
for key, values in validator_params.items(): self._validator_params = validator_params
self._add_validator(key, values)
if callback is None and callback_params is not None: if callback is None and callback_params is not None:
raise ValueError(_("params defined for a callback function but " raise ValueError(_("params defined for a callback function but "
"no callback defined" "no callback defined"
@ -114,8 +113,7 @@ class Base(StorageBase):
validate_callback(callback, callback_params, 'callback') validate_callback(callback, callback_params, 'callback')
self._callback = callback self._callback = callback
if callback_params is not None: if callback_params is not None:
for key, values in callback_params.items(): self._callback_params = callback_params
self._add_callback(key, values)
if self._calc_properties != frozenset([]) and properties is not tuple(): if self._calc_properties != frozenset([]) and properties is not tuple():
set_forbidden_properties = self._calc_properties & set(properties) set_forbidden_properties = self._calc_properties & set(properties)
if set_forbidden_properties != frozenset(): if set_forbidden_properties != frozenset():
@ -443,33 +441,21 @@ class Option(BaseOption):
def val_validator(val): def val_validator(val):
if self._validator is not None: if self._validator is not None:
class FakeCallbackParamOption(object): if self._validator_params is not None:
def __init__(self, option=None, validator_params = {}
force_permissive=None, #FIXME toujours utile ce deepcopy ?
value=None): #validator_params = deepcopy(self._validator_params)
self.option = option for val_param, values in self._validator_params.items():
self.force_permissive = force_permissive validator_params[val_param] = values
self.value = value #FIXME ... ca sert à quoi ...
if '' in validator_params:
class FakeCallbackParam(object): lst = list(validator_params[''])
def __init__(self, key, params): lst.insert(0, val)
self.name = key validator_params[''] = tuple(lst)
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)
else: else:
validator_params = (FakeCallbackParam('', (FakeCallbackParamOption(value=val),)),) validator_params[''] = (val,)
else:
validator_params = {'': (val,)}
# Raise ValueError if not valid # Raise ValueError if not valid
carry_out_calculation(self, config=context, carry_out_calculation(self, config=context,
callback=self._validator, callback=self._validator,

View File

@ -149,6 +149,25 @@ class _Information(SqlAlchemyBase):
#____________________________________________________________ #____________________________________________________________
# #
# callback # 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): class _CallbackParamOption(SqlAlchemyBase):
__tablename__ = 'callback_param_option' __tablename__ = 'callback_param_option'
id = Column(Integer, primary_key=True) id = Column(Integer, primary_key=True)
@ -169,11 +188,11 @@ class _CallbackParam(SqlAlchemyBase):
__tablename__ = 'callback_param' __tablename__ = 'callback_param'
id = Column(Integer, primary_key=True) id = Column(Integer, primary_key=True)
callback = Column(Integer, ForeignKey('baseoption.id')) callback = Column(Integer, ForeignKey('baseoption.id'))
name = Column(String) key = Column(String)
params = relationship('_CallbackParamOption') params = relationship('_CallbackParamOption')
def __init__(self, name, params): def __init__(self, key, params):
self.name = name self.key = key
for param in params: for param in params:
if isinstance(param, tuple): if isinstance(param, tuple):
self.params.append(_CallbackParamOption(option=param[0], self.params.append(_CallbackParamOption(option=param[0],
@ -220,10 +239,20 @@ class _Base(SqlAlchemyBase):
_requires = association_proxy("_reqs", "requires", getset_factory=load_requires) _requires = association_proxy("_reqs", "requires", getset_factory=load_requires)
_multi = Column(Boolean) _multi = Column(Boolean)
_multitype = Column(String) _multitype = Column(String)
######
_callback = Column(PickleType) _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 = 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')) _parent = Column(Integer, ForeignKey('baseoption.id'))
_children = relationship('BaseOption', enable_typechecks=False) _children = relationship('BaseOption', enable_typechecks=False)
#FIXME pas 2 fois la meme properties dans la base ... #FIXME pas 2 fois la meme properties dans la base ...
@ -261,15 +290,6 @@ class _Base(SqlAlchemyBase):
prop_obj = _PropertyOption(propname) prop_obj = _PropertyOption(propname)
return prop_obj 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): def _add_consistency(self, func, all_cons_opts):
_Consistency(func, all_cons_opts) _Consistency(func, all_cons_opts)