return true error message when validation
This commit is contained in:
parent
de44daafeb
commit
8056387f02
|
@ -140,9 +140,12 @@ class Option(BaseInformation):
|
||||||
if not self._multi and default_multi is not None:
|
if not self._multi and default_multi is not None:
|
||||||
raise ValueError(_("a default_multi is set whereas multi is False"
|
raise ValueError(_("a default_multi is set whereas multi is False"
|
||||||
" in option: {0}").format(name))
|
" in option: {0}").format(name))
|
||||||
if default_multi is not None and not self._validate(default_multi):
|
if default_multi is not None:
|
||||||
|
try:
|
||||||
|
self._validate(default_multi)
|
||||||
|
except ValueError, err:
|
||||||
raise ValueError(_("invalid default_multi value {0} "
|
raise ValueError(_("invalid default_multi value {0} "
|
||||||
"for option {1}").format(str(default_multi), name))
|
"for option {1}: {2}").format(str(default_multi), name, err))
|
||||||
if callback is not None and (default is not None or default_multi is not None):
|
if callback is not None and (default is not None or default_multi is not None):
|
||||||
raise ValueError(_("defaut values not allowed if option: {0} "
|
raise ValueError(_("defaut values not allowed if option: {0} "
|
||||||
"is calculated").format(name))
|
"is calculated").format(name))
|
||||||
|
@ -253,34 +256,35 @@ class Option(BaseInformation):
|
||||||
if not validate:
|
if not validate:
|
||||||
return
|
return
|
||||||
|
|
||||||
def _val_validator(val):
|
def val_validator(val):
|
||||||
|
if self._validator is not None:
|
||||||
callback_params = deepcopy(self._validator[1])
|
callback_params = deepcopy(self._validator[1])
|
||||||
callback_params.setdefault('', []).insert(0, val)
|
callback_params.setdefault('', []).insert(0, val)
|
||||||
return carry_out_calculation(self._name, config=context,
|
return carry_out_calculation(self._name, config=context,
|
||||||
callback=self._validator[0],
|
callback=self._validator[0],
|
||||||
callback_params=callback_params)
|
callback_params=callback_params)
|
||||||
|
|
||||||
def val_validator():
|
|
||||||
#add current value has first argument
|
|
||||||
if self.impl_is_multi():
|
|
||||||
for val in value:
|
|
||||||
if not _val_validator(val):
|
|
||||||
return False
|
|
||||||
return True
|
|
||||||
else:
|
else:
|
||||||
return _val_validator(value)
|
return True
|
||||||
|
|
||||||
|
def do_validation(_value, _index=None):
|
||||||
|
if _value is None:
|
||||||
|
return True
|
||||||
|
if not val_validator(_value):
|
||||||
|
raise ValueError(_("invalid value {0} for option {1} for object {2}"
|
||||||
|
"").format(_value, self._name, self.__class__))
|
||||||
|
try:
|
||||||
|
self._validate(_value)
|
||||||
|
except ValueError, err:
|
||||||
|
raise ValueError(_("invalid value {0} for option {1}: {2}"
|
||||||
|
"").format(_value, self._name, err))
|
||||||
|
if context is not None:
|
||||||
|
descr._valid_consistency(self, _value, context, _index)
|
||||||
|
|
||||||
# generic calculation
|
# generic calculation
|
||||||
if context is not None:
|
if context is not None:
|
||||||
descr = context.cfgimpl_get_description()
|
descr = context.cfgimpl_get_description()
|
||||||
if not self._multi:
|
if not self._multi:
|
||||||
if value is not None and ((self._validator is not None and
|
do_validation(value)
|
||||||
not val_validator()) or
|
|
||||||
not self._validate(value)):
|
|
||||||
raise ValueError(_("invalid value {0} for option {1} (type {2})"
|
|
||||||
"").format(value, self._name, self.__class__))
|
|
||||||
if context is not None:
|
|
||||||
descr._valid_consistency(self, value, context, None)
|
|
||||||
else:
|
else:
|
||||||
if not isinstance(value, list):
|
if not isinstance(value, list):
|
||||||
raise ValueError(_("invalid value {0} for option {1} "
|
raise ValueError(_("invalid value {0} for option {1} "
|
||||||
|
@ -288,13 +292,7 @@ class Option(BaseInformation):
|
||||||
self._name))
|
self._name))
|
||||||
for index in range(0, len(value)):
|
for index in range(0, len(value)):
|
||||||
val = value[index]
|
val = value[index]
|
||||||
if val is not None and ((self._validator is not None and
|
do_validation(val, index)
|
||||||
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)
|
|
||||||
|
|
||||||
def impl_getdefault(self, default_multi=False):
|
def impl_getdefault(self, default_multi=False):
|
||||||
"accessing the default value"
|
"accessing the default value"
|
||||||
|
@ -398,10 +396,9 @@ class ChoiceOption(Option):
|
||||||
return self._open_values
|
return self._open_values
|
||||||
|
|
||||||
def _validate(self, value):
|
def _validate(self, value):
|
||||||
if not self._open_values:
|
if not self._open_values and not value in self._values:
|
||||||
return value is None or value in self._values
|
raise ValueError(_('value {0} is not permitted, only {1} is allowed'
|
||||||
else:
|
'').format(value, self._values))
|
||||||
return True
|
|
||||||
|
|
||||||
|
|
||||||
class BoolOption(Option):
|
class BoolOption(Option):
|
||||||
|
@ -410,7 +407,8 @@ class BoolOption(Option):
|
||||||
_opt_type = 'bool'
|
_opt_type = 'bool'
|
||||||
|
|
||||||
def _validate(self, value):
|
def _validate(self, value):
|
||||||
return isinstance(value, bool)
|
if not isinstance(value, bool):
|
||||||
|
raise ValueError(_('value must be a boolean'))
|
||||||
|
|
||||||
|
|
||||||
class IntOption(Option):
|
class IntOption(Option):
|
||||||
|
@ -419,7 +417,8 @@ class IntOption(Option):
|
||||||
_opt_type = 'int'
|
_opt_type = 'int'
|
||||||
|
|
||||||
def _validate(self, value):
|
def _validate(self, value):
|
||||||
return isinstance(value, int)
|
if not isinstance(value, int):
|
||||||
|
raise ValueError(_('value must be an integer'))
|
||||||
|
|
||||||
|
|
||||||
class FloatOption(Option):
|
class FloatOption(Option):
|
||||||
|
@ -428,7 +427,8 @@ class FloatOption(Option):
|
||||||
_opt_type = 'float'
|
_opt_type = 'float'
|
||||||
|
|
||||||
def _validate(self, value):
|
def _validate(self, value):
|
||||||
return isinstance(value, float)
|
if not isinstance(value, float):
|
||||||
|
raise ValueError(_('value must be a float'))
|
||||||
|
|
||||||
|
|
||||||
class StrOption(Option):
|
class StrOption(Option):
|
||||||
|
@ -437,7 +437,8 @@ class StrOption(Option):
|
||||||
_opt_type = 'string'
|
_opt_type = 'string'
|
||||||
|
|
||||||
def _validate(self, value):
|
def _validate(self, value):
|
||||||
return isinstance(value, str)
|
if not isinstance(value, str):
|
||||||
|
raise ValueError(_('value must be a string'))
|
||||||
|
|
||||||
|
|
||||||
class UnicodeOption(Option):
|
class UnicodeOption(Option):
|
||||||
|
@ -447,7 +448,8 @@ class UnicodeOption(Option):
|
||||||
_empty = u''
|
_empty = u''
|
||||||
|
|
||||||
def _validate(self, value):
|
def _validate(self, value):
|
||||||
return isinstance(value, unicode)
|
if not isinstance(value, unicode):
|
||||||
|
raise ValueError(_('value must be an unicode'))
|
||||||
|
|
||||||
|
|
||||||
class SymLinkOption(object):
|
class SymLinkOption(object):
|
||||||
|
@ -490,15 +492,11 @@ class IPOption(Option):
|
||||||
properties=properties)
|
properties=properties)
|
||||||
|
|
||||||
def _validate(self, value):
|
def _validate(self, value):
|
||||||
try:
|
|
||||||
ip = IP('{0}/32'.format(value))
|
ip = IP('{0}/32'.format(value))
|
||||||
if ip.iptype() == 'RESERVED':
|
if ip.iptype() == 'RESERVED':
|
||||||
return False
|
raise ValueError(_("IP mustn't not be in reserved class"))
|
||||||
if self._only_private:
|
if self._only_private and not ip.iptype() == 'PRIVATE':
|
||||||
return ip.iptype() == 'PRIVATE'
|
raise ValueError(_("IP must be in private class"))
|
||||||
return True
|
|
||||||
except ValueError:
|
|
||||||
return False
|
|
||||||
|
|
||||||
|
|
||||||
class PortOption(Option):
|
class PortOption(Option):
|
||||||
|
@ -552,23 +550,19 @@ class PortOption(Option):
|
||||||
properties=properties)
|
properties=properties)
|
||||||
|
|
||||||
def _validate(self, value):
|
def _validate(self, value):
|
||||||
try:
|
|
||||||
if self._allow_range and ":" in str(value):
|
if self._allow_range and ":" in str(value):
|
||||||
value = str(value).split(':')
|
value = str(value).split(':')
|
||||||
if len(value) != 2:
|
if len(value) != 2:
|
||||||
return False
|
raise ValueError('range must have two values only')
|
||||||
if not value[0] < value[1]:
|
if not value[0] < value[1]:
|
||||||
return False
|
raise ValueError('first port in range must be smaller than the second one')
|
||||||
else:
|
else:
|
||||||
value = [value]
|
value = [value]
|
||||||
|
|
||||||
for val in value:
|
for val in value:
|
||||||
if not self._min_value <= int(val) <= self._max_value:
|
if not self._min_value <= int(val) <= self._max_value:
|
||||||
return False
|
raise ValueError('port must be an between {0} and {1}'
|
||||||
|
''.format(self._min_value, self._max_value))
|
||||||
return True
|
|
||||||
except ValueError:
|
|
||||||
return False
|
|
||||||
|
|
||||||
|
|
||||||
class NetworkOption(Option):
|
class NetworkOption(Option):
|
||||||
|
@ -577,13 +571,9 @@ class NetworkOption(Option):
|
||||||
_opt_type = 'network'
|
_opt_type = 'network'
|
||||||
|
|
||||||
def _validate(self, value):
|
def _validate(self, value):
|
||||||
try:
|
|
||||||
ip = IP(value)
|
ip = IP(value)
|
||||||
if ip.iptype() == 'RESERVED':
|
if ip.iptype() == 'RESERVED':
|
||||||
return False
|
raise ValueError(_("network mustn't not be in reserved class"))
|
||||||
return True
|
|
||||||
except ValueError:
|
|
||||||
return False
|
|
||||||
|
|
||||||
|
|
||||||
class NetmaskOption(Option):
|
class NetmaskOption(Option):
|
||||||
|
@ -592,11 +582,7 @@ class NetmaskOption(Option):
|
||||||
_opt_type = 'netmask'
|
_opt_type = 'netmask'
|
||||||
|
|
||||||
def _validate(self, value):
|
def _validate(self, value):
|
||||||
try:
|
|
||||||
IP('0.0.0.0/{0}'.format(value))
|
IP('0.0.0.0/{0}'.format(value))
|
||||||
return True
|
|
||||||
except ValueError:
|
|
||||||
return False
|
|
||||||
|
|
||||||
def _cons_network_netmask(self, optname, value, value_):
|
def _cons_network_netmask(self, optname, value, value_):
|
||||||
#opts must be (netmask, network) options
|
#opts must be (netmask, network) options
|
||||||
|
@ -669,7 +655,7 @@ class DomainnameOption(Option):
|
||||||
if self._allow_ip is True:
|
if self._allow_ip is True:
|
||||||
try:
|
try:
|
||||||
IP('{0}/32'.format(value))
|
IP('{0}/32'.format(value))
|
||||||
return True
|
return
|
||||||
except ValueError:
|
except ValueError:
|
||||||
pass
|
pass
|
||||||
if self._type == 'netbios':
|
if self._type == 'netbios':
|
||||||
|
@ -688,7 +674,8 @@ class DomainnameOption(Option):
|
||||||
raise ValueError(_("invalid value's length for {0} (max {1})"
|
raise ValueError(_("invalid value's length for {0} (max {1})"
|
||||||
"").format(self._name, length))
|
"").format(self._name, length))
|
||||||
regexp = r'^[a-z]([a-z\d{0}-])*[a-z\d]$'.format(extrachar)
|
regexp = r'^[a-z]([a-z\d{0}-])*[a-z\d]$'.format(extrachar)
|
||||||
return re.match(regexp, value) is not None
|
if re.match(regexp, value) is None:
|
||||||
|
raise ValueError(_('invalid domainname'))
|
||||||
|
|
||||||
|
|
||||||
class OptionDescription(BaseInformation):
|
class OptionDescription(BaseInformation):
|
||||||
|
@ -896,6 +883,7 @@ def validate_requires_arg(requires, name):
|
||||||
|
|
||||||
for require in requires:
|
for require in requires:
|
||||||
if not type(require) == dict:
|
if not type(require) == dict:
|
||||||
|
print require
|
||||||
raise ValueError(_("malformed requirements type for option:"
|
raise ValueError(_("malformed requirements type for option:"
|
||||||
" {0}, must be a dict").format(name))
|
" {0}, must be a dict").format(name))
|
||||||
valid_keys = ('option', 'expected', 'action', 'inverse', 'transitive',
|
valid_keys = ('option', 'expected', 'action', 'inverse', 'transitive',
|
||||||
|
@ -929,14 +917,18 @@ def validate_requires_arg(requires, name):
|
||||||
' same_action must be boolean'))
|
' same_action must be boolean'))
|
||||||
|
|
||||||
if not isinstance(option, Option):
|
if not isinstance(option, Option):
|
||||||
raise ValueError(_('malformed requirements first argument '
|
print option, type(option)
|
||||||
|
raise ValueError(_('malformed requirements '
|
||||||
'must be an option in option {0}').format(name))
|
'must be an option in option {0}').format(name))
|
||||||
if option.impl_is_multi():
|
if option.impl_is_multi():
|
||||||
raise ValueError(_('malformed requirements option {0} '
|
raise ValueError(_('malformed requirements option {0} '
|
||||||
'should not be a multi').format(name))
|
'should not be a multi').format(name))
|
||||||
if expected is not None and not option._validate(expected):
|
if expected is not None:
|
||||||
|
try:
|
||||||
|
option._validate(expected)
|
||||||
|
except ValueError, err:
|
||||||
raise ValueError(_('malformed requirements second argument '
|
raise ValueError(_('malformed requirements second argument '
|
||||||
'must be valid for option {0}').format(name))
|
'must be valid for option {0}: {1}').format(name, err))
|
||||||
if action in config_action:
|
if action in config_action:
|
||||||
if inverse != config_action[action]:
|
if inverse != config_action[action]:
|
||||||
raise ValueError(_("inconsistency in action types for option: {0}"
|
raise ValueError(_("inconsistency in action types for option: {0}"
|
||||||
|
|
|
@ -361,10 +361,13 @@ class Multi(list):
|
||||||
super(Multi, self).extend(iterable)
|
super(Multi, self).extend(iterable)
|
||||||
|
|
||||||
def _validate(self, value):
|
def _validate(self, value):
|
||||||
if value is not None and not self.opt._validate(value):
|
if value is not None:
|
||||||
|
try:
|
||||||
|
self.opt._validate(value)
|
||||||
|
except ValueError, err:
|
||||||
raise ValueError(_("invalid value {0} "
|
raise ValueError(_("invalid value {0} "
|
||||||
"for option {1}").format(str(value),
|
"for option {1}: {2}").format(str(value),
|
||||||
self.opt._name))
|
self.opt._name, err))
|
||||||
|
|
||||||
def pop(self, key, force=False):
|
def pop(self, key, force=False):
|
||||||
"""the list value can be updated (poped)
|
"""the list value can be updated (poped)
|
||||||
|
|
Loading…
Reference in New Issue