author | Pascal Volk <neverseen@users.sourceforge.net> |
Mon, 08 Feb 2010 03:14:59 +0000 | |
branch | v0.6.x |
changeset 193 | a259bdeaab5c |
parent 185 | 6e1ef32fbd82 |
child 194 | 6c06edb5b2d2 |
permissions | -rw-r--r-- |
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 |
|
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
5 |
"""Virtual Mail Manager's EmailAddress class to handle e-mail addresses.""" |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
6 |
|
185
6e1ef32fbd82
VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
162
diff
changeset
|
7 |
import re |
6e1ef32fbd82
VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
162
diff
changeset
|
8 |
|
6e1ef32fbd82
VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
162
diff
changeset
|
9 |
from VirtualMailManager import chk_domainname |
193
a259bdeaab5c
VMM/EmailAddress: rework EmailAddress class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
185
diff
changeset
|
10 |
from VirtualMailManager.constants.ERROR import \ |
a259bdeaab5c
VMM/EmailAddress: rework EmailAddress class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
185
diff
changeset
|
11 |
DOMAIN_NO_NAME, INVALID_ADDRESS, LOCALPART_INVALID, LOCALPART_TOO_LONG |
185
6e1ef32fbd82
VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
162
diff
changeset
|
12 |
from VirtualMailManager.Exceptions import VMMEmailAddressException as VMMEAE |
76
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
13 |
|
193
a259bdeaab5c
VMM/EmailAddress: rework EmailAddress class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
185
diff
changeset
|
14 |
|
76
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
15 |
RE_LOCALPART = """[^\w!#$%&'\*\+-\.\/=?^_`{\|}~]""" |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
16 |
|
193
a259bdeaab5c
VMM/EmailAddress: rework EmailAddress class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
185
diff
changeset
|
17 |
|
76
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
18 |
class EmailAddress(object): |
108
fb88585f17fe
VMM.EmailAddress: Added __slots__
Pascal Volk <neverseen@users.sourceforge.net>
parents:
102
diff
changeset
|
19 |
__slots__ = ('_localpart', '_domainname') |
193
a259bdeaab5c
VMM/EmailAddress: rework EmailAddress class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
185
diff
changeset
|
20 |
|
76
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
21 |
def __init__(self, address): |
193
a259bdeaab5c
VMM/EmailAddress: rework EmailAddress class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
185
diff
changeset
|
22 |
if not isinstance(address, basestring): |
a259bdeaab5c
VMM/EmailAddress: rework EmailAddress class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
185
diff
changeset
|
23 |
raise TypeError('address is not a str/unicode object: %r' % |
a259bdeaab5c
VMM/EmailAddress: rework EmailAddress class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
185
diff
changeset
|
24 |
address) |
76
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
25 |
self._localpart = None |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
26 |
self._domainname = None |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
27 |
self.__chkAddress(address) |
116
949c5db6447a
Fixed a logical mistake in EmailAddress.__ne__() (not used),
Pascal Volk <neverseen@users.sourceforge.net>
parents:
108
diff
changeset
|
28 |
|
76
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
29 |
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
|
30 |
if isinstance(other, self.__class__): |
193
a259bdeaab5c
VMM/EmailAddress: rework EmailAddress class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
185
diff
changeset
|
31 |
return self._localpart == other._localpart and \ |
a259bdeaab5c
VMM/EmailAddress: rework EmailAddress class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
185
diff
changeset
|
32 |
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
|
33 |
return NotImplemented |
76
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
34 |
|
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
35 |
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
|
36 |
if isinstance(other, self.__class__): |
193
a259bdeaab5c
VMM/EmailAddress: rework EmailAddress class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
185
diff
changeset
|
37 |
return self._localpart != other._localpart or \ |
a259bdeaab5c
VMM/EmailAddress: rework EmailAddress class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
185
diff
changeset
|
38 |
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
|
39 |
return NotImplemented |
76
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
40 |
|
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
41 |
def __repr__(self): |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
42 |
return "EmailAddress('%s@%s')" % (self._localpart, self._domainname) |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
43 |
|
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
44 |
def __str__(self): |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
45 |
return "%s@%s" % (self._localpart, self._domainname) |
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 __chkAddress(self, address): |
193
a259bdeaab5c
VMM/EmailAddress: rework EmailAddress class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
185
diff
changeset
|
48 |
parts = address.split('@') |
a259bdeaab5c
VMM/EmailAddress: rework EmailAddress class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
185
diff
changeset
|
49 |
p_len = len(parts) |
a259bdeaab5c
VMM/EmailAddress: rework EmailAddress class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
185
diff
changeset
|
50 |
if p_len is 2: |
a259bdeaab5c
VMM/EmailAddress: rework EmailAddress class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
185
diff
changeset
|
51 |
self._localpart = self.__chkLocalpart(parts[0]) |
a259bdeaab5c
VMM/EmailAddress: rework EmailAddress class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
185
diff
changeset
|
52 |
if len(parts[1]) > 0: |
a259bdeaab5c
VMM/EmailAddress: rework EmailAddress class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
185
diff
changeset
|
53 |
self._domainname = chk_domainname(parts[1]) |
a259bdeaab5c
VMM/EmailAddress: rework EmailAddress class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
185
diff
changeset
|
54 |
else: |
a259bdeaab5c
VMM/EmailAddress: rework EmailAddress class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
185
diff
changeset
|
55 |
raise VMMEAE(_(u"Missing domain name after “%s@”.") % |
a259bdeaab5c
VMM/EmailAddress: rework EmailAddress class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
185
diff
changeset
|
56 |
self._localpart, DOMAIN_NO_NAME) |
a259bdeaab5c
VMM/EmailAddress: rework EmailAddress class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
185
diff
changeset
|
57 |
elif p_len < 2: |
133
2d5c4745efec
Replaced angle quotes by quotation marks.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
116
diff
changeset
|
58 |
raise VMMEAE(_(u"Missing '@' sign in e-mail address “%s”.") % |
193
a259bdeaab5c
VMM/EmailAddress: rework EmailAddress class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
185
diff
changeset
|
59 |
address, INVALID_ADDRESS) |
a259bdeaab5c
VMM/EmailAddress: rework EmailAddress class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
185
diff
changeset
|
60 |
elif p_len > 2: |
a259bdeaab5c
VMM/EmailAddress: rework EmailAddress class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
185
diff
changeset
|
61 |
raise VMMEAE(_(u"Too many '@' signs in e-mail address “%s”.") % |
a259bdeaab5c
VMM/EmailAddress: rework EmailAddress class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
185
diff
changeset
|
62 |
address, INVALID_ADDRESS) |
76
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
63 |
|
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
64 |
def __chkLocalpart(self, localpart): |
155
eb866ebb9f2e
Fixed some grammar errors and typos. (Reported by Clytie Siddall)
Pascal Volk <neverseen@users.sourceforge.net>
parents:
138
diff
changeset
|
65 |
"""Validates the local-part of an e-mail address. |
116
949c5db6447a
Fixed a logical mistake in EmailAddress.__ne__() (not used),
Pascal Volk <neverseen@users.sourceforge.net>
parents:
108
diff
changeset
|
66 |
|
155
eb866ebb9f2e
Fixed some grammar errors and typos. (Reported by Clytie Siddall)
Pascal Volk <neverseen@users.sourceforge.net>
parents:
138
diff
changeset
|
67 |
Arguments: |
193
a259bdeaab5c
VMM/EmailAddress: rework EmailAddress class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
185
diff
changeset
|
68 |
localpart -- local-part of the e-mail address that should be validated |
76
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
69 |
""" |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
70 |
if len(localpart) < 1: |
193
a259bdeaab5c
VMM/EmailAddress: rework EmailAddress class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
185
diff
changeset
|
71 |
raise VMMEAE(_(u'No local-part specified.'), LOCALPART_INVALID) |
76
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
72 |
if len(localpart) > 64: |
193
a259bdeaab5c
VMM/EmailAddress: rework EmailAddress class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
185
diff
changeset
|
73 |
raise VMMEAE(_(u'The local-part “%s” is too long') % localpart, |
a259bdeaab5c
VMM/EmailAddress: rework EmailAddress class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
185
diff
changeset
|
74 |
LOCALPART_TOO_LONG) |
87
f9090d1a0730
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
76
diff
changeset
|
75 |
ic = set(re.findall(RE_LOCALPART, localpart)) |
76
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
76 |
if len(ic): |
193
a259bdeaab5c
VMM/EmailAddress: rework EmailAddress class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
185
diff
changeset
|
77 |
ichrs = u''.join((u'“%s” ' % c for c in ic)) |
155
eb866ebb9f2e
Fixed some grammar errors and typos. (Reported by Clytie Siddall)
Pascal Volk <neverseen@users.sourceforge.net>
parents:
138
diff
changeset
|
78 |
raise VMMEAE(_(u"The local-part “%(lpart)s” contains invalid\ |
76
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
79 |
characters: %(ichrs)s") % {'lpart': localpart, 'ichrs': ichrs}, |
193
a259bdeaab5c
VMM/EmailAddress: rework EmailAddress class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
185
diff
changeset
|
80 |
LOCALPART_INVALID) |
76
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
81 |
return localpart |