# HG changeset patch # User Pascal Volk # Date 1271386954 0 # Node ID 58d1b6f41664e4509186c799cfab5a3de34e987c # Parent af555e6967c8a316e1aec48ab5d5f6a11f2dd138 VMM: moved check_localpart() to the EmailAddress module. diff -r af555e6967c8 -r 58d1b6f41664 VirtualMailManager/EmailAddress.py --- a/VirtualMailManager/EmailAddress.py Thu Apr 15 23:25:24 2010 +0000 +++ b/VirtualMailManager/EmailAddress.py Fri Apr 16 03:02:34 2010 +0000 @@ -7,13 +7,15 @@ Virtual Mail Manager's EmailAddress class to handle e-mail addresses. """ +import re -from VirtualMailManager import check_domainname, check_localpart +from VirtualMailManager import check_domainname from VirtualMailManager.constants.ERROR import \ - DOMAIN_NO_NAME, INVALID_ADDRESS, LOCALPART_INVALID + DOMAIN_NO_NAME, INVALID_ADDRESS, LOCALPART_INVALID, LOCALPART_TOO_LONG from VirtualMailManager.errors import EmailAddressError as EAErr +RE_LOCALPART = re.compile(r"[^\w!#$%&'\*\+-\.\/=?^_`{\|}~]") _ = lambda msg: msg @@ -81,4 +83,23 @@ self._domainname = check_domainname(parts[1]) +def check_localpart(localpart): + """Returns the validated local-part `localpart`. + + Throws a `EmailAddressError` if the local-part is too long or contains + invalid characters. + """ + if len(localpart) > 64: + raise EAErr(_(u"The local-part '%s' is too long") % localpart, + LOCALPART_TOO_LONG) + invalid_chars = set(RE_LOCALPART.findall(localpart)) + if invalid_chars: + i_chars = u''.join((u'"%s" ' % c for c in invalid_chars)) + raise EAErr(_(u"The local-part '%(l_part)s' contains invalid \ +characters: %(i_chars)s") % + {'l_part': localpart, 'i_chars': i_chars}, + LOCALPART_INVALID) + return localpart + + del _ diff -r af555e6967c8 -r 58d1b6f41664 VirtualMailManager/__init__.py --- a/VirtualMailManager/__init__.py Thu Apr 15 23:25:24 2010 +0000 +++ b/VirtualMailManager/__init__.py Fri Apr 16 03:02:34 2010 +0000 @@ -16,8 +16,8 @@ from encodings.idna import ToASCII, ToUnicode from VirtualMailManager.constants.ERROR import \ - DOMAIN_INVALID, DOMAIN_TOO_LONG, LOCALPART_INVALID, LOCALPART_TOO_LONG, \ - NOT_EXECUTABLE, NO_SUCH_BINARY, NO_SUCH_DIRECTORY + DOMAIN_INVALID, DOMAIN_TOO_LONG, NOT_EXECUTABLE, NO_SUCH_BINARY, \ + NO_SUCH_DIRECTORY from VirtualMailManager.constants.version import __author__, __date__, \ __version__ from VirtualMailManager.errors import VMMError @@ -27,8 +27,8 @@ # version information from VERSION '__author__', '__date__', '__version__', # defined stuff - 'ENCODING', 'ace2idna', 'check_domainname', 'check_localpart', 'exec_ok', - 'expand_path', 'get_unicode', 'idn2ascii', 'is_dir', + 'ENCODING', 'ace2idna', 'check_domainname', 'exec_ok', 'expand_path', + 'get_unicode', 'idn2ascii', 'is_dir', ] @@ -42,7 +42,6 @@ # there may be many domain and e-mail address checks RE_DOMAIN = re.compile(r"^(?:[a-z0-9-]{1,63}\.){1,}[a-z]{2,6}$") -RE_LOCALPART = re.compile(r"[^\w!#$%&'\*\+-\.\/=?^_`{\|}~]") gettext.install('vmm', '/usr/local/share/locale', unicode=1) @@ -126,24 +125,4 @@ return domainname -def check_localpart(localpart): - """Returns the validated local-part `localpart`. - - Throws a `VMMError` if the local-part is too long or contains - invalid characters. - - """ - if len(localpart) > 64: - raise VMMError(_(u"The local-part '%s' is too long") % localpart, - LOCALPART_TOO_LONG) - invalid_chars = set(RE_LOCALPART.findall(localpart)) - if invalid_chars: - i_chars = u''.join((u'"%s" ' % c for c in invalid_chars)) - raise VMMError(_(u"The local-part '%(l_part)s' contains invalid \ -characters: %(i_chars)s") % - {'l_part': localpart, 'i_chars': i_chars}, - LOCALPART_INVALID) - return localpart - - del _