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