author | Pascal Volk <user@localhost.localdomain.org> |
Sat, 05 Jan 2013 18:39:15 +0000 | |
branch | v0.7.x |
changeset 674 | 995203a101e1 |
parent 670 | f374ef062c94 |
child 676 | 2bc11dada296 |
permissions | -rw-r--r-- |
262
6eea85d8b91d
VMM: moved some non-init functions to the new common module.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
1 |
# -*- coding: UTF-8 -*- |
568
14abdd04ddf5
Updated copyright notices to include the year 2012.
Pascal Volk <user@localhost.localdomain.org>
parents:
555
diff
changeset
|
2 |
# Copyright (c) 2010 - 2012, Pascal Volk |
262
6eea85d8b91d
VMM: moved some non-init functions to the new common module.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
3 |
# See COPYING for distribution information. |
6eea85d8b91d
VMM: moved some non-init functions to the new common module.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
4 |
""" |
6eea85d8b91d
VMM: moved some non-init functions to the new common module.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
5 |
VirtualMailManager.common |
320
011066435e6f
VMM/*: Made all modules names lowercase, adjusted imports.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
316
diff
changeset
|
6 |
~~~~~~~~~~~~~~~~~~~~~~~~~ |
262
6eea85d8b91d
VMM: moved some non-init functions to the new common module.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
7 |
|
6eea85d8b91d
VMM: moved some non-init functions to the new common module.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
8 |
Some common functions |
6eea85d8b91d
VMM: moved some non-init functions to the new common module.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
9 |
""" |
6eea85d8b91d
VMM: moved some non-init functions to the new common module.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
10 |
|
414
ae1a8428298c
VMM: Report quota usage/limit/percentage values formatted
Pascal Volk <neverseen@users.sourceforge.net>
parents:
399
diff
changeset
|
11 |
import locale |
262
6eea85d8b91d
VMM: moved some non-init functions to the new common module.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
12 |
import os |
263
07fdc93dde9f
VMM/common: improved version_hex() in order to convert also
Pascal Volk <neverseen@users.sourceforge.net>
parents:
262
diff
changeset
|
13 |
import re |
326
8f8d9c4c8332
VMM/common: Replaced function is_dir() by lisdir().
Pascal Volk <neverseen@users.sourceforge.net>
parents:
320
diff
changeset
|
14 |
import stat |
262
6eea85d8b91d
VMM: moved some non-init functions to the new common module.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
15 |
|
6eea85d8b91d
VMM: moved some non-init functions to the new common module.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
16 |
from VirtualMailManager import ENCODING |
614
1ec3497a6733
VMM: Moved transport-validation to common.
Pascal Volk <user@localhost.localdomain.org>
parents:
611
diff
changeset
|
17 |
from VirtualMailManager.constants import INVALID_MAIL_LOCATION, \ |
1ec3497a6733
VMM: Moved transport-validation to common.
Pascal Volk <user@localhost.localdomain.org>
parents:
611
diff
changeset
|
18 |
NOT_EXECUTABLE, NO_SUCH_BINARY, TYPE_ACCOUNT, TYPE_ALIAS, TYPE_RELOCATED |
262
6eea85d8b91d
VMM: moved some non-init functions to the new common module.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
19 |
from VirtualMailManager.errors import VMMError |
6eea85d8b91d
VMM: moved some non-init functions to the new common module.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
20 |
|
347
586367ee042b
VMM/common: Made the version re pattern object accessible as VERSION_RE.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
328
diff
changeset
|
21 |
VERSION_RE = re.compile(r'^(\d+)\.(\d+)\.(?:(\d+)|(alpha|beta|rc)(\d+))$') |
586367ee042b
VMM/common: Made the version re pattern object accessible as VERSION_RE.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
328
diff
changeset
|
22 |
|
266
e14c345b44a1
VMM/{Account,common,Handler}: Improved version_hex().
Tobias Berling <t-obi@users.sourceforge.net>
parents:
265
diff
changeset
|
23 |
_version_level = dict(alpha=0xA, beta=0xB, rc=0xC) |
273
77fc7138ef6a
VMM/common: added a caching dict for version_hex()/version_str()
Pascal Volk <neverseen@users.sourceforge.net>
parents:
266
diff
changeset
|
24 |
_version_cache = {} |
262
6eea85d8b91d
VMM: moved some non-init functions to the new common module.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
25 |
_ = lambda msg: msg |
6eea85d8b91d
VMM: moved some non-init functions to the new common module.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
26 |
|
6eea85d8b91d
VMM: moved some non-init functions to the new common module.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
27 |
|
6eea85d8b91d
VMM: moved some non-init functions to the new common module.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
28 |
def expand_path(path): |
6eea85d8b91d
VMM: moved some non-init functions to the new common module.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
29 |
"""Expands paths, starting with ``.`` or ``~``, to an absolute path.""" |
6eea85d8b91d
VMM: moved some non-init functions to the new common module.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
30 |
if path.startswith('.'): |
6eea85d8b91d
VMM: moved some non-init functions to the new common module.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
31 |
return os.path.abspath(path) |
6eea85d8b91d
VMM: moved some non-init functions to the new common module.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
32 |
if path.startswith('~'): |
6eea85d8b91d
VMM: moved some non-init functions to the new common module.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
33 |
return os.path.expanduser(path) |
6eea85d8b91d
VMM: moved some non-init functions to the new common module.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
34 |
return path |
6eea85d8b91d
VMM: moved some non-init functions to the new common module.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
35 |
|
6eea85d8b91d
VMM: moved some non-init functions to the new common module.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
36 |
|
6eea85d8b91d
VMM: moved some non-init functions to the new common module.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
37 |
def get_unicode(string): |
6eea85d8b91d
VMM: moved some non-init functions to the new common module.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
38 |
"""Converts `string` to `unicode`, if necessary.""" |
643
df1e3b67882a
Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents:
614
diff
changeset
|
39 |
if isinstance(string, str): |
262
6eea85d8b91d
VMM: moved some non-init functions to the new common module.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
40 |
return string |
643
df1e3b67882a
Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents:
614
diff
changeset
|
41 |
return str(string, ENCODING, 'replace') |
262
6eea85d8b91d
VMM: moved some non-init functions to the new common module.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
42 |
|
6eea85d8b91d
VMM: moved some non-init functions to the new common module.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
43 |
|
326
8f8d9c4c8332
VMM/common: Replaced function is_dir() by lisdir().
Pascal Volk <neverseen@users.sourceforge.net>
parents:
320
diff
changeset
|
44 |
def lisdir(path): |
8f8d9c4c8332
VMM/common: Replaced function is_dir() by lisdir().
Pascal Volk <neverseen@users.sourceforge.net>
parents:
320
diff
changeset
|
45 |
"""Checks if `path` is a directory. Doesn't follow symbolic links. |
8f8d9c4c8332
VMM/common: Replaced function is_dir() by lisdir().
Pascal Volk <neverseen@users.sourceforge.net>
parents:
320
diff
changeset
|
46 |
Returns bool. |
262
6eea85d8b91d
VMM: moved some non-init functions to the new common module.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
47 |
""" |
326
8f8d9c4c8332
VMM/common: Replaced function is_dir() by lisdir().
Pascal Volk <neverseen@users.sourceforge.net>
parents:
320
diff
changeset
|
48 |
try: |
8f8d9c4c8332
VMM/common: Replaced function is_dir() by lisdir().
Pascal Volk <neverseen@users.sourceforge.net>
parents:
320
diff
changeset
|
49 |
lstat = os.lstat(path) |
8f8d9c4c8332
VMM/common: Replaced function is_dir() by lisdir().
Pascal Volk <neverseen@users.sourceforge.net>
parents:
320
diff
changeset
|
50 |
except OSError: |
8f8d9c4c8332
VMM/common: Replaced function is_dir() by lisdir().
Pascal Volk <neverseen@users.sourceforge.net>
parents:
320
diff
changeset
|
51 |
return False |
8f8d9c4c8332
VMM/common: Replaced function is_dir() by lisdir().
Pascal Volk <neverseen@users.sourceforge.net>
parents:
320
diff
changeset
|
52 |
return stat.S_ISDIR(lstat.st_mode) |
262
6eea85d8b91d
VMM: moved some non-init functions to the new common module.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
53 |
|
6eea85d8b91d
VMM: moved some non-init functions to the new common module.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
54 |
|
6eea85d8b91d
VMM: moved some non-init functions to the new common module.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
55 |
def exec_ok(binary): |
6eea85d8b91d
VMM: moved some non-init functions to the new common module.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
56 |
"""Checks if the `binary` exists and if it is executable. |
6eea85d8b91d
VMM: moved some non-init functions to the new common module.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
57 |
|
6eea85d8b91d
VMM: moved some non-init functions to the new common module.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
58 |
Throws a `VMMError` if the `binary` isn't a file or is not |
6eea85d8b91d
VMM: moved some non-init functions to the new common module.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
59 |
executable. |
6eea85d8b91d
VMM: moved some non-init functions to the new common module.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
60 |
""" |
6eea85d8b91d
VMM: moved some non-init functions to the new common module.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
61 |
binary = expand_path(binary) |
6eea85d8b91d
VMM: moved some non-init functions to the new common module.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
62 |
if not os.path.isfile(binary): |
643
df1e3b67882a
Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents:
614
diff
changeset
|
63 |
raise VMMError(_("No such file: '%s'") % get_unicode(binary), |
262
6eea85d8b91d
VMM: moved some non-init functions to the new common module.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
64 |
NO_SUCH_BINARY) |
6eea85d8b91d
VMM: moved some non-init functions to the new common module.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
65 |
if not os.access(binary, os.X_OK): |
643
df1e3b67882a
Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents:
614
diff
changeset
|
66 |
raise VMMError(_("File is not executable: '%s'") % |
262
6eea85d8b91d
VMM: moved some non-init functions to the new common module.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
67 |
get_unicode(binary), NOT_EXECUTABLE) |
6eea85d8b91d
VMM: moved some non-init functions to the new common module.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
68 |
return binary |
6eea85d8b91d
VMM: moved some non-init functions to the new common module.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
69 |
|
6eea85d8b91d
VMM: moved some non-init functions to the new common module.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
70 |
|
393
fb2ba1456bc5
VMM/common: Added function human_size().
Pascal Volk <neverseen@users.sourceforge.net>
parents:
383
diff
changeset
|
71 |
def human_size(size): |
fb2ba1456bc5
VMM/common: Added function human_size().
Pascal Volk <neverseen@users.sourceforge.net>
parents:
383
diff
changeset
|
72 |
"""Converts the `size` in bytes in human readable format.""" |
643
df1e3b67882a
Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents:
614
diff
changeset
|
73 |
if not isinstance(size, int): |
396
7f931c1ca059
VMM/common: human_size() size argument can be also a string.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
395
diff
changeset
|
74 |
try: |
643
df1e3b67882a
Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents:
614
diff
changeset
|
75 |
size = int(size) |
396
7f931c1ca059
VMM/common: human_size() size argument can be also a string.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
395
diff
changeset
|
76 |
except ValueError: |
670
f374ef062c94
VMM/*: Post-2to3 fix. Updated some error messages/comments.
Pascal Volk <user@localhost.localdomain.org>
parents:
653
diff
changeset
|
77 |
raise TypeError("'size' must be a positive integer.") |
396
7f931c1ca059
VMM/common: human_size() size argument can be also a string.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
395
diff
changeset
|
78 |
if size < 0: |
670
f374ef062c94
VMM/*: Post-2to3 fix. Updated some error messages/comments.
Pascal Volk <user@localhost.localdomain.org>
parents:
653
diff
changeset
|
79 |
raise ValueError("'size' must be a positive integer.") |
399
fb22773f7a85
VMM/common: Reworked human_size() once more. Return bytes w/o
Pascal Volk <neverseen@users.sourceforge.net>
parents:
396
diff
changeset
|
80 |
if size < 1024: |
fb22773f7a85
VMM/common: Reworked human_size() once more. Return bytes w/o
Pascal Volk <neverseen@users.sourceforge.net>
parents:
396
diff
changeset
|
81 |
return str(size) |
609
56ec275911f2
Added a few comments for the Translation Project.
Pascal Volk <user@localhost.localdomain.org>
parents:
601
diff
changeset
|
82 |
# TP: abbreviations of gibibyte, tebibyte kibibyte and mebibyte |
643
df1e3b67882a
Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents:
614
diff
changeset
|
83 |
prefix_multiply = ((_('TiB'), 1 << 40), (_('GiB'), 1 << 30), |
df1e3b67882a
Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents:
614
diff
changeset
|
84 |
(_('MiB'), 1 << 20), (_('KiB'), 1 << 10)) |
399
fb22773f7a85
VMM/common: Reworked human_size() once more. Return bytes w/o
Pascal Volk <neverseen@users.sourceforge.net>
parents:
396
diff
changeset
|
85 |
for prefix, multiply in prefix_multiply: |
fb22773f7a85
VMM/common: Reworked human_size() once more. Return bytes w/o
Pascal Volk <neverseen@users.sourceforge.net>
parents:
396
diff
changeset
|
86 |
if size >= multiply: |
414
ae1a8428298c
VMM: Report quota usage/limit/percentage values formatted
Pascal Volk <neverseen@users.sourceforge.net>
parents:
399
diff
changeset
|
87 |
# TP: e.g.: '%(size)s %(prefix)s' -> '118.30 MiB' |
643
df1e3b67882a
Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents:
614
diff
changeset
|
88 |
return _('%(size)s %(prefix)s') % { |
414
ae1a8428298c
VMM: Report quota usage/limit/percentage values formatted
Pascal Volk <neverseen@users.sourceforge.net>
parents:
399
diff
changeset
|
89 |
'size': locale.format('%.2f', float(size) / multiply, |
653
cf07e4468934
VMM: Post-2to3 fixes.
Pascal Volk <user@localhost.localdomain.org>
parents:
643
diff
changeset
|
90 |
True), |
399
fb22773f7a85
VMM/common: Reworked human_size() once more. Return bytes w/o
Pascal Volk <neverseen@users.sourceforge.net>
parents:
396
diff
changeset
|
91 |
'prefix': prefix} |
393
fb2ba1456bc5
VMM/common: Added function human_size().
Pascal Volk <neverseen@users.sourceforge.net>
parents:
383
diff
changeset
|
92 |
|
fb2ba1456bc5
VMM/common: Added function human_size().
Pascal Volk <neverseen@users.sourceforge.net>
parents:
383
diff
changeset
|
93 |
|
383
ac5ac03b58da
VMM/common: Added function size_in_bytes().
Pascal Volk <neverseen@users.sourceforge.net>
parents:
366
diff
changeset
|
94 |
def size_in_bytes(size): |
670
f374ef062c94
VMM/*: Post-2to3 fix. Updated some error messages/comments.
Pascal Volk <user@localhost.localdomain.org>
parents:
653
diff
changeset
|
95 |
"""Converts the string `size` to an integer (size in bytes). |
383
ac5ac03b58da
VMM/common: Added function size_in_bytes().
Pascal Volk <neverseen@users.sourceforge.net>
parents:
366
diff
changeset
|
96 |
|
ac5ac03b58da
VMM/common: Added function size_in_bytes().
Pascal Volk <neverseen@users.sourceforge.net>
parents:
366
diff
changeset
|
97 |
The string `size` can be suffixed with *b* (bytes), *k* (kilobytes), |
ac5ac03b58da
VMM/common: Added function size_in_bytes().
Pascal Volk <neverseen@users.sourceforge.net>
parents:
366
diff
changeset
|
98 |
*M* (megabytes) or *G* (gigabytes). |
ac5ac03b58da
VMM/common: Added function size_in_bytes().
Pascal Volk <neverseen@users.sourceforge.net>
parents:
366
diff
changeset
|
99 |
""" |
643
df1e3b67882a
Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents:
614
diff
changeset
|
100 |
if not isinstance(size, str) or not size: |
383
ac5ac03b58da
VMM/common: Added function size_in_bytes().
Pascal Volk <neverseen@users.sourceforge.net>
parents:
366
diff
changeset
|
101 |
raise TypeError('size must be a non empty string.') |
ac5ac03b58da
VMM/common: Added function size_in_bytes().
Pascal Volk <neverseen@users.sourceforge.net>
parents:
366
diff
changeset
|
102 |
if size[-1].upper() in ('B', 'K', 'M', 'G'): |
ac5ac03b58da
VMM/common: Added function size_in_bytes().
Pascal Volk <neverseen@users.sourceforge.net>
parents:
366
diff
changeset
|
103 |
try: |
ac5ac03b58da
VMM/common: Added function size_in_bytes().
Pascal Volk <neverseen@users.sourceforge.net>
parents:
366
diff
changeset
|
104 |
num = int(size[:-1]) |
ac5ac03b58da
VMM/common: Added function size_in_bytes().
Pascal Volk <neverseen@users.sourceforge.net>
parents:
366
diff
changeset
|
105 |
except ValueError: |
ac5ac03b58da
VMM/common: Added function size_in_bytes().
Pascal Volk <neverseen@users.sourceforge.net>
parents:
366
diff
changeset
|
106 |
raise ValueError('Not a valid integer value: %r' % size[:-1]) |
ac5ac03b58da
VMM/common: Added function size_in_bytes().
Pascal Volk <neverseen@users.sourceforge.net>
parents:
366
diff
changeset
|
107 |
unit = size[-1].upper() |
ac5ac03b58da
VMM/common: Added function size_in_bytes().
Pascal Volk <neverseen@users.sourceforge.net>
parents:
366
diff
changeset
|
108 |
if unit == 'B': |
ac5ac03b58da
VMM/common: Added function size_in_bytes().
Pascal Volk <neverseen@users.sourceforge.net>
parents:
366
diff
changeset
|
109 |
return num |
ac5ac03b58da
VMM/common: Added function size_in_bytes().
Pascal Volk <neverseen@users.sourceforge.net>
parents:
366
diff
changeset
|
110 |
elif unit == 'K': |
643
df1e3b67882a
Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents:
614
diff
changeset
|
111 |
return num << 10 |
383
ac5ac03b58da
VMM/common: Added function size_in_bytes().
Pascal Volk <neverseen@users.sourceforge.net>
parents:
366
diff
changeset
|
112 |
elif unit == 'M': |
643
df1e3b67882a
Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents:
614
diff
changeset
|
113 |
return num << 20 |
383
ac5ac03b58da
VMM/common: Added function size_in_bytes().
Pascal Volk <neverseen@users.sourceforge.net>
parents:
366
diff
changeset
|
114 |
else: |
643
df1e3b67882a
Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents:
614
diff
changeset
|
115 |
return num << 30 |
383
ac5ac03b58da
VMM/common: Added function size_in_bytes().
Pascal Volk <neverseen@users.sourceforge.net>
parents:
366
diff
changeset
|
116 |
else: |
ac5ac03b58da
VMM/common: Added function size_in_bytes().
Pascal Volk <neverseen@users.sourceforge.net>
parents:
366
diff
changeset
|
117 |
try: |
ac5ac03b58da
VMM/common: Added function size_in_bytes().
Pascal Volk <neverseen@users.sourceforge.net>
parents:
366
diff
changeset
|
118 |
num = int(size) |
ac5ac03b58da
VMM/common: Added function size_in_bytes().
Pascal Volk <neverseen@users.sourceforge.net>
parents:
366
diff
changeset
|
119 |
except ValueError: |
ac5ac03b58da
VMM/common: Added function size_in_bytes().
Pascal Volk <neverseen@users.sourceforge.net>
parents:
366
diff
changeset
|
120 |
raise ValueError('Not a valid size value: %r' % size) |
ac5ac03b58da
VMM/common: Added function size_in_bytes().
Pascal Volk <neverseen@users.sourceforge.net>
parents:
366
diff
changeset
|
121 |
return num |
ac5ac03b58da
VMM/common: Added function size_in_bytes().
Pascal Volk <neverseen@users.sourceforge.net>
parents:
366
diff
changeset
|
122 |
|
ac5ac03b58da
VMM/common: Added function size_in_bytes().
Pascal Volk <neverseen@users.sourceforge.net>
parents:
366
diff
changeset
|
123 |
|
614
1ec3497a6733
VMM: Moved transport-validation to common.
Pascal Volk <user@localhost.localdomain.org>
parents:
611
diff
changeset
|
124 |
def validate_transport(transport, maillocation): |
1ec3497a6733
VMM: Moved transport-validation to common.
Pascal Volk <user@localhost.localdomain.org>
parents:
611
diff
changeset
|
125 |
"""Checks if the `transport` is usable for the given `maillocation`. |
1ec3497a6733
VMM: Moved transport-validation to common.
Pascal Volk <user@localhost.localdomain.org>
parents:
611
diff
changeset
|
126 |
|
1ec3497a6733
VMM: Moved transport-validation to common.
Pascal Volk <user@localhost.localdomain.org>
parents:
611
diff
changeset
|
127 |
Throws a `VMMError` if the chosen `transport` is unable to write |
1ec3497a6733
VMM: Moved transport-validation to common.
Pascal Volk <user@localhost.localdomain.org>
parents:
611
diff
changeset
|
128 |
messages in the `maillocation`'s mailbox format. |
1ec3497a6733
VMM: Moved transport-validation to common.
Pascal Volk <user@localhost.localdomain.org>
parents:
611
diff
changeset
|
129 |
|
1ec3497a6733
VMM: Moved transport-validation to common.
Pascal Volk <user@localhost.localdomain.org>
parents:
611
diff
changeset
|
130 |
Arguments: |
1ec3497a6733
VMM: Moved transport-validation to common.
Pascal Volk <user@localhost.localdomain.org>
parents:
611
diff
changeset
|
131 |
|
1ec3497a6733
VMM: Moved transport-validation to common.
Pascal Volk <user@localhost.localdomain.org>
parents:
611
diff
changeset
|
132 |
`transport` : VirtualMailManager.transport.Transport |
1ec3497a6733
VMM: Moved transport-validation to common.
Pascal Volk <user@localhost.localdomain.org>
parents:
611
diff
changeset
|
133 |
a Transport object |
1ec3497a6733
VMM: Moved transport-validation to common.
Pascal Volk <user@localhost.localdomain.org>
parents:
611
diff
changeset
|
134 |
`maillocation` : VirtualMailManager.maillocation.MailLocation |
1ec3497a6733
VMM: Moved transport-validation to common.
Pascal Volk <user@localhost.localdomain.org>
parents:
611
diff
changeset
|
135 |
a MailLocation object |
1ec3497a6733
VMM: Moved transport-validation to common.
Pascal Volk <user@localhost.localdomain.org>
parents:
611
diff
changeset
|
136 |
""" |
1ec3497a6733
VMM: Moved transport-validation to common.
Pascal Volk <user@localhost.localdomain.org>
parents:
611
diff
changeset
|
137 |
if transport.transport in ('virtual', 'virtual:') and \ |
1ec3497a6733
VMM: Moved transport-validation to common.
Pascal Volk <user@localhost.localdomain.org>
parents:
611
diff
changeset
|
138 |
not maillocation.postfix: |
643
df1e3b67882a
Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents:
614
diff
changeset
|
139 |
raise VMMError(_("Invalid transport '%(transport)s' for mailbox " |
df1e3b67882a
Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents:
614
diff
changeset
|
140 |
"format '%(mbfmt)s'.") % |
614
1ec3497a6733
VMM: Moved transport-validation to common.
Pascal Volk <user@localhost.localdomain.org>
parents:
611
diff
changeset
|
141 |
{'transport': transport.transport, |
1ec3497a6733
VMM: Moved transport-validation to common.
Pascal Volk <user@localhost.localdomain.org>
parents:
611
diff
changeset
|
142 |
'mbfmt': maillocation.mbformat}, INVALID_MAIL_LOCATION) |
1ec3497a6733
VMM: Moved transport-validation to common.
Pascal Volk <user@localhost.localdomain.org>
parents:
611
diff
changeset
|
143 |
|
1ec3497a6733
VMM: Moved transport-validation to common.
Pascal Volk <user@localhost.localdomain.org>
parents:
611
diff
changeset
|
144 |
|
262
6eea85d8b91d
VMM: moved some non-init functions to the new common module.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
145 |
def version_hex(version_string): |
266
e14c345b44a1
VMM/{Account,common,Handler}: Improved version_hex().
Tobias Berling <t-obi@users.sourceforge.net>
parents:
265
diff
changeset
|
146 |
"""Converts a Dovecot version, e.g.: '1.2.3' or '2.0.beta4', to an int. |
265
3c0173418d5d
VMM/{Account,common,maillocation}: Dovecot version (check) fixes.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
263
diff
changeset
|
147 |
Raises a `ValueError` if the *version_string* has the wrongâ„¢ format. |
263
07fdc93dde9f
VMM/common: improved version_hex() in order to convert also
Pascal Volk <neverseen@users.sourceforge.net>
parents:
262
diff
changeset
|
148 |
|
266
e14c345b44a1
VMM/{Account,common,Handler}: Improved version_hex().
Tobias Berling <t-obi@users.sourceforge.net>
parents:
265
diff
changeset
|
149 |
version_hex('1.2.3') -> 270548736 |
e14c345b44a1
VMM/{Account,common,Handler}: Improved version_hex().
Tobias Berling <t-obi@users.sourceforge.net>
parents:
265
diff
changeset
|
150 |
hex(version_hex('1.2.3')) -> '0x10203f00' |
262
6eea85d8b91d
VMM: moved some non-init functions to the new common module.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
151 |
""" |
347
586367ee042b
VMM/common: Made the version re pattern object accessible as VERSION_RE.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
328
diff
changeset
|
152 |
global _version_cache |
273
77fc7138ef6a
VMM/common: added a caching dict for version_hex()/version_str()
Pascal Volk <neverseen@users.sourceforge.net>
parents:
266
diff
changeset
|
153 |
if version_string in _version_cache: |
77fc7138ef6a
VMM/common: added a caching dict for version_hex()/version_str()
Pascal Volk <neverseen@users.sourceforge.net>
parents:
266
diff
changeset
|
154 |
return _version_cache[version_string] |
263
07fdc93dde9f
VMM/common: improved version_hex() in order to convert also
Pascal Volk <neverseen@users.sourceforge.net>
parents:
262
diff
changeset
|
155 |
version = 0 |
347
586367ee042b
VMM/common: Made the version re pattern object accessible as VERSION_RE.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
328
diff
changeset
|
156 |
version_mo = VERSION_RE.match(version_string) |
265
3c0173418d5d
VMM/{Account,common,maillocation}: Dovecot version (check) fixes.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
263
diff
changeset
|
157 |
if not version_mo: |
3c0173418d5d
VMM/{Account,common,maillocation}: Dovecot version (check) fixes.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
263
diff
changeset
|
158 |
raise ValueError('Invalid version string: %r' % version_string) |
3c0173418d5d
VMM/{Account,common,maillocation}: Dovecot version (check) fixes.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
263
diff
changeset
|
159 |
major, minor, patch, level, serial = version_mo.groups() |
266
e14c345b44a1
VMM/{Account,common,Handler}: Improved version_hex().
Tobias Berling <t-obi@users.sourceforge.net>
parents:
265
diff
changeset
|
160 |
major = int(major) |
e14c345b44a1
VMM/{Account,common,Handler}: Improved version_hex().
Tobias Berling <t-obi@users.sourceforge.net>
parents:
265
diff
changeset
|
161 |
minor = int(minor) |
265
3c0173418d5d
VMM/{Account,common,maillocation}: Dovecot version (check) fixes.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
263
diff
changeset
|
162 |
if patch: |
266
e14c345b44a1
VMM/{Account,common,Handler}: Improved version_hex().
Tobias Berling <t-obi@users.sourceforge.net>
parents:
265
diff
changeset
|
163 |
patch = int(patch) |
265
3c0173418d5d
VMM/{Account,common,maillocation}: Dovecot version (check) fixes.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
263
diff
changeset
|
164 |
if serial: |
266
e14c345b44a1
VMM/{Account,common,Handler}: Improved version_hex().
Tobias Berling <t-obi@users.sourceforge.net>
parents:
265
diff
changeset
|
165 |
serial = int(serial) |
e14c345b44a1
VMM/{Account,common,Handler}: Improved version_hex().
Tobias Berling <t-obi@users.sourceforge.net>
parents:
265
diff
changeset
|
166 |
|
e14c345b44a1
VMM/{Account,common,Handler}: Improved version_hex().
Tobias Berling <t-obi@users.sourceforge.net>
parents:
265
diff
changeset
|
167 |
if major > 0xFF or minor > 0xFF or \ |
e14c345b44a1
VMM/{Account,common,Handler}: Improved version_hex().
Tobias Berling <t-obi@users.sourceforge.net>
parents:
265
diff
changeset
|
168 |
patch and patch > 0xFF or serial and serial > 0xFF: |
e14c345b44a1
VMM/{Account,common,Handler}: Improved version_hex().
Tobias Berling <t-obi@users.sourceforge.net>
parents:
265
diff
changeset
|
169 |
raise ValueError('Invalid version string: %r' % version_string) |
e14c345b44a1
VMM/{Account,common,Handler}: Improved version_hex().
Tobias Berling <t-obi@users.sourceforge.net>
parents:
265
diff
changeset
|
170 |
|
e14c345b44a1
VMM/{Account,common,Handler}: Improved version_hex().
Tobias Berling <t-obi@users.sourceforge.net>
parents:
265
diff
changeset
|
171 |
version += major << 28 |
e14c345b44a1
VMM/{Account,common,Handler}: Improved version_hex().
Tobias Berling <t-obi@users.sourceforge.net>
parents:
265
diff
changeset
|
172 |
version += minor << 20 |
e14c345b44a1
VMM/{Account,common,Handler}: Improved version_hex().
Tobias Berling <t-obi@users.sourceforge.net>
parents:
265
diff
changeset
|
173 |
if patch: |
e14c345b44a1
VMM/{Account,common,Handler}: Improved version_hex().
Tobias Berling <t-obi@users.sourceforge.net>
parents:
265
diff
changeset
|
174 |
version += patch << 12 |
e14c345b44a1
VMM/{Account,common,Handler}: Improved version_hex().
Tobias Berling <t-obi@users.sourceforge.net>
parents:
265
diff
changeset
|
175 |
version += _version_level.get(level, 0xF) << 8 |
e14c345b44a1
VMM/{Account,common,Handler}: Improved version_hex().
Tobias Berling <t-obi@users.sourceforge.net>
parents:
265
diff
changeset
|
176 |
if serial: |
e14c345b44a1
VMM/{Account,common,Handler}: Improved version_hex().
Tobias Berling <t-obi@users.sourceforge.net>
parents:
265
diff
changeset
|
177 |
version += serial |
e14c345b44a1
VMM/{Account,common,Handler}: Improved version_hex().
Tobias Berling <t-obi@users.sourceforge.net>
parents:
265
diff
changeset
|
178 |
|
273
77fc7138ef6a
VMM/common: added a caching dict for version_hex()/version_str()
Pascal Volk <neverseen@users.sourceforge.net>
parents:
266
diff
changeset
|
179 |
_version_cache[version_string] = version |
263
07fdc93dde9f
VMM/common: improved version_hex() in order to convert also
Pascal Volk <neverseen@users.sourceforge.net>
parents:
262
diff
changeset
|
180 |
return version |
262
6eea85d8b91d
VMM: moved some non-init functions to the new common module.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
181 |
|
266
e14c345b44a1
VMM/{Account,common,Handler}: Improved version_hex().
Tobias Berling <t-obi@users.sourceforge.net>
parents:
265
diff
changeset
|
182 |
|
e14c345b44a1
VMM/{Account,common,Handler}: Improved version_hex().
Tobias Berling <t-obi@users.sourceforge.net>
parents:
265
diff
changeset
|
183 |
def version_str(version): |
e14c345b44a1
VMM/{Account,common,Handler}: Improved version_hex().
Tobias Berling <t-obi@users.sourceforge.net>
parents:
265
diff
changeset
|
184 |
"""Converts a Dovecot version previously converted with version_hex back to |
e14c345b44a1
VMM/{Account,common,Handler}: Improved version_hex().
Tobias Berling <t-obi@users.sourceforge.net>
parents:
265
diff
changeset
|
185 |
a string. |
670
f374ef062c94
VMM/*: Post-2to3 fix. Updated some error messages/comments.
Pascal Volk <user@localhost.localdomain.org>
parents:
653
diff
changeset
|
186 |
Raises a `TypeError` if *version* is not an integer. |
266
e14c345b44a1
VMM/{Account,common,Handler}: Improved version_hex().
Tobias Berling <t-obi@users.sourceforge.net>
parents:
265
diff
changeset
|
187 |
Raises a `ValueError` if *version* is an incorrect int version. |
e14c345b44a1
VMM/{Account,common,Handler}: Improved version_hex().
Tobias Berling <t-obi@users.sourceforge.net>
parents:
265
diff
changeset
|
188 |
""" |
347
586367ee042b
VMM/common: Made the version re pattern object accessible as VERSION_RE.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
328
diff
changeset
|
189 |
global _version_cache |
273
77fc7138ef6a
VMM/common: added a caching dict for version_hex()/version_str()
Pascal Volk <neverseen@users.sourceforge.net>
parents:
266
diff
changeset
|
190 |
if version in _version_cache: |
77fc7138ef6a
VMM/common: added a caching dict for version_hex()/version_str()
Pascal Volk <neverseen@users.sourceforge.net>
parents:
266
diff
changeset
|
191 |
return _version_cache[version] |
643
df1e3b67882a
Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents:
614
diff
changeset
|
192 |
if not isinstance(version, int): |
670
f374ef062c94
VMM/*: Post-2to3 fix. Updated some error messages/comments.
Pascal Volk <user@localhost.localdomain.org>
parents:
653
diff
changeset
|
193 |
raise TypeError('Argument is not a integer: %r', version) |
266
e14c345b44a1
VMM/{Account,common,Handler}: Improved version_hex().
Tobias Berling <t-obi@users.sourceforge.net>
parents:
265
diff
changeset
|
194 |
major = (version >> 28) & 0xFF |
e14c345b44a1
VMM/{Account,common,Handler}: Improved version_hex().
Tobias Berling <t-obi@users.sourceforge.net>
parents:
265
diff
changeset
|
195 |
minor = (version >> 20) & 0xFF |
e14c345b44a1
VMM/{Account,common,Handler}: Improved version_hex().
Tobias Berling <t-obi@users.sourceforge.net>
parents:
265
diff
changeset
|
196 |
patch = (version >> 12) & 0xFF |
e14c345b44a1
VMM/{Account,common,Handler}: Improved version_hex().
Tobias Berling <t-obi@users.sourceforge.net>
parents:
265
diff
changeset
|
197 |
level = (version >> 8) & 0x0F |
e14c345b44a1
VMM/{Account,common,Handler}: Improved version_hex().
Tobias Berling <t-obi@users.sourceforge.net>
parents:
265
diff
changeset
|
198 |
serial = version & 0xFF |
e14c345b44a1
VMM/{Account,common,Handler}: Improved version_hex().
Tobias Berling <t-obi@users.sourceforge.net>
parents:
265
diff
changeset
|
199 |
|
653
cf07e4468934
VMM: Post-2to3 fixes.
Pascal Volk <user@localhost.localdomain.org>
parents:
643
diff
changeset
|
200 |
levels = dict(list(zip(list(_version_level.values()), |
cf07e4468934
VMM: Post-2to3 fixes.
Pascal Volk <user@localhost.localdomain.org>
parents:
643
diff
changeset
|
201 |
list(_version_level.keys())))) |
266
e14c345b44a1
VMM/{Account,common,Handler}: Improved version_hex().
Tobias Berling <t-obi@users.sourceforge.net>
parents:
265
diff
changeset
|
202 |
if level == 0xF and not serial: |
e14c345b44a1
VMM/{Account,common,Handler}: Improved version_hex().
Tobias Berling <t-obi@users.sourceforge.net>
parents:
265
diff
changeset
|
203 |
version_string = '%u.%u.%u' % (major, minor, patch) |
e14c345b44a1
VMM/{Account,common,Handler}: Improved version_hex().
Tobias Berling <t-obi@users.sourceforge.net>
parents:
265
diff
changeset
|
204 |
elif level in levels and not patch: |
e14c345b44a1
VMM/{Account,common,Handler}: Improved version_hex().
Tobias Berling <t-obi@users.sourceforge.net>
parents:
265
diff
changeset
|
205 |
version_string = '%u.%u.%s%u' % (major, minor, levels[level], serial) |
e14c345b44a1
VMM/{Account,common,Handler}: Improved version_hex().
Tobias Berling <t-obi@users.sourceforge.net>
parents:
265
diff
changeset
|
206 |
else: |
e14c345b44a1
VMM/{Account,common,Handler}: Improved version_hex().
Tobias Berling <t-obi@users.sourceforge.net>
parents:
265
diff
changeset
|
207 |
raise ValueError('Invalid version: %r' % hex(version)) |
e14c345b44a1
VMM/{Account,common,Handler}: Improved version_hex().
Tobias Berling <t-obi@users.sourceforge.net>
parents:
265
diff
changeset
|
208 |
|
273
77fc7138ef6a
VMM/common: added a caching dict for version_hex()/version_str()
Pascal Volk <neverseen@users.sourceforge.net>
parents:
266
diff
changeset
|
209 |
_version_cache[version] = version_string |
266
e14c345b44a1
VMM/{Account,common,Handler}: Improved version_hex().
Tobias Berling <t-obi@users.sourceforge.net>
parents:
265
diff
changeset
|
210 |
return version_string |
e14c345b44a1
VMM/{Account,common,Handler}: Improved version_hex().
Tobias Berling <t-obi@users.sourceforge.net>
parents:
265
diff
changeset
|
211 |
|
611
8e9b0046bc8f
PEP8: Fixed all PEP8 related issues.
Pascal Volk <user@localhost.localdomain.org>
parents:
609
diff
changeset
|
212 |
|
532
2bb40aaef94e
Modify userinfo output to indicate when domain defaults are displayed
martin f. krafft <madduck@madduck.net>
parents:
528
diff
changeset
|
213 |
def format_domain_default(domaindata): |
546
79f09cdd1a21
Add docstring for common.format_domain_default
martin f. krafft <madduck@madduck.net>
parents:
532
diff
changeset
|
214 |
"""Format info output when the value displayed is the domain default.""" |
609
56ec275911f2
Added a few comments for the Translation Project.
Pascal Volk <user@localhost.localdomain.org>
parents:
601
diff
changeset
|
215 |
# TP: [domain default] indicates that a user's setting is the same as |
56ec275911f2
Added a few comments for the Translation Project.
Pascal Volk <user@localhost.localdomain.org>
parents:
601
diff
changeset
|
216 |
# configured in the user's domain. |
56ec275911f2
Added a few comments for the Translation Project.
Pascal Volk <user@localhost.localdomain.org>
parents:
601
diff
changeset
|
217 |
# e.g.: [ 0.84%] 42/5,000 [domain default] |
643
df1e3b67882a
Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents:
614
diff
changeset
|
218 |
return _('%s [domain default]') % domaindata |
532
2bb40aaef94e
Modify userinfo output to indicate when domain defaults are displayed
martin f. krafft <madduck@madduck.net>
parents:
528
diff
changeset
|
219 |
|
555
499c63f52462
Provide list{addresses,aliases,users,relocated} subcommands
martin f. krafft <madduck@madduck.net>
parents:
546
diff
changeset
|
220 |
|
499c63f52462
Provide list{addresses,aliases,users,relocated} subcommands
martin f. krafft <madduck@madduck.net>
parents:
546
diff
changeset
|
221 |
def search_addresses(dbh, typelimit=None, lpattern=None, llike=False, |
499c63f52462
Provide list{addresses,aliases,users,relocated} subcommands
martin f. krafft <madduck@madduck.net>
parents:
546
diff
changeset
|
222 |
dpattern=None, dlike=False): |
499c63f52462
Provide list{addresses,aliases,users,relocated} subcommands
martin f. krafft <madduck@madduck.net>
parents:
546
diff
changeset
|
223 |
"""'Search' for addresses by *pattern* in the database. |
499c63f52462
Provide list{addresses,aliases,users,relocated} subcommands
martin f. krafft <madduck@madduck.net>
parents:
546
diff
changeset
|
224 |
|
499c63f52462
Provide list{addresses,aliases,users,relocated} subcommands
martin f. krafft <madduck@madduck.net>
parents:
546
diff
changeset
|
225 |
The search is limited by *typelimit*, a bitfield with values TYPE_ACCOUNT, |
499c63f52462
Provide list{addresses,aliases,users,relocated} subcommands
martin f. krafft <madduck@madduck.net>
parents:
546
diff
changeset
|
226 |
TYPE_ALIAS, TYPE_RELOCATED, or a bitwise OR thereof. If no limit is |
499c63f52462
Provide list{addresses,aliases,users,relocated} subcommands
martin f. krafft <madduck@madduck.net>
parents:
546
diff
changeset
|
227 |
specified, all types will be searched. |
499c63f52462
Provide list{addresses,aliases,users,relocated} subcommands
martin f. krafft <madduck@madduck.net>
parents:
546
diff
changeset
|
228 |
|
499c63f52462
Provide list{addresses,aliases,users,relocated} subcommands
martin f. krafft <madduck@madduck.net>
parents:
546
diff
changeset
|
229 |
*lpattern* may be a local part or a partial local part - starting and/or |
499c63f52462
Provide list{addresses,aliases,users,relocated} subcommands
martin f. krafft <madduck@madduck.net>
parents:
546
diff
changeset
|
230 |
ending with a '%' sign. When the *lpattern* starts or ends with a '%' sign |
499c63f52462
Provide list{addresses,aliases,users,relocated} subcommands
martin f. krafft <madduck@madduck.net>
parents:
546
diff
changeset
|
231 |
*llike* has to be `True` to perform a wildcard search. To retrieve all |
499c63f52462
Provide list{addresses,aliases,users,relocated} subcommands
martin f. krafft <madduck@madduck.net>
parents:
546
diff
changeset
|
232 |
available addresses use the arguments' default values. |
499c63f52462
Provide list{addresses,aliases,users,relocated} subcommands
martin f. krafft <madduck@madduck.net>
parents:
546
diff
changeset
|
233 |
|
499c63f52462
Provide list{addresses,aliases,users,relocated} subcommands
martin f. krafft <madduck@madduck.net>
parents:
546
diff
changeset
|
234 |
*dpattern* and *dlike* behave analogously for the domain part of an |
499c63f52462
Provide list{addresses,aliases,users,relocated} subcommands
martin f. krafft <madduck@madduck.net>
parents:
546
diff
changeset
|
235 |
address, allowing for separate pattern matching: testuser%@example.% |
499c63f52462
Provide list{addresses,aliases,users,relocated} subcommands
martin f. krafft <madduck@madduck.net>
parents:
546
diff
changeset
|
236 |
|
499c63f52462
Provide list{addresses,aliases,users,relocated} subcommands
martin f. krafft <madduck@madduck.net>
parents:
546
diff
changeset
|
237 |
The return value of this function is a tuple. The first element is a list |
499c63f52462
Provide list{addresses,aliases,users,relocated} subcommands
martin f. krafft <madduck@madduck.net>
parents:
546
diff
changeset
|
238 |
of domain IDs sorted alphabetically by the corresponding domain names. The |
499c63f52462
Provide list{addresses,aliases,users,relocated} subcommands
martin f. krafft <madduck@madduck.net>
parents:
546
diff
changeset
|
239 |
second element is a dictionary indexed by domain ID, holding lists to |
499c63f52462
Provide list{addresses,aliases,users,relocated} subcommands
martin f. krafft <madduck@madduck.net>
parents:
546
diff
changeset
|
240 |
associated addresses. Each address is itself actually a tuple of address, |
499c63f52462
Provide list{addresses,aliases,users,relocated} subcommands
martin f. krafft <madduck@madduck.net>
parents:
546
diff
changeset
|
241 |
type, and boolean indicating whether the address stems from an alias |
499c63f52462
Provide list{addresses,aliases,users,relocated} subcommands
martin f. krafft <madduck@madduck.net>
parents:
546
diff
changeset
|
242 |
domain. |
499c63f52462
Provide list{addresses,aliases,users,relocated} subcommands
martin f. krafft <madduck@madduck.net>
parents:
546
diff
changeset
|
243 |
""" |
611
8e9b0046bc8f
PEP8: Fixed all PEP8 related issues.
Pascal Volk <user@localhost.localdomain.org>
parents:
609
diff
changeset
|
244 |
if typelimit is None: |
555
499c63f52462
Provide list{addresses,aliases,users,relocated} subcommands
martin f. krafft <madduck@madduck.net>
parents:
546
diff
changeset
|
245 |
typelimit = TYPE_ACCOUNT | TYPE_ALIAS | TYPE_RELOCATED |
499c63f52462
Provide list{addresses,aliases,users,relocated} subcommands
martin f. krafft <madduck@madduck.net>
parents:
546
diff
changeset
|
246 |
queries = [] |
499c63f52462
Provide list{addresses,aliases,users,relocated} subcommands
martin f. krafft <madduck@madduck.net>
parents:
546
diff
changeset
|
247 |
if typelimit & TYPE_ACCOUNT: |
499c63f52462
Provide list{addresses,aliases,users,relocated} subcommands
martin f. krafft <madduck@madduck.net>
parents:
546
diff
changeset
|
248 |
queries.append('SELECT gid, local_part, %d AS type FROM users' |
499c63f52462
Provide list{addresses,aliases,users,relocated} subcommands
martin f. krafft <madduck@madduck.net>
parents:
546
diff
changeset
|
249 |
% TYPE_ACCOUNT) |
499c63f52462
Provide list{addresses,aliases,users,relocated} subcommands
martin f. krafft <madduck@madduck.net>
parents:
546
diff
changeset
|
250 |
if typelimit & TYPE_ALIAS: |
611
8e9b0046bc8f
PEP8: Fixed all PEP8 related issues.
Pascal Volk <user@localhost.localdomain.org>
parents:
609
diff
changeset
|
251 |
queries.append('SELECT DISTINCT gid, address as local_part, ' |
8e9b0046bc8f
PEP8: Fixed all PEP8 related issues.
Pascal Volk <user@localhost.localdomain.org>
parents:
609
diff
changeset
|
252 |
'%d AS type FROM alias' % TYPE_ALIAS) |
555
499c63f52462
Provide list{addresses,aliases,users,relocated} subcommands
martin f. krafft <madduck@madduck.net>
parents:
546
diff
changeset
|
253 |
if typelimit & TYPE_RELOCATED: |
499c63f52462
Provide list{addresses,aliases,users,relocated} subcommands
martin f. krafft <madduck@madduck.net>
parents:
546
diff
changeset
|
254 |
queries.append('SELECT gid, address as local_part, %d AS type ' |
499c63f52462
Provide list{addresses,aliases,users,relocated} subcommands
martin f. krafft <madduck@madduck.net>
parents:
546
diff
changeset
|
255 |
'FROM relocated' % TYPE_RELOCATED) |
611
8e9b0046bc8f
PEP8: Fixed all PEP8 related issues.
Pascal Volk <user@localhost.localdomain.org>
parents:
609
diff
changeset
|
256 |
sql = "SELECT gid, local_part || '@' || domainname AS address, " |
555
499c63f52462
Provide list{addresses,aliases,users,relocated} subcommands
martin f. krafft <madduck@madduck.net>
parents:
546
diff
changeset
|
257 |
sql += 'type, NOT is_primary AS from_aliasdomain FROM (' |
499c63f52462
Provide list{addresses,aliases,users,relocated} subcommands
martin f. krafft <madduck@madduck.net>
parents:
546
diff
changeset
|
258 |
sql += ' UNION '.join(queries) |
499c63f52462
Provide list{addresses,aliases,users,relocated} subcommands
martin f. krafft <madduck@madduck.net>
parents:
546
diff
changeset
|
259 |
sql += ') a JOIN domain_name USING (gid)' |
499c63f52462
Provide list{addresses,aliases,users,relocated} subcommands
martin f. krafft <madduck@madduck.net>
parents:
546
diff
changeset
|
260 |
nextkw = 'WHERE' |
499c63f52462
Provide list{addresses,aliases,users,relocated} subcommands
martin f. krafft <madduck@madduck.net>
parents:
546
diff
changeset
|
261 |
sqlargs = [] |
499c63f52462
Provide list{addresses,aliases,users,relocated} subcommands
martin f. krafft <madduck@madduck.net>
parents:
546
diff
changeset
|
262 |
for like, field, pattern in ((dlike, 'domainname', dpattern), |
499c63f52462
Provide list{addresses,aliases,users,relocated} subcommands
martin f. krafft <madduck@madduck.net>
parents:
546
diff
changeset
|
263 |
(llike, 'local_part', lpattern)): |
499c63f52462
Provide list{addresses,aliases,users,relocated} subcommands
martin f. krafft <madduck@madduck.net>
parents:
546
diff
changeset
|
264 |
if like: |
499c63f52462
Provide list{addresses,aliases,users,relocated} subcommands
martin f. krafft <madduck@madduck.net>
parents:
546
diff
changeset
|
265 |
match = 'LIKE' |
499c63f52462
Provide list{addresses,aliases,users,relocated} subcommands
martin f. krafft <madduck@madduck.net>
parents:
546
diff
changeset
|
266 |
else: |
611
8e9b0046bc8f
PEP8: Fixed all PEP8 related issues.
Pascal Volk <user@localhost.localdomain.org>
parents:
609
diff
changeset
|
267 |
if not pattern: |
8e9b0046bc8f
PEP8: Fixed all PEP8 related issues.
Pascal Volk <user@localhost.localdomain.org>
parents:
609
diff
changeset
|
268 |
continue |
555
499c63f52462
Provide list{addresses,aliases,users,relocated} subcommands
martin f. krafft <madduck@madduck.net>
parents:
546
diff
changeset
|
269 |
match = '=' |
499c63f52462
Provide list{addresses,aliases,users,relocated} subcommands
martin f. krafft <madduck@madduck.net>
parents:
546
diff
changeset
|
270 |
sql += ' %s %s %s %%s' % (nextkw, field, match) |
499c63f52462
Provide list{addresses,aliases,users,relocated} subcommands
martin f. krafft <madduck@madduck.net>
parents:
546
diff
changeset
|
271 |
sqlargs.append(pattern) |
499c63f52462
Provide list{addresses,aliases,users,relocated} subcommands
martin f. krafft <madduck@madduck.net>
parents:
546
diff
changeset
|
272 |
nextkw = 'AND' |
499c63f52462
Provide list{addresses,aliases,users,relocated} subcommands
martin f. krafft <madduck@madduck.net>
parents:
546
diff
changeset
|
273 |
sql += ' ORDER BY domainname, local_part' |
499c63f52462
Provide list{addresses,aliases,users,relocated} subcommands
martin f. krafft <madduck@madduck.net>
parents:
546
diff
changeset
|
274 |
dbc = dbh.cursor() |
499c63f52462
Provide list{addresses,aliases,users,relocated} subcommands
martin f. krafft <madduck@madduck.net>
parents:
546
diff
changeset
|
275 |
dbc.execute(sql, sqlargs) |
499c63f52462
Provide list{addresses,aliases,users,relocated} subcommands
martin f. krafft <madduck@madduck.net>
parents:
546
diff
changeset
|
276 |
result = dbc.fetchall() |
499c63f52462
Provide list{addresses,aliases,users,relocated} subcommands
martin f. krafft <madduck@madduck.net>
parents:
546
diff
changeset
|
277 |
dbc.close() |
499c63f52462
Provide list{addresses,aliases,users,relocated} subcommands
martin f. krafft <madduck@madduck.net>
parents:
546
diff
changeset
|
278 |
|
499c63f52462
Provide list{addresses,aliases,users,relocated} subcommands
martin f. krafft <madduck@madduck.net>
parents:
546
diff
changeset
|
279 |
gids = [] |
499c63f52462
Provide list{addresses,aliases,users,relocated} subcommands
martin f. krafft <madduck@madduck.net>
parents:
546
diff
changeset
|
280 |
daddrs = {} |
499c63f52462
Provide list{addresses,aliases,users,relocated} subcommands
martin f. krafft <madduck@madduck.net>
parents:
546
diff
changeset
|
281 |
for gid, address, addrtype, aliasdomain in result: |
586
cd1200d06700
VMM/common: Improve search_address complexity
martin f. krafft <madduck@debian.org>
parents:
583
diff
changeset
|
282 |
if gid not in daddrs: |
555
499c63f52462
Provide list{addresses,aliases,users,relocated} subcommands
martin f. krafft <madduck@madduck.net>
parents:
546
diff
changeset
|
283 |
gids.append(gid) |
499c63f52462
Provide list{addresses,aliases,users,relocated} subcommands
martin f. krafft <madduck@madduck.net>
parents:
546
diff
changeset
|
284 |
daddrs[gid] = [] |
499c63f52462
Provide list{addresses,aliases,users,relocated} subcommands
martin f. krafft <madduck@madduck.net>
parents:
546
diff
changeset
|
285 |
daddrs[gid].append((address, addrtype, aliasdomain)) |
499c63f52462
Provide list{addresses,aliases,users,relocated} subcommands
martin f. krafft <madduck@madduck.net>
parents:
546
diff
changeset
|
286 |
return gids, daddrs |
499c63f52462
Provide list{addresses,aliases,users,relocated} subcommands
martin f. krafft <madduck@madduck.net>
parents:
546
diff
changeset
|
287 |
|
262
6eea85d8b91d
VMM: moved some non-init functions to the new common module.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
288 |
del _ |