author | Pascal Volk <user@localhost.localdomain.org> |
Sun, 23 Feb 2014 18:00:35 +0000 | |
branch | v0.7.x |
changeset 735 | ddcad86f7386 |
parent 711 | 2a75058fc064 |
permissions | -rw-r--r-- |
8
7e3ce56f49e6
* 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
1 |
# -*- coding: UTF-8 -*- |
703
58815c004a61
Updated copyright notices to include the year 2014.
Pascal Volk <user@localhost.localdomain.org>
parents:
675
diff
changeset
|
2 |
# Copyright (c) 2008 - 2014, 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 |
|
321
883d5cd66498
VMM/transport: Converted _mixedCase method names to
Pascal Volk <neverseen@users.sourceforge.net>
parents:
320
diff
changeset
|
12 |
_ = lambda msg: msg |
883d5cd66498
VMM/transport: Converted _mixedCase method names to
Pascal Volk <neverseen@users.sourceforge.net>
parents:
320
diff
changeset
|
13 |
|
8
7e3ce56f49e6
* 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
14 |
|
110
cb8b2f6a5fca
Transport: converted to new-style class; Domain: query reduction
Pascal Volk <neverseen@users.sourceforge.net>
parents:
102
diff
changeset
|
15 |
class Transport(object): |
cb8b2f6a5fca
Transport: converted to new-style class; Domain: query reduction
Pascal Volk <neverseen@users.sourceforge.net>
parents:
102
diff
changeset
|
16 |
"""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
|
17 |
__slots__ = ('_tid', '_transport', '_dbh') |
225
a51809f7940b
VMM/Transport: reworked Transport class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
216
diff
changeset
|
18 |
|
8
7e3ce56f49e6
* 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
19 |
def __init__(self, dbh, tid=None, transport=None): |
7e3ce56f49e6
* 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
20 |
"""Creates a new Transport instance. |
7e3ce56f49e6
* 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
21 |
|
225
a51809f7940b
VMM/Transport: reworked Transport class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
216
diff
changeset
|
22 |
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
|
23 |
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
|
24 |
|
8
7e3ce56f49e6
* 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
25 |
Keyword arguments: |
694
b1bfd4d1d9c0
Dropped pyPgSQL support.
Pascal Volk <user@localhost.localdomain.org>
parents:
676
diff
changeset
|
26 |
dbh -- a psycopg2._psycopg.connection |
670
f374ef062c94
VMM/*: Post-2to3 fix. Updated some error messages/comments.
Pascal Volk <user@localhost.localdomain.org>
parents:
643
diff
changeset
|
27 |
tid -- the id of a transport (int) |
8
7e3ce56f49e6
* 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
28 |
transport -- the value of the transport (str) |
225
a51809f7940b
VMM/Transport: reworked Transport class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
216
diff
changeset
|
29 |
|
8
7e3ce56f49e6
* 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
30 |
""" |
7e3ce56f49e6
* 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
31 |
self._dbh = dbh |
449
6826acb9b0a7
VMM/transport: Reworked Transport initialization.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
379
diff
changeset
|
32 |
self._tid = 0 |
225
a51809f7940b
VMM/Transport: reworked Transport class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
216
diff
changeset
|
33 |
assert any((tid, transport)) |
a51809f7940b
VMM/Transport: reworked Transport class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
216
diff
changeset
|
34 |
if tid: |
643
df1e3b67882a
Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents:
637
diff
changeset
|
35 |
assert not isinstance(tid, bool) and isinstance(tid, int) |
449
6826acb9b0a7
VMM/transport: Reworked Transport initialization.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
379
diff
changeset
|
36 |
self._load_by_id(tid) |
8
7e3ce56f49e6
* 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
37 |
else: |
643
df1e3b67882a
Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents:
637
diff
changeset
|
38 |
assert isinstance(transport, str) |
225
a51809f7940b
VMM/Transport: reworked Transport class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
216
diff
changeset
|
39 |
self._transport = transport |
321
883d5cd66498
VMM/transport: Converted _mixedCase method names to
Pascal Volk <neverseen@users.sourceforge.net>
parents:
320
diff
changeset
|
40 |
self._load_by_name() |
8
7e3ce56f49e6
* 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
41 |
|
225
a51809f7940b
VMM/Transport: reworked Transport class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
216
diff
changeset
|
42 |
@property |
235
9d3405ed08e5
VMM/Transport: Renamed attribute/property {,_}id to {,_}tid.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
225
diff
changeset
|
43 |
def tid(self): |
225
a51809f7940b
VMM/Transport: reworked Transport class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
216
diff
changeset
|
44 |
"""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
|
45 |
return self._tid |
225
a51809f7940b
VMM/Transport: reworked Transport class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
216
diff
changeset
|
46 |
|
a51809f7940b
VMM/Transport: reworked Transport class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
216
diff
changeset
|
47 |
@property |
a51809f7940b
VMM/Transport: reworked Transport class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
216
diff
changeset
|
48 |
def transport(self): |
a51809f7940b
VMM/Transport: reworked Transport class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
216
diff
changeset
|
49 |
"""The transport's value, ex: 'dovecot:'""" |
a51809f7940b
VMM/Transport: reworked Transport class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
216
diff
changeset
|
50 |
return self._transport |
a51809f7940b
VMM/Transport: reworked Transport class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
216
diff
changeset
|
51 |
|
110
cb8b2f6a5fca
Transport: converted to new-style class; Domain: query reduction
Pascal Volk <neverseen@users.sourceforge.net>
parents:
102
diff
changeset
|
52 |
def __eq__(self, other): |
cb8b2f6a5fca
Transport: converted to new-style class; Domain: query reduction
Pascal Volk <neverseen@users.sourceforge.net>
parents:
102
diff
changeset
|
53 |
if isinstance(other, self.__class__): |
321
883d5cd66498
VMM/transport: Converted _mixedCase method names to
Pascal Volk <neverseen@users.sourceforge.net>
parents:
320
diff
changeset
|
54 |
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
|
55 |
return NotImplemented |
cb8b2f6a5fca
Transport: converted to new-style class; Domain: query reduction
Pascal Volk <neverseen@users.sourceforge.net>
parents:
102
diff
changeset
|
56 |
|
cb8b2f6a5fca
Transport: converted to new-style class; Domain: query reduction
Pascal Volk <neverseen@users.sourceforge.net>
parents:
102
diff
changeset
|
57 |
def __ne__(self, other): |
cb8b2f6a5fca
Transport: converted to new-style class; Domain: query reduction
Pascal Volk <neverseen@users.sourceforge.net>
parents:
102
diff
changeset
|
58 |
if isinstance(other, self.__class__): |
321
883d5cd66498
VMM/transport: Converted _mixedCase method names to
Pascal Volk <neverseen@users.sourceforge.net>
parents:
320
diff
changeset
|
59 |
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
|
60 |
return NotImplemented |
cb8b2f6a5fca
Transport: converted to new-style class; Domain: query reduction
Pascal Volk <neverseen@users.sourceforge.net>
parents:
102
diff
changeset
|
61 |
|
cb8b2f6a5fca
Transport: converted to new-style class; Domain: query reduction
Pascal Volk <neverseen@users.sourceforge.net>
parents:
102
diff
changeset
|
62 |
def __str__(self): |
225
a51809f7940b
VMM/Transport: reworked Transport class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
216
diff
changeset
|
63 |
return self._transport |
110
cb8b2f6a5fca
Transport: converted to new-style class; Domain: query reduction
Pascal Volk <neverseen@users.sourceforge.net>
parents:
102
diff
changeset
|
64 |
|
449
6826acb9b0a7
VMM/transport: Reworked Transport initialization.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
379
diff
changeset
|
65 |
def _load_by_id(self, tid): |
321
883d5cd66498
VMM/transport: Converted _mixedCase method names to
Pascal Volk <neverseen@users.sourceforge.net>
parents:
320
diff
changeset
|
66 |
"""load a transport by its id from the database""" |
8
7e3ce56f49e6
* 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
67 |
dbc = self._dbh.cursor() |
449
6826acb9b0a7
VMM/transport: Reworked Transport initialization.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
379
diff
changeset
|
68 |
dbc.execute('SELECT transport FROM transport WHERE tid = %s', (tid,)) |
8
7e3ce56f49e6
* 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
69 |
result = dbc.fetchone() |
7e3ce56f49e6
* 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
70 |
dbc.close() |
449
6826acb9b0a7
VMM/transport: Reworked Transport initialization.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
379
diff
changeset
|
71 |
if not result: |
6826acb9b0a7
VMM/transport: Reworked Transport initialization.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
379
diff
changeset
|
72 |
raise ValueError('Unknown transport id specified: %r' % tid) |
6826acb9b0a7
VMM/transport: Reworked Transport initialization.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
379
diff
changeset
|
73 |
self._transport = result[0] |
6826acb9b0a7
VMM/transport: Reworked Transport initialization.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
379
diff
changeset
|
74 |
self._tid = tid |
8
7e3ce56f49e6
* 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
75 |
|
321
883d5cd66498
VMM/transport: Converted _mixedCase method names to
Pascal Volk <neverseen@users.sourceforge.net>
parents:
320
diff
changeset
|
76 |
def _load_by_name(self): |
883d5cd66498
VMM/transport: Converted _mixedCase method names to
Pascal Volk <neverseen@users.sourceforge.net>
parents:
320
diff
changeset
|
77 |
"""Load a transport by its transport name from the database.""" |
8
7e3ce56f49e6
* 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
78 |
dbc = self._dbh.cursor() |
7e3ce56f49e6
* 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
79 |
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
|
80 |
(self._transport,)) |
8
7e3ce56f49e6
* 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
81 |
result = dbc.fetchone() |
7e3ce56f49e6
* 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
82 |
dbc.close() |
225
a51809f7940b
VMM/Transport: reworked Transport class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
216
diff
changeset
|
83 |
if result: |
235
9d3405ed08e5
VMM/Transport: Renamed attribute/property {,_}id to {,_}tid.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
225
diff
changeset
|
84 |
self._tid = result[0] |
8
7e3ce56f49e6
* 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
85 |
else: |
7e3ce56f49e6
* 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
86 |
self._save() |
7e3ce56f49e6
* 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
87 |
|
7e3ce56f49e6
* 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
88 |
def _save(self): |
321
883d5cd66498
VMM/transport: Converted _mixedCase method names to
Pascal Volk <neverseen@users.sourceforge.net>
parents:
320
diff
changeset
|
89 |
"""Save the new transport in the database.""" |
8
7e3ce56f49e6
* 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
90 |
dbc = self._dbh.cursor() |
7e3ce56f49e6
* 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
91 |
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
|
92 |
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
|
93 |
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
|
94 |
(self._tid, self._transport)) |
8
7e3ce56f49e6
* 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
95 |
self._dbh.commit() |
7e3ce56f49e6
* 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
96 |
dbc.close() |
321
883d5cd66498
VMM/transport: Converted _mixedCase method names to
Pascal Volk <neverseen@users.sourceforge.net>
parents:
320
diff
changeset
|
97 |
|
883d5cd66498
VMM/transport: Converted _mixedCase method names to
Pascal Volk <neverseen@users.sourceforge.net>
parents:
320
diff
changeset
|
98 |
del _ |