VMM: moved check_localpart() to the EmailAddress module. v0.6.x
authorPascal Volk <neverseen@users.sourceforge.net>
Fri, 16 Apr 2010 03:02:34 +0000
branchv0.6.x
changeset 253 58d1b6f41664
parent 252 af555e6967c8
child 254 8aecc83a0d32
VMM: moved check_localpart() to the EmailAddress module.
VirtualMailManager/EmailAddress.py
VirtualMailManager/__init__.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 _
--- 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 _