reorganisation
This commit is contained in:
@ -59,10 +59,21 @@ class Option(OnlyOption):
|
||||
'_choice_values_params',
|
||||
)
|
||||
_empty = ''
|
||||
def __init__(self, name, doc, default=undefined, default_multi=None,
|
||||
requires=None, multi=False, unique=undefined, callback=None,
|
||||
callback_params=None, validator=None, validator_params=None,
|
||||
properties=None, warnings_only=False, extra=None,
|
||||
def __init__(self,
|
||||
name,
|
||||
doc,
|
||||
default=undefined,
|
||||
default_multi=None,
|
||||
requires=None,
|
||||
multi=False,
|
||||
unique=undefined,
|
||||
callback=None,
|
||||
callback_params=None,
|
||||
validator=None,
|
||||
validator_params=None,
|
||||
properties=None,
|
||||
warnings_only=False,
|
||||
extra=None,
|
||||
allow_empty_list=undefined):
|
||||
|
||||
_setattr = object.__setattr__
|
||||
@ -91,9 +102,13 @@ class Option(OnlyOption):
|
||||
default = []
|
||||
if validator is not None:
|
||||
if multi: # and validator_params is None:
|
||||
validator_params = self._build_validator_params(validator, validator_params)
|
||||
validator_params = self._build_validator_params(validator,
|
||||
validator_params)
|
||||
|
||||
validate_callback(validator, validator_params, 'validator', self)
|
||||
validate_callback(validator,
|
||||
validator_params,
|
||||
'validator',
|
||||
self)
|
||||
if validator_params is None:
|
||||
val_call = (validator,)
|
||||
else:
|
||||
@ -110,22 +125,25 @@ class Option(OnlyOption):
|
||||
if allow_empty_list is not undefined:
|
||||
_setattr(self, '_allow_empty_list', allow_empty_list)
|
||||
|
||||
super(Option, self).__init__(name, doc, requires=requires,
|
||||
properties=properties, is_multi=is_multi)
|
||||
super(Option, self).__init__(name,
|
||||
doc,
|
||||
requires=requires,
|
||||
properties=properties,
|
||||
is_multi=is_multi)
|
||||
if is_multi and default_multi is not None:
|
||||
def test_multi_value(value):
|
||||
err = self._validate(value)
|
||||
if err:
|
||||
raise ValueError(_("invalid default_multi value {0} "
|
||||
"for option {1}: {2}").format(
|
||||
str(value),
|
||||
self.impl_getname(), str(err)))
|
||||
"for option {1}: {2}").format(str(value),
|
||||
self.impl_getname(),
|
||||
str(err)))
|
||||
if _multi is submulti:
|
||||
if not isinstance(default_multi, list):
|
||||
raise ValueError(_("invalid default_multi value {0} "
|
||||
"for option {1}: must be a list for a submulti").format(
|
||||
str(default_multi),
|
||||
self.impl_getname()))
|
||||
"for option {1}: must be a list for a submulti"
|
||||
"").format(str(default_multi),
|
||||
self.impl_getname()))
|
||||
for value in default_multi:
|
||||
test_multi_value(value)
|
||||
else:
|
||||
@ -133,7 +151,8 @@ class Option(OnlyOption):
|
||||
_setattr(self, '_default_multi', default_multi)
|
||||
if unique is not undefined:
|
||||
_setattr(self, '_unique', unique)
|
||||
err = self.impl_validate(default, is_multi=is_multi)
|
||||
err = self.impl_validate(default,
|
||||
is_multi=is_multi)
|
||||
if err:
|
||||
raise err
|
||||
if (is_multi and default != []) or \
|
||||
@ -142,11 +161,18 @@ class Option(OnlyOption):
|
||||
default = tuple(default)
|
||||
_setattr(self, '_default', default)
|
||||
|
||||
self.impl_set_callback(callback, callback_params, _init=True)
|
||||
self.impl_set_callback(callback,
|
||||
callback_params,
|
||||
_init=True)
|
||||
|
||||
def impl_is_multi(self):
|
||||
return getattr(self, '_multi', 1) != 1
|
||||
|
||||
def _validate(self,
|
||||
*args,
|
||||
**kwargs):
|
||||
pass
|
||||
|
||||
def _launch_consistency(self,
|
||||
current_opt,
|
||||
func,
|
||||
@ -270,7 +296,6 @@ class Option(OnlyOption):
|
||||
def impl_validate(self,
|
||||
value,
|
||||
context=undefined,
|
||||
validate=True,
|
||||
force_index=None,
|
||||
current_opt=undefined,
|
||||
is_multi=None,
|
||||
@ -282,14 +307,11 @@ class Option(OnlyOption):
|
||||
:param value: the option's value
|
||||
:param context: Config's context
|
||||
:type context: :class:`tiramisu.config.Config`
|
||||
:param validate: if true enables ``self._validator`` validation
|
||||
:type validate: boolean
|
||||
:param force_index: if multi, value has to be a list
|
||||
not if force_index is not None
|
||||
:type force_index: integer
|
||||
"""
|
||||
if not validate:
|
||||
return
|
||||
if current_opt is undefined:
|
||||
current_opt = self
|
||||
|
||||
@ -298,12 +320,13 @@ class Option(OnlyOption):
|
||||
display_warnings = display_warnings and (setting_properties is undefined or 'warnings' in setting_properties)
|
||||
|
||||
def _is_not_unique(value):
|
||||
#FIXME pourquoi la longueur doit etre egal ???
|
||||
if display_error and self.impl_is_unique() and len(set(value)) != len(value):
|
||||
for idx, val in enumerate(value):
|
||||
if val in value[idx+1:]:
|
||||
raise ValueError(_('invalid value "{}", this value is already in "{}"'
|
||||
'').format(val,
|
||||
self.impl_get_display_name()))
|
||||
'').format(val,
|
||||
self.impl_get_display_name()))
|
||||
|
||||
def calculation_validator(val,
|
||||
_index):
|
||||
@ -323,115 +346,79 @@ class Option(OnlyOption):
|
||||
else:
|
||||
validator_params_ = {'': (val,)}
|
||||
# Raise ValueError if not valid
|
||||
value = carry_out_calculation(current_opt,
|
||||
context=context,
|
||||
callback=validator,
|
||||
callback_params=validator_params_,
|
||||
setting_properties=setting_properties,
|
||||
index=_index,
|
||||
is_validator=True)
|
||||
if isinstance(value, Exception):
|
||||
return value
|
||||
carry_out_calculation(current_opt,
|
||||
context=context,
|
||||
callback=validator,
|
||||
callback_params=validator_params_,
|
||||
setting_properties=setting_properties,
|
||||
index=_index,
|
||||
is_validator=True)
|
||||
|
||||
def do_validation(_value,
|
||||
_index):
|
||||
if _value is None:
|
||||
error = None
|
||||
else:
|
||||
if display_error:
|
||||
# option validation
|
||||
err = self._validate(_value,
|
||||
context,
|
||||
current_opt)
|
||||
if err:
|
||||
if debug: # pragma: no cover
|
||||
log.debug('do_validation: value: {0}, index: {1}:'
|
||||
' {2}'.format(_value,
|
||||
_index),
|
||||
exc_info=True)
|
||||
err_msg = '{0}'.format(err)
|
||||
if err_msg:
|
||||
msg = _('"{0}" is an invalid {1} for "{2}", {3}'
|
||||
'').format(_value,
|
||||
self._display_name,
|
||||
self.impl_get_display_name(), err_msg)
|
||||
else:
|
||||
msg = _('"{0}" is an invalid {1} for "{2}"'
|
||||
'').format(_value,
|
||||
self._display_name,
|
||||
self.impl_get_display_name())
|
||||
raise ValueError(msg)
|
||||
error = None
|
||||
is_warnings_only = getattr(self, '_warnings_only', False)
|
||||
if ((display_error and not is_warnings_only) or
|
||||
(display_warnings and is_warnings_only)):
|
||||
error = calculation_validator(_value,
|
||||
_index)
|
||||
if not error:
|
||||
error = self._second_level_validation(_value,
|
||||
is_warnings_only)
|
||||
if error:
|
||||
if debug: # pragma: no cover
|
||||
log.debug(_('do_validation for {0}: error in value').format(
|
||||
self.impl_getname()), exc_info=True)
|
||||
if is_warnings_only:
|
||||
msg = _('attention, "{0}" could be an invalid {1} for "{2}", {3}'
|
||||
'').format(_value,
|
||||
self._display_name,
|
||||
self.impl_get_display_name(),
|
||||
error)
|
||||
warnings.warn_explicit(ValueWarning(msg, self),
|
||||
ValueWarning,
|
||||
self.__class__.__name__, 0)
|
||||
error = None
|
||||
if error is None:
|
||||
# if context launch consistency validation
|
||||
#if context is not undefined:
|
||||
ret = self._valid_consistency(current_opt,
|
||||
_value,
|
||||
context,
|
||||
_index,
|
||||
display_warnings,
|
||||
display_error,
|
||||
setting_properties)
|
||||
if isinstance(ret, ValueError):
|
||||
error = ret
|
||||
elif ret:
|
||||
return ret
|
||||
if error:
|
||||
err_msg = '{0}'.format(error)
|
||||
if err_msg:
|
||||
msg = _('"{0}" is an invalid {1} for "{2}", {3}'
|
||||
'').format(_value, self._display_name,
|
||||
self.impl_get_display_name(), err_msg)
|
||||
if isinstance(_value, list): # pragma: no cover
|
||||
raise ValueError(_('invalid value "{}" for "{}" '
|
||||
'which must not be a list').format(_value,
|
||||
self.impl_get_display_name()))
|
||||
is_warnings_only = getattr(self, '_warnings_only', False)
|
||||
try:
|
||||
if _value is not None:
|
||||
if display_error:
|
||||
# option validation
|
||||
self._validate(_value,
|
||||
context,
|
||||
current_opt)
|
||||
if ((display_error and not is_warnings_only) or
|
||||
(display_warnings and is_warnings_only)):
|
||||
calculation_validator(_value,
|
||||
_index)
|
||||
self._second_level_validation(_value,
|
||||
is_warnings_only)
|
||||
self._valid_consistency(current_opt,
|
||||
_value,
|
||||
context,
|
||||
_index,
|
||||
display_warnings,
|
||||
display_error,
|
||||
setting_properties)
|
||||
except ValueError as err:
|
||||
if debug: # pragma: no cover
|
||||
log.debug('do_validation: value: {0}, index: {1}:'
|
||||
' {2}'.format(_value,
|
||||
_index,
|
||||
err),
|
||||
exc_info=True)
|
||||
if is_warnings_only:
|
||||
msg = _('attention, "{0}" could be an invalid {1} for "{2}"'
|
||||
'').format(_value,
|
||||
self._display_name,
|
||||
self.impl_get_display_name())
|
||||
else:
|
||||
msg = _('"{0}" is an invalid {1} for "{2}"'
|
||||
'').format(_value, self._display_name,
|
||||
'').format(_value,
|
||||
self._display_name,
|
||||
self.impl_get_display_name())
|
||||
raise ValueError(msg)
|
||||
err_msg = '{0}'.format(err)
|
||||
if err_msg:
|
||||
msg += ', {}'.format(err_msg)
|
||||
if is_warnings_only:
|
||||
warnings.warn_explicit(ValueWarning(msg, self),
|
||||
ValueWarning,
|
||||
self.__class__.__name__, 0)
|
||||
else:
|
||||
raise ValueError(msg)
|
||||
|
||||
if is_multi is None:
|
||||
is_multi = self.impl_is_multi()
|
||||
|
||||
if not is_multi:
|
||||
return do_validation(value, None)
|
||||
do_validation(value, None)
|
||||
elif force_index is not None:
|
||||
if self.impl_is_submulti():
|
||||
err = _is_not_unique(value)
|
||||
if err:
|
||||
return err
|
||||
if not isinstance(value, list):
|
||||
raise ValueError(_('invalid value "{0}" for "{1}" which'
|
||||
' must be a list').format(
|
||||
value, self.impl_get_display_name()))
|
||||
_is_not_unique(value)
|
||||
for idx, val in enumerate(value):
|
||||
if isinstance(val, list): # pragma: no cover
|
||||
raise ValueError(_('invalid value "{}" for "{}" '
|
||||
'which must not be a list').format(val,
|
||||
self.impl_get_display_name()))
|
||||
err = do_validation(val, force_index)
|
||||
if err:
|
||||
return err
|
||||
do_validation(val,
|
||||
force_index)
|
||||
else:
|
||||
if multi is not None and self.impl_is_unique() and value in multi:
|
||||
if not self.impl_is_submulti() and len(multi) - 1 >= force_index:
|
||||
@ -441,44 +428,37 @@ class Option(OnlyOption):
|
||||
lst = multi
|
||||
if value in lst:
|
||||
raise ValueError(_('invalid value "{}", this value is already'
|
||||
' in "{}"').format(value,
|
||||
self.impl_get_display_name()))
|
||||
return do_validation(value, force_index)
|
||||
' in "{}"').format(value,
|
||||
self.impl_get_display_name()))
|
||||
do_validation(value,
|
||||
force_index)
|
||||
elif not isinstance(value, list):
|
||||
raise ValueError(_('invalid value "{0}" for "{1}" which '
|
||||
'must be a list').format(value,
|
||||
self.impl_getname()))
|
||||
'must be a list').format(value,
|
||||
self.impl_getname()))
|
||||
elif self.impl_is_submulti():
|
||||
for idx, val in enumerate(value):
|
||||
err = _is_not_unique(val)
|
||||
if err:
|
||||
return err
|
||||
_is_not_unique(val)
|
||||
if not isinstance(val, list):
|
||||
raise ValueError(_('invalid value "{0}" for "{1}" '
|
||||
'which must be a list of list'
|
||||
'').format(val,
|
||||
self.impl_getname()))
|
||||
'which must be a list of list'
|
||||
'').format(val,
|
||||
self.impl_getname()))
|
||||
for slave_val in val:
|
||||
err = do_validation(slave_val,
|
||||
idx)
|
||||
if err:
|
||||
return err
|
||||
do_validation(slave_val,
|
||||
idx)
|
||||
else:
|
||||
err = _is_not_unique(value)
|
||||
if err:
|
||||
return err
|
||||
_is_not_unique(value)
|
||||
for idx, val in enumerate(value):
|
||||
err = do_validation(val,
|
||||
idx)
|
||||
if err:
|
||||
return err
|
||||
return self._valid_consistency(current_opt,
|
||||
None,
|
||||
context,
|
||||
None,
|
||||
display_warnings,
|
||||
display_error,
|
||||
setting_properties)
|
||||
do_validation(val,
|
||||
idx)
|
||||
#self._valid_consistency(current_opt,
|
||||
# None,
|
||||
# context,
|
||||
# None,
|
||||
# display_warnings,
|
||||
# display_error,
|
||||
# setting_properties)
|
||||
|
||||
def impl_is_dynsymlinkoption(self):
|
||||
return False
|
||||
@ -634,18 +614,16 @@ class Option(OnlyOption):
|
||||
suffix))
|
||||
else:
|
||||
opts = all_cons_opts
|
||||
err = opts[0]()._launch_consistency(self,
|
||||
func,
|
||||
option,
|
||||
value,
|
||||
context,
|
||||
index,
|
||||
opts,
|
||||
warnings_only,
|
||||
transitive,
|
||||
setting_properties)
|
||||
if err:
|
||||
return err
|
||||
opts[0]()._launch_consistency(self,
|
||||
func,
|
||||
option,
|
||||
value,
|
||||
context,
|
||||
index,
|
||||
opts,
|
||||
warnings_only,
|
||||
transitive,
|
||||
setting_properties)
|
||||
|
||||
def _cons_not_equal(self,
|
||||
current_opt,
|
||||
|
@ -163,11 +163,12 @@ class CacheOptionDescription(BaseOption):
|
||||
raise ConfigError(_('a dynoption ({0}) cannot have '
|
||||
'force_store_value property').format(subpath))
|
||||
if force_store_values and not config._impl_values._p_.hasvalue(subpath, session):
|
||||
value = config.cfgimpl_get_values().get_cached_value(option,
|
||||
path=subpath,
|
||||
validate=False,
|
||||
trusted_cached_properties=False,
|
||||
validate_properties=True)
|
||||
value = impl_build_force_store_values.getvalue(option,
|
||||
subpath,
|
||||
index=None,
|
||||
setting_properties=undefined,
|
||||
self_properties=undefined,
|
||||
validate=False)
|
||||
value_set = True
|
||||
config._impl_values._p_.setvalue(subpath, value,
|
||||
owners.forced, None, session, False)
|
||||
|
Reference in New Issue
Block a user