author | Pascal Volk <neverseen@users.sourceforge.net> |
Fri, 11 Feb 2011 02:11:15 +0000 | |
branch | v0.6.x |
changeset 393 | fb2ba1456bc5 |
parent 379 | 7518d927d443 |
child 449 | 6826acb9b0a7 |
permissions | -rw-r--r-- |
8
7e3ce56f49e6
* 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
1 |
# -*- coding: UTF-8 -*- |
366
d6573da35b5f
Updated copyright notices to include the year 2011.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
360
diff
changeset
|
2 |
# Copyright (c) 2008 - 2011, Pascal Volk |
8
7e3ce56f49e6
* 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
3 |
# See COPYING for distribution information. |
225
a51809f7940b
VMM/Transport: reworked Transport class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
216
diff
changeset
|
4 |
""" |
320
011066435e6f
VMM/*: Made all modules names lowercase, adjusted imports.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
316
diff
changeset
|
5 |
VirtualMailManager.transport |
308
aa4a9fc31e1b
VMM/Transport: Small cosmetics.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
235
diff
changeset
|
6 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
8
7e3ce56f49e6
* 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
7 |
|
225
a51809f7940b
VMM/Transport: reworked Transport class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
216
diff
changeset
|
8 |
Virtual Mail Manager's Transport class to manage the transport for |
a51809f7940b
VMM/Transport: reworked Transport class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
216
diff
changeset
|
9 |
domains and accounts. |
a51809f7940b
VMM/Transport: reworked Transport class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
216
diff
changeset
|
10 |
""" |
a51809f7940b
VMM/Transport: reworked Transport class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
216
diff
changeset
|
11 |
|
316
31d8931dc535
VMM/constants: Replaced the constants subpackage by a module.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
308
diff
changeset
|
12 |
from VirtualMailManager.constants import UNKNOWN_TRANSPORT_ID |
216
0c8c053b451c
Moved VirtualMailManager/Exceptions to VirtualMailManager/errors.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
185
diff
changeset
|
13 |
from VirtualMailManager.errors import TransportError |
225
a51809f7940b
VMM/Transport: reworked Transport class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
216
diff
changeset
|
14 |
from VirtualMailManager.pycompat import any |
a51809f7940b
VMM/Transport: reworked Transport class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
216
diff
changeset
|
15 |
|
321
883d5cd66498
VMM/transport: Converted _mixedCase method names to
Pascal Volk <neverseen@users.sourceforge.net>
parents:
320
diff
changeset
|
16 |
_ = lambda msg: msg |
883d5cd66498
VMM/transport: Converted _mixedCase method names to
Pascal Volk <neverseen@users.sourceforge.net>
parents:
320
diff
changeset
|
17 |
|
8
7e3ce56f49e6
* 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
18 |
|
110
cb8b2f6a5fca
Transport: converted to new-style class; Domain: query reduction
Pascal Volk <neverseen@users.sourceforge.net>
parents:
102
diff
changeset
|
19 |
class Transport(object): |
cb8b2f6a5fca
Transport: converted to new-style class; Domain: query reduction
Pascal Volk <neverseen@users.sourceforge.net>
parents:
102
diff
changeset
|
20 |
"""A wrapper class that provides access to the transport table""" |
235
9d3405ed08e5
VMM/Transport: Renamed attribute/property {,_}id to {,_}tid.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
225
diff
changeset
|
21 |
__slots__ = ('_tid', '_transport', '_dbh') |
225
a51809f7940b
VMM/Transport: reworked Transport class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
216
diff
changeset
|
22 |
|
8
7e3ce56f49e6
* 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
23 |
def __init__(self, dbh, tid=None, transport=None): |
7e3ce56f49e6
* 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
24 |
"""Creates a new Transport instance. |
7e3ce56f49e6
* 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
25 |
|
225
a51809f7940b
VMM/Transport: reworked Transport class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
216
diff
changeset
|
26 |
Either tid or transport must be specified. When both arguments |
a51809f7940b
VMM/Transport: reworked Transport class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
216
diff
changeset
|
27 |
are given, tid will be used. |
128
cf8116625866
Converted VirtualMailManager and Postconf to new-style classes.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
110
diff
changeset
|
28 |
|
8
7e3ce56f49e6
* 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
29 |
Keyword arguments: |
7e3ce56f49e6
* 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
30 |
dbh -- a pyPgSQL.PgSQL.connection |
225
a51809f7940b
VMM/Transport: reworked Transport class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
216
diff
changeset
|
31 |
tid -- the id of a transport (int/long) |
8
7e3ce56f49e6
* 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
32 |
transport -- the value of the transport (str) |
225
a51809f7940b
VMM/Transport: reworked Transport class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
216
diff
changeset
|
33 |
|
8
7e3ce56f49e6
* 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
34 |
""" |
7e3ce56f49e6
* 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
35 |
self._dbh = dbh |
225
a51809f7940b
VMM/Transport: reworked Transport class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
216
diff
changeset
|
36 |
assert any((tid, transport)) |
a51809f7940b
VMM/Transport: reworked Transport class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
216
diff
changeset
|
37 |
if tid: |
a51809f7940b
VMM/Transport: reworked Transport class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
216
diff
changeset
|
38 |
assert not isinstance(tid, bool) and isinstance(tid, (int, long)) |
235
9d3405ed08e5
VMM/Transport: Renamed attribute/property {,_}id to {,_}tid.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
225
diff
changeset
|
39 |
self._tid = tid |
321
883d5cd66498
VMM/transport: Converted _mixedCase method names to
Pascal Volk <neverseen@users.sourceforge.net>
parents:
320
diff
changeset
|
40 |
self._load_by_id() |
8
7e3ce56f49e6
* 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
41 |
else: |
225
a51809f7940b
VMM/Transport: reworked Transport class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
216
diff
changeset
|
42 |
assert isinstance(transport, basestring) |
a51809f7940b
VMM/Transport: reworked Transport class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
216
diff
changeset
|
43 |
self._transport = transport |
321
883d5cd66498
VMM/transport: Converted _mixedCase method names to
Pascal Volk <neverseen@users.sourceforge.net>
parents:
320
diff
changeset
|
44 |
self._load_by_name() |
8
7e3ce56f49e6
* 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
45 |
|
225
a51809f7940b
VMM/Transport: reworked Transport class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
216
diff
changeset
|
46 |
@property |
235
9d3405ed08e5
VMM/Transport: Renamed attribute/property {,_}id to {,_}tid.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
225
diff
changeset
|
47 |
def tid(self): |
225
a51809f7940b
VMM/Transport: reworked Transport class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
216
diff
changeset
|
48 |
"""The transport's unique ID.""" |
235
9d3405ed08e5
VMM/Transport: Renamed attribute/property {,_}id to {,_}tid.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
225
diff
changeset
|
49 |
return self._tid |
225
a51809f7940b
VMM/Transport: reworked Transport class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
216
diff
changeset
|
50 |
|
a51809f7940b
VMM/Transport: reworked Transport class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
216
diff
changeset
|
51 |
@property |
a51809f7940b
VMM/Transport: reworked Transport class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
216
diff
changeset
|
52 |
def transport(self): |
a51809f7940b
VMM/Transport: reworked Transport class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
216
diff
changeset
|
53 |
"""The transport's value, ex: 'dovecot:'""" |
a51809f7940b
VMM/Transport: reworked Transport class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
216
diff
changeset
|
54 |
return self._transport |
a51809f7940b
VMM/Transport: reworked Transport class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
216
diff
changeset
|
55 |
|
110
cb8b2f6a5fca
Transport: converted to new-style class; Domain: query reduction
Pascal Volk <neverseen@users.sourceforge.net>
parents:
102
diff
changeset
|
56 |
def __eq__(self, other): |
cb8b2f6a5fca
Transport: converted to new-style class; Domain: query reduction
Pascal Volk <neverseen@users.sourceforge.net>
parents:
102
diff
changeset
|
57 |
if isinstance(other, self.__class__): |
321
883d5cd66498
VMM/transport: Converted _mixedCase method names to
Pascal Volk <neverseen@users.sourceforge.net>
parents:
320
diff
changeset
|
58 |
return self._tid == other._tid |
110
cb8b2f6a5fca
Transport: converted to new-style class; Domain: query reduction
Pascal Volk <neverseen@users.sourceforge.net>
parents:
102
diff
changeset
|
59 |
return NotImplemented |
cb8b2f6a5fca
Transport: converted to new-style class; Domain: query reduction
Pascal Volk <neverseen@users.sourceforge.net>
parents:
102
diff
changeset
|
60 |
|
cb8b2f6a5fca
Transport: converted to new-style class; Domain: query reduction
Pascal Volk <neverseen@users.sourceforge.net>
parents:
102
diff
changeset
|
61 |
def __ne__(self, other): |
cb8b2f6a5fca
Transport: converted to new-style class; Domain: query reduction
Pascal Volk <neverseen@users.sourceforge.net>
parents:
102
diff
changeset
|
62 |
if isinstance(other, self.__class__): |
321
883d5cd66498
VMM/transport: Converted _mixedCase method names to
Pascal Volk <neverseen@users.sourceforge.net>
parents:
320
diff
changeset
|
63 |
return self._tid != other._tid |
110
cb8b2f6a5fca
Transport: converted to new-style class; Domain: query reduction
Pascal Volk <neverseen@users.sourceforge.net>
parents:
102
diff
changeset
|
64 |
return NotImplemented |
cb8b2f6a5fca
Transport: converted to new-style class; Domain: query reduction
Pascal Volk <neverseen@users.sourceforge.net>
parents:
102
diff
changeset
|
65 |
|
cb8b2f6a5fca
Transport: converted to new-style class; Domain: query reduction
Pascal Volk <neverseen@users.sourceforge.net>
parents:
102
diff
changeset
|
66 |
def __str__(self): |
225
a51809f7940b
VMM/Transport: reworked Transport class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
216
diff
changeset
|
67 |
return self._transport |
110
cb8b2f6a5fca
Transport: converted to new-style class; Domain: query reduction
Pascal Volk <neverseen@users.sourceforge.net>
parents:
102
diff
changeset
|
68 |
|
321
883d5cd66498
VMM/transport: Converted _mixedCase method names to
Pascal Volk <neverseen@users.sourceforge.net>
parents:
320
diff
changeset
|
69 |
def _load_by_id(self): |
883d5cd66498
VMM/transport: Converted _mixedCase method names to
Pascal Volk <neverseen@users.sourceforge.net>
parents:
320
diff
changeset
|
70 |
"""load a transport by its id from the database""" |
8
7e3ce56f49e6
* 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
71 |
dbc = self._dbh.cursor() |
352
22d115376e4d
VMM/…: Provide parameters as tuple to cursor.execute().
Pascal Volk <neverseen@users.sourceforge.net>
parents:
321
diff
changeset
|
72 |
dbc.execute('SELECT transport FROM transport WHERE tid = %s', |
22d115376e4d
VMM/…: Provide parameters as tuple to cursor.execute().
Pascal Volk <neverseen@users.sourceforge.net>
parents:
321
diff
changeset
|
73 |
(self._tid,)) |
8
7e3ce56f49e6
* 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
74 |
result = dbc.fetchone() |
7e3ce56f49e6
* 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
75 |
dbc.close() |
225
a51809f7940b
VMM/Transport: reworked Transport class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
216
diff
changeset
|
76 |
if result: |
a51809f7940b
VMM/Transport: reworked Transport class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
216
diff
changeset
|
77 |
self._transport = result[0] |
8
7e3ce56f49e6
* 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
78 |
else: |
360
44283818f8db
VMM/transport: Small error message 'improvement'.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
352
diff
changeset
|
79 |
raise TransportError(_(u'Unknown transport id specified.'), |
225
a51809f7940b
VMM/Transport: reworked Transport class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
216
diff
changeset
|
80 |
UNKNOWN_TRANSPORT_ID) |
8
7e3ce56f49e6
* 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
81 |
|
321
883d5cd66498
VMM/transport: Converted _mixedCase method names to
Pascal Volk <neverseen@users.sourceforge.net>
parents:
320
diff
changeset
|
82 |
def _load_by_name(self): |
883d5cd66498
VMM/transport: Converted _mixedCase method names to
Pascal Volk <neverseen@users.sourceforge.net>
parents:
320
diff
changeset
|
83 |
"""Load a transport by its transport name from the database.""" |
8
7e3ce56f49e6
* 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
84 |
dbc = self._dbh.cursor() |
7e3ce56f49e6
* 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
85 |
dbc.execute('SELECT tid FROM transport WHERE transport = %s', |
352
22d115376e4d
VMM/…: Provide parameters as tuple to cursor.execute().
Pascal Volk <neverseen@users.sourceforge.net>
parents:
321
diff
changeset
|
86 |
(self._transport,)) |
8
7e3ce56f49e6
* 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
87 |
result = dbc.fetchone() |
7e3ce56f49e6
* 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
88 |
dbc.close() |
225
a51809f7940b
VMM/Transport: reworked Transport class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
216
diff
changeset
|
89 |
if result: |
235
9d3405ed08e5
VMM/Transport: Renamed attribute/property {,_}id to {,_}tid.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
225
diff
changeset
|
90 |
self._tid = result[0] |
8
7e3ce56f49e6
* 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
91 |
else: |
7e3ce56f49e6
* 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
92 |
self._save() |
7e3ce56f49e6
* 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
93 |
|
7e3ce56f49e6
* 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
94 |
def _save(self): |
321
883d5cd66498
VMM/transport: Converted _mixedCase method names to
Pascal Volk <neverseen@users.sourceforge.net>
parents:
320
diff
changeset
|
95 |
"""Save the new transport in the database.""" |
8
7e3ce56f49e6
* 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
96 |
dbc = self._dbh.cursor() |
7e3ce56f49e6
* 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
97 |
dbc.execute("SELECT nextval('transport_id')") |
235
9d3405ed08e5
VMM/Transport: Renamed attribute/property {,_}id to {,_}tid.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
225
diff
changeset
|
98 |
self._tid = dbc.fetchone()[0] |
379
7518d927d443
VMM/*: Use target column names in all INSERT statements.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
366
diff
changeset
|
99 |
dbc.execute('INSERT INTO transport (tid, transport) VALUES (%s, %s)', |
7518d927d443
VMM/*: Use target column names in all INSERT statements.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
366
diff
changeset
|
100 |
(self._tid, self._transport)) |
8
7e3ce56f49e6
* 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
101 |
self._dbh.commit() |
7e3ce56f49e6
* 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
102 |
dbc.close() |
321
883d5cd66498
VMM/transport: Converted _mixedCase method names to
Pascal Volk <neverseen@users.sourceforge.net>
parents:
320
diff
changeset
|
103 |
|
883d5cd66498
VMM/transport: Converted _mixedCase method names to
Pascal Volk <neverseen@users.sourceforge.net>
parents:
320
diff
changeset
|
104 |
del _ |