callback must be a function and validator support same paramaters has callback

This commit is contained in:
Emmanuel Garette 2013-04-22 16:48:13 +02:00
parent e883e5b89e
commit 1fd5f685de
3 changed files with 35 additions and 15 deletions

View File

@ -43,6 +43,8 @@ def carry_out_calculation(name, config, callback, callback_params):
for key, values in callback_params.items():
for value in values:
if type(value) == tuple:
if config is None:
raise ConfigError(_('no config specified but needed'))
path, check_disabled = value
try:
opt_value = config._getattr(path, force_permissive=True)
@ -108,13 +110,4 @@ def carry_out_calculation(name, config, callback, callback_params):
def calculate(name, callback, params, tcparams):
try:
# XXX not only creole...
from creole import eosfunc
return getattr(eosfunc, callback)(*params, **tcparams)
except AttributeError, err:
import traceback
traceback.print_exc()
raise ConfigError("callback: {0} return error {1} for "
"option: {2}".format(callback, str(err),
name))
return callback(*params, **tcparams)

View File

@ -24,6 +24,7 @@
# or if not logical
# or if validation failled
# or multi must be a list
# or error in autolib
#TypeError if parameter has no good type
#AttributeError if no option or optiondescription in optiondescription (also when specified a path)

View File

@ -21,13 +21,14 @@
# the whole pypy projet is under MIT licence
# ____________________________________________________________
import re
from copy import copy
from copy import copy, deepcopy
from types import FunctionType
from IPy import IP
from tiramisu.error import ConflictError
from tiramisu.setting import groups, multitypes
from tiramisu.i18n import _
from tiramisu.autolib import carry_out_calculation
name_regexp = re.compile(r'^\d+')
forbidden_names = ['iter_all', 'iter_group', 'find', 'find_fisrt',
@ -138,8 +139,8 @@ class Option(BaseInformation):
raise ValueError(_("params defined for a callback function but "
"no callback defined yet for option {0}").format(name))
if callback is not None:
if not isinstance(callback, str):
raise ValueError('callback must be a string')
if type(callback) != FunctionType:
raise ValueError('callback must be a function')
if callback_params is not None and \
not isinstance(callback_params, dict):
raise ValueError('callback_params must be a dict')
@ -176,6 +177,22 @@ class Option(BaseInformation):
:param value: the option's value
:param validate: if true enables ``self._validator`` validation
"""
def _val_validator(val):
callback_params = deepcopy(self._validator[1])
callback_params.setdefault('', []).insert(0, val)
return carry_out_calculation(self._name, config=context,
callback=self._validator[0],
callback_params=callback_params)
def val_validator():
#add current value has first argument
if self.optimpl_is_multi():
for val in value:
if not _val_validator(val):
return False
else:
return _val_validator(val)
return True
# generic calculation
if context is not None:
cons = context.cfgimpl_get_description()
@ -203,11 +220,14 @@ class Option(BaseInformation):
if val is not None:
# customizing the validator
if validate and self._validator is not None and \
not self._validator[0](val, **self._validator[1]):
not val_validator():
return False
if not self._validate(val):
return False
if cons is not None and not cons._valid_consistency(self, val, context, index):
if cons is not None and not cons._valid_consistency(self,
val,
context,
index):
return False
return True
@ -308,6 +328,12 @@ class ChoiceOption(Option):
validator_args=validator_args,
properties=properties)
def optimpl_get_values(self):
return self._values
def optimpl_is_openvalues(self):
return self._open_values
def _validate(self, value):
if not self._open_values:
return value is None or value in self._values