VirtualMailManager/EmailAddress.py
branchv0.6.x
changeset 199 0684790fff7c
parent 195 05dd49fc3ea1
child 208 efa1327b721f
equal deleted inserted replaced
198:02d467e4fbab 199:0684790fff7c
     6     VirtualMailManager.EmailAddress
     6     VirtualMailManager.EmailAddress
     7 
     7 
     8     Virtual Mail Manager's EmailAddress class to handle e-mail addresses.
     8     Virtual Mail Manager's EmailAddress class to handle e-mail addresses.
     9 """
     9 """
    10 
    10 
    11 import re
    11 from VirtualMailManager import check_domainname, check_localpart
    12 
       
    13 from VirtualMailManager import chk_domainname
       
    14 from VirtualMailManager.constants.ERROR import \
    12 from VirtualMailManager.constants.ERROR import \
    15      DOMAIN_NO_NAME, INVALID_ADDRESS, LOCALPART_INVALID, LOCALPART_TOO_LONG
    13      DOMAIN_NO_NAME, INVALID_ADDRESS, LOCALPART_INVALID
    16 from VirtualMailManager.Exceptions import VMMEmailAddressException as VMMEAE
    14 from VirtualMailManager.Exceptions import VMMEmailAddressException as VMMEAE
    17 
    15 
    18 
    16 
    19 RE_LOCALPART = """[^\w!#$%&'\*\+-\.\/=?^_`{\|}~]"""
    17 RE_LOCALPART = """[^\w!#$%&'\*\+-\.\/=?^_`{\|}~]"""
       
    18 _ = lambda msg: msg
    20 
    19 
    21 
    20 
    22 class EmailAddress(object):
    21 class EmailAddress(object):
    23     """Simple class for validated e-mail addresses."""
    22     """Simple class for validated e-mail addresses."""
    24     __slots__ = ('_localpart', '_domainname')
    23     __slots__ = ('_localpart', '_domainname')
    56 
    55 
    57     def __repr__(self):
    56     def __repr__(self):
    58         return "EmailAddress('%s@%s')" % (self._localpart, self._domainname)
    57         return "EmailAddress('%s@%s')" % (self._localpart, self._domainname)
    59 
    58 
    60     def __str__(self):
    59     def __str__(self):
    61         return "%s@%s" % (self._localpart, self._domainname)
    60         return '%s@%s' % (self._localpart, self._domainname)
    62 
    61 
    63     def _chk_address(self, address):
    62     def _chk_address(self, address):
    64         """Checks if the string ``address`` could be used for an e-mail
    63         """Checks if the string ``address`` could be used for an e-mail
    65         address."""
    64         address. If so, it will assign the corresponding values to the
       
    65         attributes `_localpart` and `_domainname`."""
    66         parts = address.split('@')
    66         parts = address.split('@')
    67         p_len = len(parts)
    67         p_len = len(parts)
    68         if p_len is 2:
    68         if p_len < 2:
    69             self._localpart = check_localpart(parts[0])
    69             raise VMMEAE(_(u"Missing the '@' sign in address %r") % address,
    70             if len(parts[1]) > 0:
    70                          INVALID_ADDRESS)
    71                 self._domainname = chk_domainname(parts[1])
       
    72             else:
       
    73                 raise VMMEAE(_(u"Missing domain name after “%s@”.") %
       
    74                              self._localpart, DOMAIN_NO_NAME)
       
    75         elif p_len < 2:
       
    76             raise VMMEAE(_(u"Missing '@' sign in e-mail address “%s”.") %
       
    77                          address, INVALID_ADDRESS)
       
    78         elif p_len > 2:
    71         elif p_len > 2:
    79             raise VMMEAE(_(u"Too many '@' signs in e-mail address “%s”.") %
    72             raise VMMEAE(_(u"Too many '@' signs in address %r") % address,
    80                          address, INVALID_ADDRESS)
    73                          INVALID_ADDRESS)
       
    74         if not parts[0]:
       
    75             raise VMMEAE(_(u"Missing local-part in address %r") % address,
       
    76                          LOCALPART_INVALID)
       
    77         if not parts[1]:
       
    78             raise VMMEAE(_(u"Missing domain name in address %r") % address,
       
    79                          DOMAIN_NO_NAME)
       
    80         self._localpart = check_localpart(parts[0])
       
    81         self._domainname = check_domainname(parts[1])
    81 
    82 
    82 
    83 
    83 _ = lambda msg: msg
       
    84 
       
    85 
       
    86 def check_localpart(localpart):
       
    87     """Validates the local-part of an e-mail address.
       
    88 
       
    89     Argument:
       
    90     localpart -- local-part of the e-mail address that should be validated
       
    91     """
       
    92     if len(localpart) < 1:
       
    93         raise VMMEAE(_(u'No local-part specified.'), LOCALPART_INVALID)
       
    94     if len(localpart) > 64:
       
    95         raise VMMEAE(_(u'The local-part “%s” is too long') % localpart,
       
    96                      LOCALPART_TOO_LONG)
       
    97     invalid_chars = set(re.findall(RE_LOCALPART, localpart))
       
    98     if invalid_chars:
       
    99         i_chrs = u''.join((u'“%s” ' % c for c in invalid_chars))
       
   100         raise VMMEAE(_(u"The local-part “%(l_part)s” contains invalid\
       
   101  characters: %(i_chrs)s") % {'l_part': localpart, 'i_chrs': i_chrs},
       
   102                      LOCALPART_INVALID)
       
   103     return localpart
       
   104 
       
   105 del _
    84 del _