* 'VirtualMailManager/Config.py'
- Config.configure(): Eliminated another UnicodeEncodeError
Thanks to samfisch for reporting this bug (also rev 110)
# -*- coding: UTF-8 -*-# Copyright 2008 VEB IT# See COPYING for distribution information.# $Id$"""A small - r/o - wrapper class for Postfix' postconf."""fromVirtualMailManager.constants.VERSIONimportVERSION__author__='Pascal Volk <p.volk@veb-it.de>'__version__=VERSION__revision__='rev '+'$Rev$'.split()[1]__date__='$Date$'.split()[1]importrefromsubprocessimportPopen,PIPEimportVirtualMailManager.constants.ERRORasERRfromVirtualMailManager.ExceptionsimportVMMExceptionRE_PC_PARAMS="""^\w+$"""RE_PC_VARIABLES=r"""\$\b\w+\b"""classPostconf:def__init__(self,postconf_bin):"""Creates a new Postconf instance. Keyword arguments: postconf_bin -- absolute path to Postfix' postconf binary (str) """self.__bin=postconf_binself.__val=''self.__varFinder=re.compile(RE_PC_VARIABLES)defread(self,parameter,expand_vars=True):"""Returns the parameters value. If expand_vars is True (default), all variables in the value will be expanded: e.g. mydestination -> mail.example.com, localhost.example.com, localhost Otherwise the value may contain one or more variables. e.g. mydestination -> $myhostname, localhost.$mydomain, localhost Keyword arguments: parameter -- the name of a Postfix configuration parameter (str) expand_vars -- default True (bool) """ifnotre.match(RE_PC_PARAMS,parameter):raiseVMMException(_(u'The value »%s« looks not like a valid\ postfix configuration parameter name.')%parameter,ERR.INVALID_AGUMENT)self.__val=self.__read(parameter)ifexpand_vars:self.__expandVars()returnself.__valdef__expandVars(self):whileTrue:pvars=set(self.__varFinder.findall(self.__val))pvars_len=len(pvars)ifpvars_len<1:breakifpvars_len>1:self.__expandMultiVars(self.__readMulti(pvars))continuepvars=pvars.pop()self.__val=self.__val.replace(pvars,self.__read(pvars[1:]))def__expandMultiVars(self,old_new):forold,newinold_new.items():self.__val=self.__val.replace('$'+old,new)def__read(self,parameter):out,err=Popen([self.__bin,'-h',parameter],stdout=PIPE,stderr=PIPE).communicate()iflen(err):raiseException,err.strip()returnout.strip()def__readMulti(self,parameters):cmd=[self.__bin]forparameterinparameters:cmd.append(parameter[1:])out,err=Popen(cmd,stdout=PIPE,stderr=PIPE).communicate()iflen(err):raiseException,err.strip()par_val={}forlineinout.splitlines():par,val=line.split(' = ')par_val[par]=valreturnpar_val