multi, None and validation

This commit is contained in:
2013-05-10 22:32:42 +02:00
parent bd489346a9
commit 536eb27904
5 changed files with 75 additions and 30 deletions

View File

@ -239,6 +239,9 @@ class Option(BaseInformation):
:param value: the option's value
:param validate: if true enables ``self._validator`` validation
"""
if not validate:
return
def _val_validator(val):
callback_params = deepcopy(self._validator[1])
callback_params.setdefault('', []).insert(0, val)
@ -255,32 +258,32 @@ class Option(BaseInformation):
return True
else:
return _val_validator(value)
# generic calculation
if context is not None:
descr = context.cfgimpl_get_description()
if validate:
if not self._multi:
if value is not None and ((self._validator is not None and
not val_validator()) or
not self._validate(value)):
raise ValueError(_("invalid value {0} for option {1}"
"").format(value, self._name))
if not self._multi:
if value is not None and ((self._validator is not None and
not val_validator()) or
not self._validate(value)):
raise ValueError(_("invalid value {0} for option {1}"
"").format(value, self._name))
if context is not None:
descr._valid_consistency(self, value, context, None)
else:
if not isinstance(value, list):
raise ValueError(_("invalid value {0} for option {1} "
"which must be a list").format(value,
self._name))
for index in range(0, len(value)):
val = value[index]
if val is not None and ((self._validator is not None and
not val_validator()) or
not self._validate(val)):
raise ValueError(_("invalid value {0} for option {1}"
"").format(value, self._name))
if context is not None:
descr._valid_consistency(self, value, context, None)
else:
if not isinstance(value, list):
raise ValueError(_("invalid value {0} for option {1} "
"which must be a list").format(value,
self._name))
for index in range(0, len(value)):
val = value[index]
if val is not None and ((self._validator is not None and
not val_validator()) or
not self._validate(val)):
raise ValueError(_("invalid value {0} for option {1}"
"").format(value, self._name))
if context is not None:
descr._valid_consistency(self, val, context, index)
descr._valid_consistency(self, val, context, index)
def impl_getdefault(self, default_multi=False):
"accessing the default value"
@ -330,10 +333,12 @@ class Option(BaseInformation):
raise ValueError('consistency must be set with an option')
if self is opt:
raise ValueError('cannot add consistency with itself')
if self.impl_is_multi() != opt.impl_is_multi():
raise ValueError('options in consistency should be multi in two sides')
func = '_cons_{}'.format(func)
self._launch_consistency(func, self, self.impl_getdefault(), None, None, opt)
self._consistencies.append((func, opt))
self.impl_validate(self.impl_getdefault(), None)
self.impl_validate(self.impl_getdefault())
def _cons_not_equal(self, optname, value, value_):
if value == value_:

View File

@ -178,7 +178,7 @@ class Setting(object):
def __init__(self, context):
# properties attribute: the name of a property enables this property
# key is None for global properties
self._properties = {None: set(('expire',))}
self._properties = {None: set(('expire', 'validator'))}
# permissive properties
self._permissives = {}
# generic owner

View File

@ -46,13 +46,13 @@ class Values(object):
else:
return opt.impl_getdefault()
def _get_value(self, opt):
def _get_value(self, opt, validate=True):
"return value or default value if not set"
#if no value
if opt not in self._values:
value = self._get_default(opt)
if opt.impl_is_multi():
value = Multi(value, self.context, opt)
value = Multi(value, self.context, opt, validate)
else:
#if value
value = self._values[opt][1]
@ -64,7 +64,8 @@ class Values(object):
def _reset(self, opt):
if opt in self._values:
setting = self.context.cfgimpl_get_settings()
opt.impl_validate(opt.impl_getdefault(), self.context, 'validator' in setting)
opt.impl_validate(opt.impl_getdefault(), self.context,
'validator' in setting)
self.context.cfgimpl_reset_cache()
del(self._values[opt])
@ -106,7 +107,7 @@ class Values(object):
def _getitem(self, opt, validate, force_permissive, force_properties):
# options with callbacks
setting = self.context.cfgimpl_get_settings()
value = self._get_value(opt)
value = self._get_value(opt, validate)
is_frozen = 'frozen' in setting[opt]
if opt.impl_has_callback():
#if value is set and :