10 """ |
10 """ |
11 |
11 |
12 from VirtualMailManager.constants import UNKNOWN_TRANSPORT_ID |
12 from VirtualMailManager.constants import UNKNOWN_TRANSPORT_ID |
13 from VirtualMailManager.errors import TransportError |
13 from VirtualMailManager.errors import TransportError |
14 from VirtualMailManager.pycompat import any |
14 from VirtualMailManager.pycompat import any |
|
15 |
|
16 _ = lambda msg: msg |
15 |
17 |
16 |
18 |
17 class Transport(object): |
19 class Transport(object): |
18 """A wrapper class that provides access to the transport table""" |
20 """A wrapper class that provides access to the transport table""" |
19 __slots__ = ('_tid', '_transport', '_dbh') |
21 __slots__ = ('_tid', '_transport', '_dbh') |
33 self._dbh = dbh |
35 self._dbh = dbh |
34 assert any((tid, transport)) |
36 assert any((tid, transport)) |
35 if tid: |
37 if tid: |
36 assert not isinstance(tid, bool) and isinstance(tid, (int, long)) |
38 assert not isinstance(tid, bool) and isinstance(tid, (int, long)) |
37 self._tid = tid |
39 self._tid = tid |
38 self._loadByID() |
40 self._load_by_id() |
39 else: |
41 else: |
40 assert isinstance(transport, basestring) |
42 assert isinstance(transport, basestring) |
41 self._transport = transport |
43 self._transport = transport |
42 self._loadByName() |
44 self._load_by_name() |
43 |
45 |
44 @property |
46 @property |
45 def tid(self): |
47 def tid(self): |
46 """The transport's unique ID.""" |
48 """The transport's unique ID.""" |
47 return self._tid |
49 return self._tid |
51 """The transport's value, ex: 'dovecot:'""" |
53 """The transport's value, ex: 'dovecot:'""" |
52 return self._transport |
54 return self._transport |
53 |
55 |
54 def __eq__(self, other): |
56 def __eq__(self, other): |
55 if isinstance(other, self.__class__): |
57 if isinstance(other, self.__class__): |
56 return self._tid == other.tid |
58 return self._tid == other._tid |
57 return NotImplemented |
59 return NotImplemented |
58 |
60 |
59 def __ne__(self, other): |
61 def __ne__(self, other): |
60 if isinstance(other, self.__class__): |
62 if isinstance(other, self.__class__): |
61 return self._tid != other.tid |
63 return self._tid != other._tid |
62 return NotImplemented |
64 return NotImplemented |
63 |
65 |
64 def __str__(self): |
66 def __str__(self): |
65 return self._transport |
67 return self._transport |
66 |
68 |
67 def _loadByID(self): |
69 def _load_by_id(self): |
|
70 """load a transport by its id from the database""" |
68 dbc = self._dbh.cursor() |
71 dbc = self._dbh.cursor() |
69 dbc.execute('SELECT transport FROM transport WHERE tid=%s', self._tid) |
72 dbc.execute('SELECT transport FROM transport WHERE tid=%s', self._tid) |
70 result = dbc.fetchone() |
73 result = dbc.fetchone() |
71 dbc.close() |
74 dbc.close() |
72 if result: |
75 if result: |
73 self._transport = result[0] |
76 self._transport = result[0] |
74 else: |
77 else: |
75 raise TransportError(_(u'Unknown tid specified.'), |
78 raise TransportError(_(u'Unknown tid specified.'), |
76 UNKNOWN_TRANSPORT_ID) |
79 UNKNOWN_TRANSPORT_ID) |
77 |
80 |
78 def _loadByName(self): |
81 def _load_by_name(self): |
|
82 """Load a transport by its transport name from the database.""" |
79 dbc = self._dbh.cursor() |
83 dbc = self._dbh.cursor() |
80 dbc.execute('SELECT tid FROM transport WHERE transport = %s', |
84 dbc.execute('SELECT tid FROM transport WHERE transport = %s', |
81 self._transport) |
85 self._transport) |
82 result = dbc.fetchone() |
86 result = dbc.fetchone() |
83 dbc.close() |
87 dbc.close() |
85 self._tid = result[0] |
89 self._tid = result[0] |
86 else: |
90 else: |
87 self._save() |
91 self._save() |
88 |
92 |
89 def _save(self): |
93 def _save(self): |
|
94 """Save the new transport in the database.""" |
90 dbc = self._dbh.cursor() |
95 dbc = self._dbh.cursor() |
91 dbc.execute("SELECT nextval('transport_id')") |
96 dbc.execute("SELECT nextval('transport_id')") |
92 self._tid = dbc.fetchone()[0] |
97 self._tid = dbc.fetchone()[0] |
93 dbc.execute('INSERT INTO transport VALUES (%s, %s)', self._tid, |
98 dbc.execute('INSERT INTO transport VALUES (%s, %s)', self._tid, |
94 self._transport) |
99 self._transport) |
95 self._dbh.commit() |
100 self._dbh.commit() |
96 dbc.close() |
101 dbc.close() |
|
102 |
|
103 del _ |