VirtualMailManager/EmailAddress.py
author Pascal Volk <neverseen@users.sourceforge.net>
Sat, 27 Feb 2010 09:28:33 +0000
branchv0.6.x
changeset 218 84094c7fa28b
parent 216 0c8c053b451c
child 253 58d1b6f41664
permissions -rw-r--r--
VMM/EmailAddress: implemented EmailAddress.__hash__(). So we can have a set() of EmailAddress instances.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
76
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     1
# -*- coding: UTF-8 -*-
162
0ac9ef587769 Updated copyright notices to include the year 2010.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 155
diff changeset
     2
# Copyright (c) 2008 - 2010, Pascal Volk
76
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     3
# See COPYING for distribution information.
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     4
194
6c06edb5b2d2 VMM/EmailAddress: reworked once more.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 193
diff changeset
     5
"""
6c06edb5b2d2 VMM/EmailAddress: reworked once more.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 193
diff changeset
     6
    VirtualMailManager.EmailAddress
6c06edb5b2d2 VMM/EmailAddress: reworked once more.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 193
diff changeset
     7
6c06edb5b2d2 VMM/EmailAddress: reworked once more.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 193
diff changeset
     8
    Virtual Mail Manager's EmailAddress class to handle e-mail addresses.
6c06edb5b2d2 VMM/EmailAddress: reworked once more.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 193
diff changeset
     9
"""
76
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    10
199
0684790fff7c VMM: renamed function chk_domainname() -> check_domainname().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 195
diff changeset
    11
from VirtualMailManager import check_domainname, check_localpart
193
a259bdeaab5c VMM/EmailAddress: rework EmailAddress class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    12
from VirtualMailManager.constants.ERROR import \
199
0684790fff7c VMM: renamed function chk_domainname() -> check_domainname().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 195
diff changeset
    13
     DOMAIN_NO_NAME, INVALID_ADDRESS, LOCALPART_INVALID
216
0c8c053b451c Moved VirtualMailManager/Exceptions to VirtualMailManager/errors.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 215
diff changeset
    14
from VirtualMailManager.errors import EmailAddressError as EAErr
76
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    15
193
a259bdeaab5c VMM/EmailAddress: rework EmailAddress class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    16
199
0684790fff7c VMM: renamed function chk_domainname() -> check_domainname().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 195
diff changeset
    17
_ = lambda msg: msg
76
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    18
193
a259bdeaab5c VMM/EmailAddress: rework EmailAddress class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    19
76
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    20
class EmailAddress(object):
194
6c06edb5b2d2 VMM/EmailAddress: reworked once more.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 193
diff changeset
    21
    """Simple class for validated e-mail addresses."""
195
05dd49fc3ea1 VMM/EmailAddress: reworked class EmailAddress again.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 194
diff changeset
    22
    __slots__ = ('_localpart', '_domainname')
193
a259bdeaab5c VMM/EmailAddress: rework EmailAddress class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    23
76
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    24
    def __init__(self, address):
194
6c06edb5b2d2 VMM/EmailAddress: reworked once more.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 193
diff changeset
    25
        """Creates a new instance from the string/unicode ``address``."""
213
1a9fee6b93bc VMM:/{Alias,EmailAddress,Relocated}: use assertions for argument checks.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 208
diff changeset
    26
        assert isinstance(address, basestring)
195
05dd49fc3ea1 VMM/EmailAddress: reworked class EmailAddress again.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 194
diff changeset
    27
        self._localpart = None
05dd49fc3ea1 VMM/EmailAddress: reworked class EmailAddress again.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 194
diff changeset
    28
        self._domainname = None
194
6c06edb5b2d2 VMM/EmailAddress: reworked once more.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 193
diff changeset
    29
        self._chk_address(address)
116
949c5db6447a Fixed a logical mistake in EmailAddress.__ne__() (not used),
Pascal Volk <neverseen@users.sourceforge.net>
parents: 108
diff changeset
    30
195
05dd49fc3ea1 VMM/EmailAddress: reworked class EmailAddress again.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 194
diff changeset
    31
    @property
05dd49fc3ea1 VMM/EmailAddress: reworked class EmailAddress again.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 194
diff changeset
    32
    def localpart(self):
05dd49fc3ea1 VMM/EmailAddress: reworked class EmailAddress again.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 194
diff changeset
    33
        """The local-part of the address *local-part@domain*"""
05dd49fc3ea1 VMM/EmailAddress: reworked class EmailAddress again.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 194
diff changeset
    34
        return self._localpart
05dd49fc3ea1 VMM/EmailAddress: reworked class EmailAddress again.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 194
diff changeset
    35
05dd49fc3ea1 VMM/EmailAddress: reworked class EmailAddress again.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 194
diff changeset
    36
    @property
05dd49fc3ea1 VMM/EmailAddress: reworked class EmailAddress again.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 194
diff changeset
    37
    def domainname(self):
05dd49fc3ea1 VMM/EmailAddress: reworked class EmailAddress again.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 194
diff changeset
    38
        """The domain part of the address *local-part@domain*"""
05dd49fc3ea1 VMM/EmailAddress: reworked class EmailAddress again.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 194
diff changeset
    39
        return self._domainname
05dd49fc3ea1 VMM/EmailAddress: reworked class EmailAddress again.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 194
diff changeset
    40
76
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    41
    def __eq__(self, other):
116
949c5db6447a Fixed a logical mistake in EmailAddress.__ne__() (not used),
Pascal Volk <neverseen@users.sourceforge.net>
parents: 108
diff changeset
    42
        if isinstance(other, self.__class__):
195
05dd49fc3ea1 VMM/EmailAddress: reworked class EmailAddress again.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 194
diff changeset
    43
            return self._localpart == other.localpart and \
05dd49fc3ea1 VMM/EmailAddress: reworked class EmailAddress again.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 194
diff changeset
    44
                    self._domainname == other.domainname
116
949c5db6447a Fixed a logical mistake in EmailAddress.__ne__() (not used),
Pascal Volk <neverseen@users.sourceforge.net>
parents: 108
diff changeset
    45
        return NotImplemented
76
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    46
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    47
    def __ne__(self, other):
116
949c5db6447a Fixed a logical mistake in EmailAddress.__ne__() (not used),
Pascal Volk <neverseen@users.sourceforge.net>
parents: 108
diff changeset
    48
        if isinstance(other, self.__class__):
195
05dd49fc3ea1 VMM/EmailAddress: reworked class EmailAddress again.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 194
diff changeset
    49
            return self._localpart != other.localpart or \
05dd49fc3ea1 VMM/EmailAddress: reworked class EmailAddress again.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 194
diff changeset
    50
                    self._domainname != other.domainname
116
949c5db6447a Fixed a logical mistake in EmailAddress.__ne__() (not used),
Pascal Volk <neverseen@users.sourceforge.net>
parents: 108
diff changeset
    51
        return NotImplemented
76
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    52
218
84094c7fa28b VMM/EmailAddress: implemented EmailAddress.__hash__().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    53
    def __hash__(self):
84094c7fa28b VMM/EmailAddress: implemented EmailAddress.__hash__().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    54
        return hash((self._localpart.lower(), self._domainname.lower()))
84094c7fa28b VMM/EmailAddress: implemented EmailAddress.__hash__().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    55
76
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    56
    def __repr__(self):
195
05dd49fc3ea1 VMM/EmailAddress: reworked class EmailAddress again.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 194
diff changeset
    57
        return "EmailAddress('%s@%s')" % (self._localpart, self._domainname)
76
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    58
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    59
    def __str__(self):
199
0684790fff7c VMM: renamed function chk_domainname() -> check_domainname().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 195
diff changeset
    60
        return '%s@%s' % (self._localpart, self._domainname)
76
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    61
194
6c06edb5b2d2 VMM/EmailAddress: reworked once more.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 193
diff changeset
    62
    def _chk_address(self, address):
6c06edb5b2d2 VMM/EmailAddress: reworked once more.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 193
diff changeset
    63
        """Checks if the string ``address`` could be used for an e-mail
215
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 213
diff changeset
    64
        address.  If so, it will assign the corresponding values to the
199
0684790fff7c VMM: renamed function chk_domainname() -> check_domainname().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 195
diff changeset
    65
        attributes `_localpart` and `_domainname`."""
193
a259bdeaab5c VMM/EmailAddress: rework EmailAddress class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    66
        parts = address.split('@')
a259bdeaab5c VMM/EmailAddress: rework EmailAddress class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    67
        p_len = len(parts)
199
0684790fff7c VMM: renamed function chk_domainname() -> check_domainname().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 195
diff changeset
    68
        if p_len < 2:
216
0c8c053b451c Moved VirtualMailManager/Exceptions to VirtualMailManager/errors.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 215
diff changeset
    69
            raise EAErr(_(u"Missing the '@' sign in address %r") % address,
0c8c053b451c Moved VirtualMailManager/Exceptions to VirtualMailManager/errors.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 215
diff changeset
    70
                        INVALID_ADDRESS)
193
a259bdeaab5c VMM/EmailAddress: rework EmailAddress class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    71
        elif p_len > 2:
216
0c8c053b451c Moved VirtualMailManager/Exceptions to VirtualMailManager/errors.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 215
diff changeset
    72
            raise EAErr(_(u"Too many '@' signs in address %r") % address,
0c8c053b451c Moved VirtualMailManager/Exceptions to VirtualMailManager/errors.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 215
diff changeset
    73
                        INVALID_ADDRESS)
199
0684790fff7c VMM: renamed function chk_domainname() -> check_domainname().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 195
diff changeset
    74
        if not parts[0]:
216
0c8c053b451c Moved VirtualMailManager/Exceptions to VirtualMailManager/errors.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 215
diff changeset
    75
            raise EAErr(_(u'Missing local-part in address %r') % address,
0c8c053b451c Moved VirtualMailManager/Exceptions to VirtualMailManager/errors.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 215
diff changeset
    76
                        LOCALPART_INVALID)
199
0684790fff7c VMM: renamed function chk_domainname() -> check_domainname().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 195
diff changeset
    77
        if not parts[1]:
216
0c8c053b451c Moved VirtualMailManager/Exceptions to VirtualMailManager/errors.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 215
diff changeset
    78
            raise EAErr(_(u'Missing domain name in address %r') % address,
0c8c053b451c Moved VirtualMailManager/Exceptions to VirtualMailManager/errors.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 215
diff changeset
    79
                        DOMAIN_NO_NAME)
199
0684790fff7c VMM: renamed function chk_domainname() -> check_domainname().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 195
diff changeset
    80
        self._localpart = check_localpart(parts[0])
0684790fff7c VMM: renamed function chk_domainname() -> check_domainname().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 195
diff changeset
    81
        self._domainname = check_domainname(parts[1])
194
6c06edb5b2d2 VMM/EmailAddress: reworked once more.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 193
diff changeset
    82
6c06edb5b2d2 VMM/EmailAddress: reworked once more.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 193
diff changeset
    83
6c06edb5b2d2 VMM/EmailAddress: reworked once more.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 193
diff changeset
    84
del _