author | Pascal Volk <neverseen@users.sourceforge.net> |
Tue, 20 Jul 2010 18:29:26 +0000 | |
branch | v0.6.x |
changeset 299 | d15a27eaa9d2 |
parent 290 | e2785e04f92e |
child 311 | a0a10100aee5 |
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): |
290
e2785e04f92e
VMM/…: re-indented long queries and error messages.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
223
diff
changeset
|
42 |
raise VMMError(_(u"The value '%s' doesn't look like a valid " |
e2785e04f92e
VMM/…: re-indented long queries and error messages.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
223
diff
changeset
|
43 |
u"postfix configuration parameter name.") % |
e2785e04f92e
VMM/…: re-indented long queries and error messages.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
223
diff
changeset
|
44 |
parameter, ERR.VMM_ERROR) |
86
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
45 |
self.__val = self.__read(parameter) |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
46 |
if expand_vars: |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
47 |
self.__expandVars() |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
48 |
return self.__val |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
49 |
|
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
50 |
def __expandVars(self): |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
51 |
while True: |
87
f9090d1a0730
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
86
diff
changeset
|
52 |
pvars = set(self.__varFinder.findall(self.__val)) |
86
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
53 |
pvars_len = len(pvars) |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
54 |
if pvars_len < 1: |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
55 |
break |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
56 |
if pvars_len > 1: |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
57 |
self.__expandMultiVars(self.__readMulti(pvars)) |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
58 |
continue |
89
752d3b57ebb6
small fixes/updates
Pascal Volk <neverseen@users.sourceforge.net>
parents:
87
diff
changeset
|
59 |
pvars = pvars.pop() |
752d3b57ebb6
small fixes/updates
Pascal Volk <neverseen@users.sourceforge.net>
parents:
87
diff
changeset
|
60 |
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
|
61 |
|
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
62 |
def __expandMultiVars(self, old_new): |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
63 |
for old, new in old_new.items(): |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
64 |
self.__val = self.__val.replace('$'+old, new) |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
65 |
|
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
66 |
def __read(self, parameter): |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
67 |
out, err = Popen([self.__bin, '-h', parameter], stdout=PIPE, |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
68 |
stderr=PIPE).communicate() |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
69 |
if len(err): |
216
0c8c053b451c
Moved VirtualMailManager/Exceptions to VirtualMailManager/errors.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
185
diff
changeset
|
70 |
raise VMMError(err.strip(), ERR.VMM_ERROR) |
86
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
71 |
return out.strip() |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
72 |
|
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
73 |
def __readMulti(self, parameters): |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
74 |
cmd = [self.__bin] |
223
5c7b7cbb01cd
replaced some "for x in list …"
Pascal Volk <neverseen@users.sourceforge.net>
parents:
216
diff
changeset
|
75 |
cmd.extend(parameter[1:] for parameter in parameters) |
86
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
76 |
out, err = Popen(cmd, stdout=PIPE, stderr=PIPE).communicate() |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
77 |
if len(err): |
216
0c8c053b451c
Moved VirtualMailManager/Exceptions to VirtualMailManager/errors.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
185
diff
changeset
|
78 |
raise VMMError(err.strip(), ERR.VMM_ERROR) |
86
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
79 |
par_val = {} |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
80 |
for line in out.splitlines(): |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
81 |
par, val = line.split(' = ') |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
82 |
par_val[par] = val |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
83 |
return par_val |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
84 |