|
1 # -*- coding: UTF-8 -*- |
|
2 # Copyright (c) 2008 - 2012, Pascal Volk |
|
3 # See COPYING for distribution information. |
|
4 """ |
|
5 VirtualMailManager.transport |
|
6 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|
7 |
|
8 Virtual Mail Manager's Transport class to manage the transport for |
|
9 domains and accounts. |
|
10 """ |
|
11 |
|
12 from VirtualMailManager.pycompat import any |
|
13 |
|
14 _ = lambda msg: msg |
|
15 |
|
16 |
|
17 class Transport(object): |
|
18 """A wrapper class that provides access to the transport table""" |
|
19 __slots__ = ('_tid', '_transport', '_dbh') |
|
20 |
|
21 def __init__(self, dbh, tid=None, transport=None): |
|
22 """Creates a new Transport instance. |
|
23 |
|
24 Either tid or transport must be specified. When both arguments |
|
25 are given, tid will be used. |
|
26 |
|
27 Keyword arguments: |
|
28 dbh -- a pyPgSQL.PgSQL.connection |
|
29 tid -- the id of a transport (int/long) |
|
30 transport -- the value of the transport (str) |
|
31 |
|
32 """ |
|
33 self._dbh = dbh |
|
34 self._tid = 0 |
|
35 assert any((tid, transport)) |
|
36 if tid: |
|
37 assert not isinstance(tid, bool) and isinstance(tid, (int, long)) |
|
38 self._load_by_id(tid) |
|
39 else: |
|
40 assert isinstance(transport, basestring) |
|
41 self._transport = transport |
|
42 self._load_by_name() |
|
43 |
|
44 @property |
|
45 def tid(self): |
|
46 """The transport's unique ID.""" |
|
47 return self._tid |
|
48 |
|
49 @property |
|
50 def transport(self): |
|
51 """The transport's value, ex: 'dovecot:'""" |
|
52 return self._transport |
|
53 |
|
54 def __eq__(self, other): |
|
55 if isinstance(other, self.__class__): |
|
56 return self._tid == other._tid |
|
57 return NotImplemented |
|
58 |
|
59 def __ne__(self, other): |
|
60 if isinstance(other, self.__class__): |
|
61 return self._tid != other._tid |
|
62 return NotImplemented |
|
63 |
|
64 def __str__(self): |
|
65 return self._transport |
|
66 |
|
67 def _load_by_id(self, tid): |
|
68 """load a transport by its id from the database""" |
|
69 dbc = self._dbh.cursor() |
|
70 dbc.execute('SELECT transport FROM transport WHERE tid = %s', (tid,)) |
|
71 result = dbc.fetchone() |
|
72 dbc.close() |
|
73 if not result: |
|
74 raise ValueError('Unknown transport id specified: %r' % tid) |
|
75 self._transport = result[0] |
|
76 self._tid = tid |
|
77 |
|
78 def _load_by_name(self): |
|
79 """Load a transport by its transport name from the database.""" |
|
80 dbc = self._dbh.cursor() |
|
81 dbc.execute('SELECT tid FROM transport WHERE transport = %s', |
|
82 (self._transport,)) |
|
83 result = dbc.fetchone() |
|
84 dbc.close() |
|
85 if result: |
|
86 self._tid = result[0] |
|
87 else: |
|
88 self._save() |
|
89 |
|
90 def _save(self): |
|
91 """Save the new transport in the database.""" |
|
92 dbc = self._dbh.cursor() |
|
93 dbc.execute("SELECT nextval('transport_id')") |
|
94 self._tid = dbc.fetchone()[0] |
|
95 dbc.execute('INSERT INTO transport (tid, transport) VALUES (%s, %s)', |
|
96 (self._tid, self._transport)) |
|
97 self._dbh.commit() |
|
98 dbc.close() |
|
99 |
|
100 del _ |