VirtualMailManager/EmailAddress.py
branchv0.6.x
changeset 199 0684790fff7c
parent 195 05dd49fc3ea1
child 208 efa1327b721f
--- a/VirtualMailManager/EmailAddress.py	Wed Feb 10 08:55:51 2010 +0000
+++ b/VirtualMailManager/EmailAddress.py	Thu Feb 11 03:08:11 2010 +0000
@@ -8,15 +8,14 @@
     Virtual Mail Manager's EmailAddress class to handle e-mail addresses.
 """
 
-import re
-
-from VirtualMailManager import chk_domainname
+from VirtualMailManager import check_domainname, check_localpart
 from VirtualMailManager.constants.ERROR import \
-     DOMAIN_NO_NAME, INVALID_ADDRESS, LOCALPART_INVALID, LOCALPART_TOO_LONG
+     DOMAIN_NO_NAME, INVALID_ADDRESS, LOCALPART_INVALID
 from VirtualMailManager.Exceptions import VMMEmailAddressException as VMMEAE
 
 
 RE_LOCALPART = """[^\w!#$%&'\*\+-\.\/=?^_`{\|}~]"""
+_ = lambda msg: msg
 
 
 class EmailAddress(object):
@@ -58,48 +57,28 @@
         return "EmailAddress('%s@%s')" % (self._localpart, self._domainname)
 
     def __str__(self):
-        return "%s@%s" % (self._localpart, self._domainname)
+        return '%s@%s' % (self._localpart, self._domainname)
 
     def _chk_address(self, address):
         """Checks if the string ``address`` could be used for an e-mail
-        address."""
+        address. If so, it will assign the corresponding values to the
+        attributes `_localpart` and `_domainname`."""
         parts = address.split('@')
         p_len = len(parts)
-        if p_len is 2:
-            self._localpart = check_localpart(parts[0])
-            if len(parts[1]) > 0:
-                self._domainname = chk_domainname(parts[1])
-            else:
-                raise VMMEAE(_(u"Missing domain name after “%s@”.") %
-                             self._localpart, DOMAIN_NO_NAME)
-        elif p_len < 2:
-            raise VMMEAE(_(u"Missing '@' sign in e-mail address “%s”.") %
-                         address, INVALID_ADDRESS)
+        if p_len < 2:
+            raise VMMEAE(_(u"Missing the '@' sign in address %r") % address,
+                         INVALID_ADDRESS)
         elif p_len > 2:
-            raise VMMEAE(_(u"Too many '@' signs in e-mail address “%s”.") %
-                         address, INVALID_ADDRESS)
-
-
-_ = lambda msg: msg
+            raise VMMEAE(_(u"Too many '@' signs in address %r") % address,
+                         INVALID_ADDRESS)
+        if not parts[0]:
+            raise VMMEAE(_(u"Missing local-part in address %r") % address,
+                         LOCALPART_INVALID)
+        if not parts[1]:
+            raise VMMEAE(_(u"Missing domain name in address %r") % address,
+                         DOMAIN_NO_NAME)
+        self._localpart = check_localpart(parts[0])
+        self._domainname = check_domainname(parts[1])
 
 
-def check_localpart(localpart):
-    """Validates the local-part of an e-mail address.
-
-    Argument:
-    localpart -- local-part of the e-mail address that should be validated
-    """
-    if len(localpart) < 1:
-        raise VMMEAE(_(u'No local-part specified.'), LOCALPART_INVALID)
-    if len(localpart) > 64:
-        raise VMMEAE(_(u'The local-part “%s” is too long') % localpart,
-                     LOCALPART_TOO_LONG)
-    invalid_chars = set(re.findall(RE_LOCALPART, localpart))
-    if invalid_chars:
-        i_chrs = u''.join((u'“%s” ' % c for c in invalid_chars))
-        raise VMMEAE(_(u"The local-part “%(l_part)s” contains invalid\
- characters: %(i_chrs)s") % {'l_part': localpart, 'i_chrs': i_chrs},
-                     LOCALPART_INVALID)
-    return localpart
-
 del _