13 from VirtualMailManager.Exceptions import VMMException |
13 from VirtualMailManager.Exceptions import VMMException |
14 |
14 |
15 RE_PC_PARAMS = """^\w+$""" |
15 RE_PC_PARAMS = """^\w+$""" |
16 RE_PC_VARIABLES = r"""\$\b\w+\b""" |
16 RE_PC_VARIABLES = r"""\$\b\w+\b""" |
17 |
17 |
18 class Postconf: |
18 class Postconf(object): |
|
19 __slots__ = ('__bin', '__val', '__varFinder') |
19 def __init__(self, postconf_bin): |
20 def __init__(self, postconf_bin): |
20 """Creates a new Postconf instance. |
21 """Creates a new Postconf instance. |
21 |
22 |
22 Keyword arguments: |
23 Keyword arguments: |
23 postconf_bin -- absolute path to Postfix' postconf binary (str) |
24 postconf_bin -- absolute path to the Postfix postconf binary (str) |
24 """ |
25 """ |
25 self.__bin = postconf_bin |
26 self.__bin = postconf_bin |
26 self.__val = '' |
27 self.__val = '' |
27 self.__varFinder = re.compile(RE_PC_VARIABLES) |
28 self.__varFinder = re.compile(RE_PC_VARIABLES) |
28 |
29 |
29 def read(self, parameter, expand_vars=True): |
30 def read(self, parameter, expand_vars=True): |
30 """Returns the parameters value. |
31 """Returns the parameters value. |
31 |
32 |
32 If expand_vars is True (default), all variables in the value will be |
33 If expand_vars is True (default), all variables in the value will be |
33 expanded: |
34 expanded: |
34 e.g. mydestination -> mail.example.com, localhost.example.com, localhost |
35 e.g. mydestination -> mail.example.com, localhost.example.com, localhost |
35 Otherwise the value may contain one or more variables. |
36 Otherwise the value may contain one or more variables. |
36 e.g. mydestination -> $myhostname, localhost.$mydomain, localhost |
37 e.g. mydestination -> $myhostname, localhost.$mydomain, localhost |
37 |
38 |
39 parameter -- the name of a Postfix configuration parameter (str) |
40 parameter -- the name of a Postfix configuration parameter (str) |
40 expand_vars -- default True (bool) |
41 expand_vars -- default True (bool) |
41 """ |
42 """ |
42 if not re.match(RE_PC_PARAMS, parameter): |
43 if not re.match(RE_PC_PARAMS, parameter): |
43 raise VMMException(_(u'The value »%s« looks not like a valid\ |
44 raise VMMException(_(u'The value »%s« looks not like a valid\ |
44 postfix configuration parameter name.') % parameter, ERR.INVALID_AGUMENT) |
45 postfix configuration parameter name.') % parameter, ERR.VMM_ERROR) |
45 self.__val = self.__read(parameter) |
46 self.__val = self.__read(parameter) |
46 if expand_vars: |
47 if expand_vars: |
47 self.__expandVars() |
48 self.__expandVars() |
48 return self.__val |
49 return self.__val |
49 |
50 |
65 |
66 |
66 def __read(self, parameter): |
67 def __read(self, parameter): |
67 out, err = Popen([self.__bin, '-h', parameter], stdout=PIPE, |
68 out, err = Popen([self.__bin, '-h', parameter], stdout=PIPE, |
68 stderr=PIPE).communicate() |
69 stderr=PIPE).communicate() |
69 if len(err): |
70 if len(err): |
70 raise Exception, err.strip() |
71 raise VMMException(err.strip(), ERR.VMM_ERROR) |
71 return out.strip() |
72 return out.strip() |
72 |
73 |
73 def __readMulti(self, parameters): |
74 def __readMulti(self, parameters): |
74 cmd = [self.__bin] |
75 cmd = [self.__bin] |
75 for parameter in parameters: |
76 for parameter in parameters: |
76 cmd.append(parameter[1:]) |
77 cmd.append(parameter[1:]) |
77 out, err = Popen(cmd, stdout=PIPE, stderr=PIPE).communicate() |
78 out, err = Popen(cmd, stdout=PIPE, stderr=PIPE).communicate() |
78 if len(err): |
79 if len(err): |
79 raise Exception, err.strip() |
80 raise VMMException(err.strip(), ERR.VMM_ERROR) |
80 par_val = {} |
81 par_val = {} |
81 for line in out.splitlines(): |
82 for line in out.splitlines(): |
82 par, val = line.split(' = ') |
83 par, val = line.split(' = ') |
83 par_val[par] = val |
84 par_val[par] = val |
84 return par_val |
85 return par_val |