valid Option is an unicode or a string if needed

This commit is contained in:
2015-04-19 09:15:18 +02:00
parent 057bba83e4
commit 2b019027be
6 changed files with 233 additions and 204 deletions

View File

@ -387,6 +387,10 @@ class BaseOption(Base):
def impl_getproperties(self):
return self._properties
def _impl_valid_unicode(self, value):
if not isinstance(value, unicode) and not isinstance(value, str):
raise ValueError(_('invalid unicode or string'))
class OnlyOption(BaseOption):
__slots__ = tuple()

View File

@ -172,6 +172,7 @@ class IPOption(Option):
def _validate(self, value, context=undefined):
# sometimes an ip term starts with a zero
# but this does not fit in some case, for example bind does not like it
self._impl_valid_unicode(value)
try:
for val in value.split('.'):
if val.startswith("0") and len(val) > 1:
@ -273,6 +274,9 @@ class PortOption(Option):
extra=extra)
def _validate(self, value, context=undefined):
if isinstance(value, int):
value = unicode(value)
self._impl_valid_unicode(value)
if self._get_extra('_allow_range') and ":" in str(value): # pragma: optional cover
value = str(value).split(':')
if len(value) != 2:
@ -300,6 +304,7 @@ class NetworkOption(Option):
__slots__ = tuple()
def _validate(self, value, context=undefined):
self._impl_valid_unicode(value)
try:
IP(value)
except ValueError: # pragma: optional cover
@ -320,6 +325,7 @@ class NetmaskOption(Option):
__slots__ = tuple()
def _validate(self, value, context=undefined):
self._impl_valid_unicode(value)
try:
IP('0.0.0.0/{0}'.format(value))
except ValueError: # pragma: optional cover
@ -367,6 +373,7 @@ class BroadcastOption(Option):
__slots__ = tuple()
def _validate(self, value, context=undefined):
self._impl_valid_unicode(value)
try:
IP('{0}/32'.format(value))
except ValueError: # pragma: optional cover
@ -424,6 +431,7 @@ class DomainnameOption(Option):
extra=extra)
def _validate(self, value, context=undefined):
self._impl_valid_unicode(value)
def _valid_length(val):
if len(val) < 2:
raise ValueError(_("invalid domainname's length (min 2)"))
@ -479,6 +487,7 @@ class EmailOption(DomainnameOption):
username_re = re.compile(r"^[\w!#$%&'*+\-/=?^`{|}~.]+$")
def _validate(self, value, context=undefined):
self._impl_valid_unicode(value)
splitted = value.split('@', 1)
try:
username, domain = splitted
@ -500,6 +509,7 @@ class URLOption(DomainnameOption):
path_re = re.compile(r"^[a-z0-9\-\._~:/\?#\[\]@!%\$&\'\(\)\*\+,;=]+$")
def _validate(self, value, context=undefined):
self._impl_valid_unicode(value)
match = self.proto_re.search(value)
if not match: # pragma: optional cover
raise ValueError(_('invalid url, must start with http:// or '
@ -540,6 +550,7 @@ class UsernameOption(Option):
username_re = re.compile(r"^[a-z_][a-z0-9_-]{0,30}[$a-z0-9_-]{0,1}$")
def _validate(self, value, context=undefined):
self._impl_valid_unicode(value)
match = self.username_re.search(value)
if not match:
raise ValueError(_('invalid username')) # pragma: optional cover
@ -550,6 +561,7 @@ class FilenameOption(Option):
path_re = re.compile(r"^[a-zA-Z0-9\-\._~/+]+$")
def _validate(self, value, context=undefined):
self._impl_valid_unicode(value)
match = self.path_re.search(value)
if not match:
raise ValueError(_('invalid filename')) # pragma: optional cover