update invalid's message and display all informations when raises

This commit is contained in:
2013-09-30 22:50:49 +02:00
parent cce080cbd3
commit c8a20eefb5
3 changed files with 401 additions and 320 deletions

View File

@@ -421,7 +421,11 @@ class Option(BaseOption):
if _value is None:
return
# option validation
self._validate(_value)
try:
self._validate(_value)
except ValueError as err:
raise ValueError(_('invalid value for option {0}: {1}'
'').format(self._name, err.message))
try:
# valid with self._validator
val_validator(_value)
@@ -430,8 +434,8 @@ class Option(BaseOption):
descr._valid_consistency(self, _value, context, _index)
self._second_level_validation(_value)
except ValueError as err:
msg = _("invalid value {0} for option {1}: {2}").format(
_value, self._name, err)
msg = _("invalid value for option {0}: {1}").format(
self._name, err.message)
if self._warnings_only:
warnings.warn_explicit(ValueWarning(msg, self),
ValueWarning,
@@ -677,7 +681,7 @@ class BoolOption(Option):
def _validate(self, value):
if not isinstance(value, bool):
raise ValueError(_('value must be a boolean'))
raise ValueError(_('invalid boolean'))
class IntOption(Option):
@@ -687,7 +691,7 @@ class IntOption(Option):
def _validate(self, value):
if not isinstance(value, int):
raise ValueError(_('value must be an integer'))
raise ValueError(_('invalid integer'))
class FloatOption(Option):
@@ -697,7 +701,7 @@ class FloatOption(Option):
def _validate(self, value):
if not isinstance(value, float):
raise ValueError(_('value must be a float'))
raise ValueError(_('invalid float'))
class StrOption(Option):
@@ -707,8 +711,7 @@ class StrOption(Option):
def _validate(self, value):
if not isinstance(value, str):
raise ValueError(_('value must be a string, not '
'{0}').format(type(value)))
raise ValueError(_('invalid string'))
if sys.version_info[0] >= 3:
@@ -725,7 +728,7 @@ else:
def _validate(self, value):
if not isinstance(value, unicode):
raise ValueError(_('value must be an unicode'))
raise ValueError(_('invalid unicode'))
class SymLinkOption(BaseOption):
@@ -786,14 +789,14 @@ class IPOption(Option):
try:
IP('{0}/32'.format(value))
except ValueError:
raise ValueError(_('invalid IP {0}').format(self._name))
raise ValueError(_('invalid IP'))
def _second_level_validation(self, value):
ip = IP('{0}/32'.format(value))
if not self._allow_reserved and ip.iptype() == 'RESERVED':
raise ValueError(_("IP mustn't not be in reserved class"))
raise ValueError(_("invalid IP, mustn't not be in reserved class"))
if self._private_only and not ip.iptype() == 'PRIVATE':
raise ValueError(_("IP must be in private class"))
raise ValueError(_("invalid IP, must be in private class"))
class PortOption(Option):
@@ -853,16 +856,17 @@ class PortOption(Option):
if self._allow_range and ":" in str(value):
value = str(value).split(':')
if len(value) != 2:
raise ValueError('range must have two values only')
raise ValueError('invalid part, range must have two values '
'only')
if not value[0] < value[1]:
raise ValueError('first port in range must be'
raise ValueError('invalid port, first port in range must be'
' smaller than the second one')
else:
value = [value]
for val in value:
if not self._min_value <= int(val) <= self._max_value:
raise ValueError('port must be an between {0} and {1}'
raise ValueError('invalid port, must be an between {0} and {1}'
''.format(self._min_value, self._max_value))
@@ -875,12 +879,12 @@ class NetworkOption(Option):
try:
IP(value)
except ValueError:
raise ValueError(_('invalid network address {0}').format(self._name))
raise ValueError(_('invalid network address'))
def _second_level_validation(self, value):
ip = IP(value)
if ip.iptype() == 'RESERVED':
raise ValueError(_("network shall not be in reserved class"))
raise ValueError(_("invalid network address, must not be in reserved class"))
class NetmaskOption(Option):
@@ -892,7 +896,7 @@ class NetmaskOption(Option):
try:
IP('0.0.0.0/{0}'.format(value))
except ValueError:
raise ValueError(_('invalid netmask address {0}').format(self._name))
raise ValueError(_('invalid netmask address'))
def _cons_network_netmask(self, opts, vals):
#opts must be (netmask, network) options
@@ -930,12 +934,12 @@ class NetmaskOption(Option):
except ValueError:
if make_net:
msg = _("invalid IP {0} ({1}) with netmask {2} ({3})")
msg = _('invalid IP {0} ({1}) with netmask {2}')
else:
msg = _("invalid network {0} ({1}) with netmask {2} ({3})")
msg = _('invalid network {0} ({1}) with netmask {2}')
if msg is not None:
raise ValueError(msg.format(val_ipnetwork, opts[1]._name,
val_netmask, self._name))
val_netmask))
class BroadcastOption(Option):
@@ -946,7 +950,7 @@ class BroadcastOption(Option):
try:
IP('{0}/32'.format(value))
except ValueError:
raise ValueError(_('invalid broadcast address {0}').format(self._name))
raise ValueError(_('invalid broadcast address'))
def _cons_broadcast(self, opts, vals):
if len(vals) != 3:
@@ -1017,16 +1021,13 @@ class DomainnameOption(Option):
pass
if self._type == 'domainname' and not self._allow_without_dot and \
'.' not in value:
raise ValueError(_("invalid domainname for {0}, must have dot"
"").format(self._name))
raise ValueError(_("invalid domainname, must have dot"))
if len(value) > 255:
raise ValueError(_("invalid domainname's length for"
" {0} (max 255)").format(self._name))
raise ValueError(_("invalid domainname's length (max 255)"))
if len(value) < 2:
raise ValueError(_("invalid domainname's length for {0} (min 2)"
"").format(self._name))
raise ValueError(_("invalid domainname's length (min 2)"))
if not self._domain_re.search(value):
raise ValueError(_('invalid domainname: {0}'.format(self._name)))
raise ValueError(_('invalid domainname'))
class EmailOption(DomainnameOption):
@@ -1045,10 +1046,10 @@ class EmailOption(DomainnameOption):
try:
username, domain = splitted
except ValueError:
raise ValueError(_('invalid email address, should contains one @ '
'for {0}').format(self._name))
raise ValueError(_('invalid email address, should contains one @'
))
if not self.username_re.search(username):
raise ValueError(_('invalid username in email address for {0}').format(self._name))
raise ValueError(_('invalid username in email address'))
super(EmailOption, self)._validate(domain)
@@ -1068,7 +1069,7 @@ class URLOption(DomainnameOption):
match = self.proto_re.search(value)
if not match:
raise ValueError(_('invalid url, should start with http:// or '
'https:// for {0}').format(self._name))
'https://'))
value = value[len(match.group(0)):]
# get domain/files
splitted = value.split('/', 1)
@@ -1086,13 +1087,13 @@ class URLOption(DomainnameOption):
domain = splitted[0]
port = 0
if not 0 <= int(port) <= 65535:
raise ValueError(_('port must be an between 0 and 65536'))
raise ValueError(_('invalid url, port must be an between 0 and '
'65536'))
# validate domainname
super(URLOption, self)._validate(domain)
# validate file
if files is not None and files != '' and not self.path_re.search(files):
raise ValueError(_('invalid url, should endswith with filename for'
' {0}').format(self._name))
raise ValueError(_('invalid url, should ends with filename'))
class FileOption(Option):
@@ -1103,7 +1104,7 @@ class FileOption(Option):
def _validate(self, value):
match = self.path_re.search(value)
if not match:
raise ValueError(_('invalid filename for {0}').format(self._name))
raise ValueError(_('invalid filename'))
class OptionDescription(BaseOption):