#!/usr/bin/env python# -*- coding: UTF-8 -*-# opyright 2007-2008 VEB IT# See COPYING for distribution information.# $Id$"""Configurtion class for read, modify and write theconfiguration from Virtual Mail Manager."""__author__='Pascal Volk <p.volk@veb-it.de>'__version__='rev '+'$Rev$'.split()[1]__date__='$Date$'.split()[1]importsysfromshutilimportcopy2fromConfigParserimportConfigParserfromExceptionsimportVMMConfigExceptionimportconstants.EXITasEXITclassVMMConfig(ConfigParser):"""This class is for configure the mailadmin. You can specify settings for the database connection and maildirectories. """missingOptCtr=-1def__init__(self,filename):"""Creates a new VMMConfig instance Keyword arguments: filename -- name of the configuration file """ConfigParser.__init__(self)self.__cfgFileName=filenameself.__cfgFile=Noneself.__VMMsections=['database','maildir','domdir','bin','misc','config']self.__changes=Falseself.__missingSect=[]self.__dbopts=[['host','localhot'],['user','vmm'],['pass','your secret password'],['name','mailsys']]self.__mdopts=[['base','/home/mail'],['folder','Maildir'],['mode',448],['diskusage','false'],['delete','false']]self.__domdopts=[['mode',504],['delete','false']]self.__binopts=[['dovecotpw','/usr/sbin/dovecotpw'],['du','/usr/bin/du']]self.__miscopts=[['passwdscheme','CRAM-MD5'],['gid_mail',8],['forcedel','false']]defload(self):"""Loads the configuration, r/o"""try:self.__cfgFile=file(self.__cfgFileName,'r')except:raiseself.readfp(self.__cfgFile)self.__cfgFile.close()defgetsections(self):"""Return a list with all configurable sections."""returnself.__VMMsections[:-1]defconfigure(self,sections):"""Interactive method for configuring all options in the given section Keyword arguments: sections -- list of strings """ifnotisinstance(sections,list):raiseTypeError("Argument 'sections' is not a list.")# if [config] done = false (default at 1st run),# then set changes truetry:ifnotself.getboolean('config','done'):self.__changes=TrueexceptValueError:self.set('config','done','False')self.__changes=Trueforsinsections:ifs=='config':passelse:print'* Config section: %s'%sforopt,valinself.items(s):newval=raw_input('Enter new value for %s [%s]: '%(opt,val))ifnewvalandnewval!=val:self.set(s,opt,newval)self.__changes=Trueprintifself.__changes:self.__saveChanges()def__saveChanges(self):"""Writes changes to the configuration file."""self.set('config','done','true')copy2(self.__cfgFileName,self.__cfgFileName+'.bak')self.__cfgFile=file(self.__cfgFileName,'w')self.write(self.__cfgFile)self.__cfgFile.close()def__chkSections(self):"""Checks if all configuration sections are existing."""retval=Falseforsinself.__VMMsections:ifnotself.has_section(s):self.__missingSect.append(s)else:retval=self.__chkOptions(s)returnretvaldef__chkOptions(self,section):"""Checks if all configuration options in section are existing. Keyword arguments: section -- the section to be checked """retval=TrueVMMConfig.missingOptCtr+=1self.__missingOpt.append([])ifsection=='database':opts=self.__dboptselifsection=='maildir':opts=self.__mdoptselifsection=='bin':opts=self.__binoptselifsection=='misc':opts=self.__miscoptsforo,vinopts:ifnotself.has_option(section,o):self.__missingOpt[VMMConfig.missingOptCtr].append(o)retval=Falsereturnretval