VirtualMailManager/emailaddress.py
branchv0.7.x
changeset 643 df1e3b67882a
parent 568 14abdd04ddf5
child 673 de046a3b29a5
equal deleted inserted replaced
642:4cd9d0a9f42f 643:df1e3b67882a
    24     """Simple class for validated e-mail addresses."""
    24     """Simple class for validated e-mail addresses."""
    25     __slots__ = ('_localpart', '_domainname')
    25     __slots__ = ('_localpart', '_domainname')
    26 
    26 
    27     def __init__(self, address, _validate=True):
    27     def __init__(self, address, _validate=True):
    28         """Creates a new instance from the string/unicode ``address``."""
    28         """Creates a new instance from the string/unicode ``address``."""
    29         assert isinstance(address, basestring)
    29         assert isinstance(address, str)
    30         self._localpart = None
    30         self._localpart = None
    31         self._domainname = None
    31         self._domainname = None
    32         if _validate:
    32         if _validate:
    33             self._chk_address(address)
    33             self._chk_address(address)
    34 
    34 
    68         address.  If so, it will assign the corresponding values to the
    68         address.  If so, it will assign the corresponding values to the
    69         attributes `_localpart` and `_domainname`."""
    69         attributes `_localpart` and `_domainname`."""
    70         parts = address.split('@')
    70         parts = address.split('@')
    71         p_len = len(parts)
    71         p_len = len(parts)
    72         if p_len < 2:
    72         if p_len < 2:
    73             raise EAErr(_(u"Missing the '@' sign in address: '%s'") % address,
    73             raise EAErr(_("Missing the '@' sign in address: '%s'") % address,
    74                         INVALID_ADDRESS)
    74                         INVALID_ADDRESS)
    75         elif p_len > 2:
    75         elif p_len > 2:
    76             raise EAErr(_(u"Too many '@' signs in address: '%s'") % address,
    76             raise EAErr(_("Too many '@' signs in address: '%s'") % address,
    77                         INVALID_ADDRESS)
    77                         INVALID_ADDRESS)
    78         if not parts[0]:
    78         if not parts[0]:
    79             raise EAErr(_(u"Missing local-part in address: '%s'") % address,
    79             raise EAErr(_("Missing local-part in address: '%s'") % address,
    80                         LOCALPART_INVALID)
    80                         LOCALPART_INVALID)
    81         if not parts[1]:
    81         if not parts[1]:
    82             raise EAErr(_(u"Missing domain name in address: '%s'") % address,
    82             raise EAErr(_("Missing domain name in address: '%s'") % address,
    83                         DOMAIN_NO_NAME)
    83                         DOMAIN_NO_NAME)
    84         self._localpart = check_localpart(parts[0])
    84         self._localpart = check_localpart(parts[0])
    85         self._domainname = check_domainname(parts[1])
    85         self._domainname = check_domainname(parts[1])
    86 
    86 
    87 
    87 
   103         super(DestinationEmailAddress, self).__init__(address, _validate)
   103         super(DestinationEmailAddress, self).__init__(address, _validate)
   104         self._localhost = False
   104         self._localhost = False
   105         if not _validate:
   105         if not _validate:
   106             try:
   106             try:
   107                 self._chk_address(address)
   107                 self._chk_address(address)
   108             except DomainError, err:
   108             except DomainError as err:
   109                 if err.code is DOMAIN_INVALID and \
   109                 if err.code is DOMAIN_INVALID and \
   110                    address.split('@')[1] == 'localhost':
   110                    address.split('@')[1] == 'localhost':
   111                     self._localhost = True
   111                     self._localhost = True
   112                     self._domainname = 'localhost'
   112                     self._domainname = 'localhost'
   113                 else:
   113                 else:
   140 
   140 
   141     Throws a `EmailAddressError` if the local-part is too long or contains
   141     Throws a `EmailAddressError` if the local-part is too long or contains
   142     invalid characters.
   142     invalid characters.
   143     """
   143     """
   144     if len(localpart) > 64:
   144     if len(localpart) > 64:
   145         raise EAErr(_(u"The local-part '%s' is too long.") % localpart,
   145         raise EAErr(_("The local-part '%s' is too long.") % localpart,
   146                     LOCALPART_TOO_LONG)
   146                     LOCALPART_TOO_LONG)
   147     invalid_chars = set(RE_LOCALPART.findall(localpart))
   147     invalid_chars = set(RE_LOCALPART.findall(localpart))
   148     if invalid_chars:
   148     if invalid_chars:
   149         i_chars = u''.join((u'"%s" ' % c for c in invalid_chars))
   149         i_chars = ''.join(('"%s" ' % c for c in invalid_chars))
   150         raise EAErr(_(u"The local-part '%(l_part)s' contains invalid "
   150         raise EAErr(_("The local-part '%(l_part)s' contains invalid "
   151                       u"characters: %(i_chars)s") % {'l_part': localpart,
   151                       "characters: %(i_chars)s") % {'l_part': localpart,
   152                     'i_chars': i_chars}, LOCALPART_INVALID)
   152                     'i_chars': i_chars}, LOCALPART_INVALID)
   153     return localpart
   153     return localpart
   154 
   154 
   155 del _
   155 del _