VirtualMailManager/EmailAddress.py
branchv0.6.x
changeset 195 05dd49fc3ea1
parent 194 6c06edb5b2d2
child 199 0684790fff7c
equal deleted inserted replaced
194:6c06edb5b2d2 195:05dd49fc3ea1
    19 RE_LOCALPART = """[^\w!#$%&'\*\+-\.\/=?^_`{\|}~]"""
    19 RE_LOCALPART = """[^\w!#$%&'\*\+-\.\/=?^_`{\|}~]"""
    20 
    20 
    21 
    21 
    22 class EmailAddress(object):
    22 class EmailAddress(object):
    23     """Simple class for validated e-mail addresses."""
    23     """Simple class for validated e-mail addresses."""
    24     __slots__ = ('localpart', 'domainname')
    24     __slots__ = ('_localpart', '_domainname')
    25 
    25 
    26     def __init__(self, address):
    26     def __init__(self, address):
    27         """Creates a new instance from the string/unicode ``address``."""
    27         """Creates a new instance from the string/unicode ``address``."""
    28         if not isinstance(address, basestring):
    28         if not isinstance(address, basestring):
    29             raise TypeError('address is not a str/unicode object: %r' %
    29             raise TypeError('address is not a str/unicode object: %r' %
    30                             address)
    30                             address)
    31         self.localpart = None
    31         self._localpart = None
    32         self.domainname = None
    32         self._domainname = None
    33         self._chk_address(address)
    33         self._chk_address(address)
       
    34 
       
    35     @property
       
    36     def localpart(self):
       
    37         """The local-part of the address *local-part@domain*"""
       
    38         return self._localpart
       
    39 
       
    40     @property
       
    41     def domainname(self):
       
    42         """The domain part of the address *local-part@domain*"""
       
    43         return self._domainname
    34 
    44 
    35     def __eq__(self, other):
    45     def __eq__(self, other):
    36         if isinstance(other, self.__class__):
    46         if isinstance(other, self.__class__):
    37             return self.localpart == other.localpart and \
    47             return self._localpart == other.localpart and \
    38                     self.domainname == other.domainname
    48                     self._domainname == other.domainname
    39         return NotImplemented
    49         return NotImplemented
    40 
    50 
    41     def __ne__(self, other):
    51     def __ne__(self, other):
    42         if isinstance(other, self.__class__):
    52         if isinstance(other, self.__class__):
    43             return self.localpart != other.localpart or \
    53             return self._localpart != other.localpart or \
    44                     self.domainname != other.domainname
    54                     self._domainname != other.domainname
    45         return NotImplemented
    55         return NotImplemented
    46 
    56 
    47     def __repr__(self):
    57     def __repr__(self):
    48         return "EmailAddress('%s@%s')" % (self.localpart, self.domainname)
    58         return "EmailAddress('%s@%s')" % (self._localpart, self._domainname)
    49 
    59 
    50     def __str__(self):
    60     def __str__(self):
    51         return "%s@%s" % (self.localpart, self.domainname)
    61         return "%s@%s" % (self._localpart, self._domainname)
    52 
    62 
    53     def _chk_address(self, address):
    63     def _chk_address(self, address):
    54         """Checks if the string ``address`` could be used for an e-mail
    64         """Checks if the string ``address`` could be used for an e-mail
    55         address."""
    65         address."""
    56         parts = address.split('@')
    66         parts = address.split('@')
    57         p_len = len(parts)
    67         p_len = len(parts)
    58         if p_len is 2:
    68         if p_len is 2:
    59             self.localpart = check_localpart(parts[0])
    69             self._localpart = check_localpart(parts[0])
    60             if len(parts[1]) > 0:
    70             if len(parts[1]) > 0:
    61                 self.domainname = chk_domainname(parts[1])
    71                 self._domainname = chk_domainname(parts[1])
    62             else:
    72             else:
    63                 raise VMMEAE(_(u"Missing domain name after “%s@”.") %
    73                 raise VMMEAE(_(u"Missing domain name after “%s@”.") %
    64                              self.localpart, DOMAIN_NO_NAME)
    74                              self._localpart, DOMAIN_NO_NAME)
    65         elif p_len < 2:
    75         elif p_len < 2:
    66             raise VMMEAE(_(u"Missing '@' sign in e-mail address “%s”.") %
    76             raise VMMEAE(_(u"Missing '@' sign in e-mail address “%s”.") %
    67                          address, INVALID_ADDRESS)
    77                          address, INVALID_ADDRESS)
    68         elif p_len > 2:
    78         elif p_len > 2:
    69             raise VMMEAE(_(u"Too many '@' signs in e-mail address “%s”.") %
    79             raise VMMEAE(_(u"Too many '@' signs in e-mail address “%s”.") %