author | Pascal Volk <neverseen@users.sourceforge.net> |
Wed, 03 Mar 2010 14:32:30 +0000 | |
branch | v0.6.x |
changeset 228 | a7b000ca4ac9 |
parent 223 | 5c7b7cbb01cd |
child 290 | e2785e04f92e |
permissions | -rw-r--r-- |
86
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
1 |
# -*- coding: UTF-8 -*- |
162
0ac9ef587769
Updated copyright notices to include the year 2010.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
155
diff
changeset
|
2 |
# Copyright (c) 2008 - 2010, Pascal Volk |
86
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
3 |
# See COPYING for distribution information. |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
4 |
|
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
5 |
"""A small - r/o - wrapper class for Postfix' postconf.""" |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
6 |
|
185
6e1ef32fbd82
VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
162
diff
changeset
|
7 |
import re |
86
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
8 |
from subprocess import Popen, PIPE |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
9 |
|
185
6e1ef32fbd82
VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
162
diff
changeset
|
10 |
import VirtualMailManager.constants.ERROR as ERR |
216
0c8c053b451c
Moved VirtualMailManager/Exceptions to VirtualMailManager/errors.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
185
diff
changeset
|
11 |
from VirtualMailManager.errors import VMMError |
86
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
12 |
|
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
13 |
RE_PC_PARAMS = """^\w+$""" |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
14 |
RE_PC_VARIABLES = r"""\$\b\w+\b""" |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
15 |
|
128
cf8116625866
Converted VirtualMailManager and Postconf to new-style classes.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
102
diff
changeset
|
16 |
class Postconf(object): |
cf8116625866
Converted VirtualMailManager and Postconf to new-style classes.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
102
diff
changeset
|
17 |
__slots__ = ('__bin', '__val', '__varFinder') |
86
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
18 |
def __init__(self, postconf_bin): |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
19 |
"""Creates a new Postconf instance. |
128
cf8116625866
Converted VirtualMailManager and Postconf to new-style classes.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
102
diff
changeset
|
20 |
|
86
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
21 |
Keyword arguments: |
128
cf8116625866
Converted VirtualMailManager and Postconf to new-style classes.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
102
diff
changeset
|
22 |
postconf_bin -- absolute path to the Postfix postconf binary (str) |
86
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
23 |
""" |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
24 |
self.__bin = postconf_bin |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
25 |
self.__val = '' |
87
f9090d1a0730
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
86
diff
changeset
|
26 |
self.__varFinder = re.compile(RE_PC_VARIABLES) |
86
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
27 |
|
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
28 |
def read(self, parameter, expand_vars=True): |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
29 |
"""Returns the parameters value. |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
30 |
|
128
cf8116625866
Converted VirtualMailManager and Postconf to new-style classes.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
102
diff
changeset
|
31 |
If expand_vars is True (default), all variables in the value will be |
86
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
32 |
expanded: |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
33 |
e.g. mydestination -> mail.example.com, localhost.example.com, localhost |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
34 |
Otherwise the value may contain one or more variables. |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
35 |
e.g. mydestination -> $myhostname, localhost.$mydomain, localhost |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
36 |
|
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
37 |
Keyword arguments: |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
38 |
parameter -- the name of a Postfix configuration parameter (str) |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
39 |
expand_vars -- default True (bool) |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
40 |
""" |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
41 |
if not re.match(RE_PC_PARAMS, parameter): |
216
0c8c053b451c
Moved VirtualMailManager/Exceptions to VirtualMailManager/errors.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
185
diff
changeset
|
42 |
raise VMMError(_(u'The value “%s” doesn\'t look like a valid\ |
128
cf8116625866
Converted VirtualMailManager and Postconf to new-style classes.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
102
diff
changeset
|
43 |
postfix configuration parameter name.') % parameter, ERR.VMM_ERROR) |
86
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
44 |
self.__val = self.__read(parameter) |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
45 |
if expand_vars: |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
46 |
self.__expandVars() |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
47 |
return self.__val |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
48 |
|
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
49 |
def __expandVars(self): |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
50 |
while True: |
87
f9090d1a0730
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
86
diff
changeset
|
51 |
pvars = set(self.__varFinder.findall(self.__val)) |
86
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
52 |
pvars_len = len(pvars) |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
53 |
if pvars_len < 1: |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
54 |
break |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
55 |
if pvars_len > 1: |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
56 |
self.__expandMultiVars(self.__readMulti(pvars)) |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
57 |
continue |
89
752d3b57ebb6
small fixes/updates
Pascal Volk <neverseen@users.sourceforge.net>
parents:
87
diff
changeset
|
58 |
pvars = pvars.pop() |
752d3b57ebb6
small fixes/updates
Pascal Volk <neverseen@users.sourceforge.net>
parents:
87
diff
changeset
|
59 |
self.__val = self.__val.replace(pvars, self.__read(pvars[1:])) |
86
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
60 |
|
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
61 |
def __expandMultiVars(self, old_new): |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
62 |
for old, new in old_new.items(): |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
63 |
self.__val = self.__val.replace('$'+old, new) |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
64 |
|
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
65 |
def __read(self, parameter): |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
66 |
out, err = Popen([self.__bin, '-h', parameter], stdout=PIPE, |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
67 |
stderr=PIPE).communicate() |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
68 |
if len(err): |
216
0c8c053b451c
Moved VirtualMailManager/Exceptions to VirtualMailManager/errors.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
185
diff
changeset
|
69 |
raise VMMError(err.strip(), ERR.VMM_ERROR) |
86
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
70 |
return out.strip() |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
71 |
|
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
72 |
def __readMulti(self, parameters): |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
73 |
cmd = [self.__bin] |
223
5c7b7cbb01cd
replaced some "for x in list …"
Pascal Volk <neverseen@users.sourceforge.net>
parents:
216
diff
changeset
|
74 |
cmd.extend(parameter[1:] for parameter in parameters) |
86
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
75 |
out, err = Popen(cmd, stdout=PIPE, stderr=PIPE).communicate() |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
76 |
if len(err): |
216
0c8c053b451c
Moved VirtualMailManager/Exceptions to VirtualMailManager/errors.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
185
diff
changeset
|
77 |
raise VMMError(err.strip(), ERR.VMM_ERROR) |
86
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
78 |
par_val = {} |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
79 |
for line in out.splitlines(): |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
80 |
par, val = line.split(' = ') |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
81 |
par_val[par] = val |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
82 |
return par_val |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
83 |