VMM/EmailAddress: rework EmailAddress class. v0.6.x
authorPascal Volk <neverseen@users.sourceforge.net>
Mon, 08 Feb 2010 03:14:59 +0000
branchv0.6.x
changeset 193 a259bdeaab5c
parent 192 0854fb9f3bc5
child 194 6c06edb5b2d2
VMM/EmailAddress: rework EmailAddress class.
VirtualMailManager/EmailAddress.py
--- a/VirtualMailManager/EmailAddress.py	Sun Feb 07 06:28:35 2010 +0000
+++ b/VirtualMailManager/EmailAddress.py	Mon Feb 08 03:14:59 2010 +0000
@@ -6,29 +6,36 @@
 
 import re
 
-import VirtualMailManager.constants.ERROR as ERR
 from VirtualMailManager import chk_domainname
+from VirtualMailManager.constants.ERROR import \
+     DOMAIN_NO_NAME, INVALID_ADDRESS, LOCALPART_INVALID, LOCALPART_TOO_LONG
 from VirtualMailManager.Exceptions import VMMEmailAddressException as VMMEAE
 
+
 RE_LOCALPART = """[^\w!#$%&'\*\+-\.\/=?^_`{\|}~]"""
 
+
 class EmailAddress(object):
     __slots__ = ('_localpart', '_domainname')
+
     def __init__(self, address):
+        if not isinstance(address, basestring):
+            raise TypeError('address is not a str/unicode object: %r' %
+                            address)
         self._localpart = None
         self._domainname = None
         self.__chkAddress(address)
 
     def __eq__(self, other):
         if isinstance(other, self.__class__):
-            return self._localpart == other._localpart\
-                    and self._domainname == other._domainname
+            return self._localpart == other._localpart and \
+                    self._domainname == other._domainname
         return NotImplemented
 
     def __ne__(self, other):
         if isinstance(other, self.__class__):
-            return self._localpart != other._localpart\
-                    or self._domainname != other._domainname
+            return self._localpart != other._localpart or \
+                    self._domainname != other._domainname
         return NotImplemented
 
     def __repr__(self):
@@ -38,41 +45,37 @@
         return "%s@%s" % (self._localpart, self._domainname)
 
     def __chkAddress(self, address):
-        try:
-            localpart, domain = address.split('@')
-        except ValueError:
+        parts = address.split('@')
+        p_len = len(parts)
+        if p_len is 2:
+            self._localpart = self.__chkLocalpart(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, ERR.INVALID_ADDRESS)
-        except AttributeError:
-            raise VMMEAE(_(u"“%s” doesn't look like an e-mail address.") %
-                         address, ERR.INVALID_ADDRESS)
-        if len(domain) > 0:
-            domain = chk_domainname(domain)
-        else:
-            raise VMMEAE(_(u"Missing domain name after “%s@”.") % localpart,
-                         ERR.DOMAIN_NO_NAME)
-        localpart = self.__chkLocalpart(localpart)
-        self._localpart, self._domainname = localpart, domain
+                         address, INVALID_ADDRESS)
+        elif p_len > 2:
+            raise VMMEAE(_(u"Too many '@' signs in e-mail address “%s”.") %
+                         address, INVALID_ADDRESS)
 
     def __chkLocalpart(self, localpart):
         """Validates the local-part of an e-mail address.
 
         Arguments:
-        localpart -- local-part of the e-mail address that should be validated (str)
+        localpart -- local-part of the e-mail address that should be validated
         """
         if len(localpart) < 1:
-            raise VMMEAE(_(u'No local-part specified.'),
-                ERR.LOCALPART_INVALID)
+            raise VMMEAE(_(u'No local-part specified.'), LOCALPART_INVALID)
         if len(localpart) > 64:
-            raise VMMEAE(_(u'The local-part “%s” is too long') %
-                localpart, ERR.LOCALPART_TOO_LONG)
+            raise VMMEAE(_(u'The local-part “%s” is too long') % localpart,
+                         LOCALPART_TOO_LONG)
         ic = set(re.findall(RE_LOCALPART, localpart))
         if len(ic):
-            ichrs = ''
-            for c in ic:
-                ichrs += u"“%s” " % c
+            ichrs = u''.join((u'“%s” ' % c for c in ic))
             raise VMMEAE(_(u"The local-part “%(lpart)s” contains invalid\
  characters: %(ichrs)s") % {'lpart': localpart, 'ichrs': ichrs},
-                ERR.LOCALPART_INVALID)
+                         LOCALPART_INVALID)
         return localpart
-