VirtualMailManager/ext/Postconf.py
branchv0.6.x
changeset 216 0c8c053b451c
parent 185 6e1ef32fbd82
child 223 5c7b7cbb01cd
equal deleted inserted replaced
215:33f727efa7c4 216:0c8c053b451c
     6 
     6 
     7 import re
     7 import re
     8 from subprocess import Popen, PIPE
     8 from subprocess import Popen, PIPE
     9 
     9 
    10 import VirtualMailManager.constants.ERROR as ERR
    10 import VirtualMailManager.constants.ERROR as ERR
    11 from VirtualMailManager.Exceptions import VMMException
    11 from VirtualMailManager.errors import VMMError
    12 
    12 
    13 RE_PC_PARAMS = """^\w+$"""
    13 RE_PC_PARAMS = """^\w+$"""
    14 RE_PC_VARIABLES = r"""\$\b\w+\b"""
    14 RE_PC_VARIABLES = r"""\$\b\w+\b"""
    15 
    15 
    16 class Postconf(object):
    16 class Postconf(object):
    37         Keyword arguments:
    37         Keyword arguments:
    38         parameter -- the name of a Postfix configuration parameter (str)
    38         parameter -- the name of a Postfix configuration parameter (str)
    39         expand_vars -- default True (bool)
    39         expand_vars -- default True (bool)
    40         """
    40         """
    41         if not re.match(RE_PC_PARAMS, parameter):
    41         if not re.match(RE_PC_PARAMS, parameter):
    42             raise VMMException(_(u'The value ā€œ%sā€ doesn\'t look like a valid\
    42             raise VMMError(_(u'The value ā€œ%sā€ doesn\'t look like a valid\
    43  postfix configuration parameter name.') % parameter, ERR.VMM_ERROR)
    43  postfix configuration parameter name.') % parameter, ERR.VMM_ERROR)
    44         self.__val = self.__read(parameter)
    44         self.__val = self.__read(parameter)
    45         if expand_vars:
    45         if expand_vars:
    46             self.__expandVars()
    46             self.__expandVars()
    47         return self.__val
    47         return self.__val
    64 
    64 
    65     def __read(self, parameter):
    65     def __read(self, parameter):
    66         out, err = Popen([self.__bin, '-h', parameter], stdout=PIPE,
    66         out, err = Popen([self.__bin, '-h', parameter], stdout=PIPE,
    67                 stderr=PIPE).communicate()
    67                 stderr=PIPE).communicate()
    68         if len(err):
    68         if len(err):
    69             raise VMMException(err.strip(), ERR.VMM_ERROR)
    69             raise VMMError(err.strip(), ERR.VMM_ERROR)
    70         return out.strip()
    70         return out.strip()
    71 
    71 
    72     def __readMulti(self, parameters):
    72     def __readMulti(self, parameters):
    73         cmd = [self.__bin]
    73         cmd = [self.__bin]
    74         for parameter in parameters:
    74         for parameter in parameters:
    75             cmd.append(parameter[1:])
    75             cmd.append(parameter[1:])
    76         out, err = Popen(cmd, stdout=PIPE, stderr=PIPE).communicate()
    76         out, err = Popen(cmd, stdout=PIPE, stderr=PIPE).communicate()
    77         if len(err):
    77         if len(err):
    78             raise VMMException(err.strip(), ERR.VMM_ERROR)
    78             raise VMMError(err.strip(), ERR.VMM_ERROR)
    79         par_val = {}
    79         par_val = {}
    80         for line in out.splitlines():
    80         for line in out.splitlines():
    81             par, val = line.split(' = ')
    81             par, val = line.split(' = ')
    82             par_val[par] = val
    82             par_val[par] = val
    83         return par_val
    83         return par_val