pooling EmailOption, UsernameOption and FilenameOption

This commit is contained in:
Emmanuel Garette 2016-10-21 22:56:40 +02:00
parent 4e8d780929
commit a14e156a63
2 changed files with 27 additions and 48 deletions

View File

@ -143,10 +143,11 @@ def test_email():
od = OptionDescription('a', '', [e])
c = Config(od)
c.read_write()
c.e = 'root@foo.com'
raises(ValueError, "c.e = 'root'")
raises(ValueError, "c.e = 'root@domain'")
raises(ValueError, "c.e = 'root[]@domain'")
c.e = u'foo-bar.baz@example.com'
c.e = u'root@foo.com'
c.e = u'root@domain'
raises(ValueError, "c.e = u'root'")
raises(ValueError, "c.e = u'root[]@domain'")
def test_url():

View File

@ -543,30 +543,6 @@ class DomainnameOption(Option):
return _valid_char(value)
class EmailOption(DomainnameOption):
__slots__ = tuple()
username_re = re.compile(r"^[\w!#$%&'*+\-/=?^`{|}~.]+$")
_display_name = _('email address')
def _validate(self, value, context=undefined, current_opt=undefined):
err = self._impl_valid_unicode(value)
if err:
return err
splitted = value.split('@', 1)
if len(splitted) == 1:
return ValueError(_('must contains one @'))
username, domain = value.split('@', 1)
if not self.username_re.search(username):
return ValueError(_('invalid username in email address')) # pragma: optional cover
err = super(EmailOption, self)._validate(domain)
if err:
return err
return super(EmailOption, self)._second_level_validation(domain, False)
def _second_level_validation(self, value, warnings_only):
pass
class URLOption(DomainnameOption):
__slots__ = tuple()
proto_re = re.compile(r'(http|https)://')
@ -614,30 +590,32 @@ class URLOption(DomainnameOption):
pass
class UsernameOption(Option):
class _RegexpOption(Option):
__slots__ = tuple()
def _validate(self, value, context=undefined, current_opt=undefined):
err = self._impl_valid_unicode(value)
if err:
return err
match = self._regexp.search(value)
if not match:
return ValueError()
class EmailOption(_RegexpOption):
__slots__ = tuple()
#https://www.w3.org/TR/html-markup/input.email.html#input.email.attrs.value.single.
_regexp = re.compile(r"^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$")
_display_name = _('email address')
class UsernameOption(_RegexpOption):
__slots__ = tuple()
#regexp build with 'man 8 adduser' informations
username_re = re.compile(r"^[a-z_][a-z0-9_-]{0,30}[$a-z0-9_-]{0,1}$")
_regexp = re.compile(r"^[a-z_][a-z0-9_-]{0,30}[$a-z0-9_-]{0,1}$")
_display_name = _('username')
def _validate(self, value, context=undefined, current_opt=undefined):
err = self._impl_valid_unicode(value)
if err:
return err
match = self.username_re.search(value)
if not match:
return ValueError() # pragma: optional cover
class FilenameOption(Option):
class FilenameOption(_RegexpOption):
__slots__ = tuple()
path_re = re.compile(r"^[a-zA-Z0-9\-\._~/+]+$")
_regexp = re.compile(r"^[a-zA-Z0-9\-\._~/+]+$")
_display_name = _('file name')
def _validate(self, value, context=undefined, current_opt=undefined):
err = self._impl_valid_unicode(value)
if err:
return err
match = self.path_re.search(value)
if not match:
return ValueError('some characters are not allowed') # pragma: optional cover