author | Pascal Volk <neverseen@users.sourceforge.net> |
Tue, 16 Sep 2008 05:55:54 +0000 | |
changeset 86 | e43e4e7e9ec0 |
child 87 | f9090d1a0730 |
permissions | -rw-r--r-- |
86
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
1 |
# -*- coding: UTF-8 -*- |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
2 |
# Copyright 2007-2008 VEB IT |
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 |
# $Id$ |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
5 |
|
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
6 |
"""A small - r/o - wrapper class for Postfix' postconf.""" |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
7 |
|
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
8 |
from VirtualMailManager.constants.VERSION import VERSION |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
9 |
|
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
10 |
__author__ = 'Pascal Volk <p.volk@veb-it.de>' |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
11 |
__version__ = VERSION |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
12 |
__revision__ = 'rev '+'$Rev$'.split()[1] |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
13 |
__date__ = '$Date$'.split()[1] |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
14 |
|
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
15 |
import re |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
16 |
from subprocess import Popen, PIPE |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
17 |
|
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
18 |
import VirtualMailManager.constants.ERROR as ERR |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
19 |
from VirtualMailManager.Exceptions import VMMException |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
20 |
|
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
21 |
RE_PC_PARAMS = """^\w+$""" |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
22 |
RE_PC_VARIABLES = r"""\$\b\w+\b""" |
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 |
class Postconf: |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
25 |
def __init__(self, postconf_bin): |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
26 |
"""Creates a new Postconf instance. |
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 |
Keyword arguments: |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
29 |
postconf_bin -- absolute path to Postfix' postconf binary (str) |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
30 |
""" |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
31 |
self.__bin = postconf_bin |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
32 |
self.__val = '' |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
33 |
re.compile(RE_PC_PARAMS) |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
34 |
re.compile(RE_PC_VARIABLES) |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
35 |
|
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
36 |
def read(self, parameter, expand_vars=True): |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
37 |
"""Returns the parameters value. |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
38 |
|
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
39 |
If expand_vars is True (default), all variables in the value will be |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
40 |
expanded: |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
41 |
e.g. mydestination -> mail.example.com, localhost.example.com, localhost |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
42 |
Otherwise the value may contain one or more variables. |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
43 |
e.g. mydestination -> $myhostname, localhost.$mydomain, localhost |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
44 |
|
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
45 |
Keyword arguments: |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
46 |
parameter -- the name of a Postfix configuration parameter (str) |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
47 |
expand_vars -- default True (bool) |
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 |
if not re.match(RE_PC_PARAMS, parameter): |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
50 |
raise VMMException(_(u'The value »%s« looks not like a valid\ |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
51 |
postfix configuration parameter name.'), ERR.INVALID_AGUMENT) |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
52 |
self.__val = self.__read(parameter) |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
53 |
if expand_vars: |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
54 |
self.__expandVars() |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
55 |
return self.__val |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
56 |
|
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
57 |
def __expandVars(self): |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
58 |
while True: |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
59 |
pvars = set(re.findall(RE_PC_VARIABLES, self.__val)) |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
60 |
pvars_len = len(pvars) |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
61 |
if pvars_len < 1: |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
62 |
break |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
63 |
if pvars_len > 1: |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
64 |
self.__expandMultiVars(self.__readMulti(pvars)) |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
65 |
continue |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
66 |
for var in pvars: |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
67 |
self.__val = self.__val.replace(var, self.__read(var[1:])) |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
68 |
|
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
69 |
def __expandMultiVars(self, old_new): |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
70 |
for old, new in old_new.items(): |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
71 |
self.__val = self.__val.replace('$'+old, new) |
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 __read(self, parameter): |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
74 |
out, err = Popen([self.__bin, '-h', parameter], stdout=PIPE, |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
75 |
stderr=PIPE).communicate() |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
76 |
if len(err): |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
77 |
raise Exception, err.strip() |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
78 |
return out.strip() |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
79 |
|
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
80 |
def __readMulti(self, parameters): |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
81 |
cmd = [self.__bin] |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
82 |
for parameter in parameters: |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
83 |
cmd.append(parameter[1:]) |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
84 |
out, err = Popen(cmd, stdout=PIPE, stderr=PIPE).communicate() |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
85 |
if len(err): |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
86 |
raise Exception, err.strip() |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
87 |
par_val = {} |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
88 |
for line in out.splitlines(): |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
89 |
par, val = line.split(' = ') |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
90 |
par_val[par] = val |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
91 |
return par_val |
e43e4e7e9ec0
* 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
92 |