VirtualMailManager/Config.py
changeset 120 928659c8ee9f
parent 113 e35755191ff3
child 128 cf8116625866
equal deleted inserted replaced
119:014335f38962 120:928659c8ee9f
     1 # -*- coding: UTF-8 -*-
     1 # -*- coding: UTF-8 -*-
     2 # Copyright (c) 2007 - 2009, VEB IT
     2 # Copyright (c) 2007 - 2009, VEB IT
     3 # See COPYING for distribution information.
     3 # See COPYING for distribution information.
     4 
     4 
     5 """Configurtion class for read, modify and write the
     5 """Configuration class for read, modify and write the
     6 configuration from Virtual Mail Manager.
     6 configuration from Virtual Mail Manager.
     7 
     7 
     8 """
     8 """
     9 
     9 
    10 from constants.VERSION import *
    10 from constants.VERSION import *
    25     for arg in args:
    25     for arg in args:
    26         sys.stdout.write(arg.encode(ENCODING, 'replace'))
    26         sys.stdout.write(arg.encode(ENCODING, 'replace'))
    27         sys.stdout.write('\n')
    27         sys.stdout.write('\n')
    28 
    28 
    29 class Config(ConfigParser):
    29 class Config(ConfigParser):
    30     """This class is for configure the Virtual Mail Manager.
    30     """This class is for reading and modifying vmm's configuration file."""
    31 
       
    32     You can specify settings for the database connection
       
    33     and maildirectories.
       
    34     """
       
    35 
    31 
    36     def __init__(self, filename):
    32     def __init__(self, filename):
    37         """Creates a new Config instance
    33         """Creates a new Config instance
    38 
    34 
    39         Keyword arguments:
    35         Arguments:
    40         filename -- name of the configuration file
    36         filename -- path to the configuration file
    41         """
    37         """
    42         ConfigParser.__init__(self)
    38         ConfigParser.__init__(self)
    43         self.__cfgFileName = filename
    39         self.__cfgFileName = filename
    44         self.__cfgFile = None
    40         self.__cfgFile = None
    45         self.__VMMsections = ['database', 'maildir', 'services', 'domdir',
    41         self.__VMMsections = ['database', 'maildir', 'services', 'domdir',
    82                 ['transport', 'dovecot:'],
    78                 ['transport', 'dovecot:'],
    83                 ['dovecotvers', '11']
    79                 ['dovecotvers', '11']
    84                 ]
    80                 ]
    85 
    81 
    86     def load(self):
    82     def load(self):
    87         """Loads the configuration, r/o"""
    83         """Loads the configuration, read only.
       
    84 
       
    85         Raises a VMMConfigException if the configuration syntax is invalid.
       
    86         """
    88         try:
    87         try:
    89             self.__cfgFile = file(self.__cfgFileName, 'r')
    88             self.__cfgFile = file(self.__cfgFileName, 'r')
    90             self.readfp(self.__cfgFile)
    89             self.readfp(self.__cfgFile)
    91         except (MissingSectionHeaderError, ParsingError), e:
    90         except (MissingSectionHeaderError, ParsingError), e:
    92             self.__cfgFile.close()
    91             self.__cfgFile.close()
    93             raise VMMConfigException(str(e), ERR.CONF_ERROR)
    92             raise VMMConfigException(str(e), ERR.CONF_ERROR)
    94         self.__cfgFile.close()
    93         self.__cfgFile.close()
    95 
    94 
    96     def check(self):
    95     def check(self):
       
    96         """Performs a configuration check.
       
    97         
       
    98         Raises a VMMConfigException if the check fails.
       
    99         """
    97         if not self.__chkSections():
   100         if not self.__chkSections():
    98             errmsg = StringIO()
   101             errmsg = StringIO()
    99             errmsg.write(_("Using configuration file: %s\n") %\
   102             errmsg.write(_("Using configuration file: %s\n") %\
   100                     self.__cfgFileName)
   103                     self.__cfgFileName)
   101             for k,v in self.__missing.items():
   104             for k,v in self.__missing.items():
   114     def get(self, section, option, raw=False, vars=None):
   117     def get(self, section, option, raw=False, vars=None):
   115         return unicode(ConfigParser.get(self, section, option, raw, vars),
   118         return unicode(ConfigParser.get(self, section, option, raw, vars),
   116                 ENCODING, 'replace')
   119                 ENCODING, 'replace')
   117 
   120 
   118     def configure(self, sections):
   121     def configure(self, sections):
   119         """Interactive method for configuring all options in the given section
   122         """Interactive method for configuring all options in the given sections
   120 
   123 
   121         Keyword arguments:
   124         Arguments:
   122         sections -- list of strings
   125         sections -- list of strings with section names
   123         """
   126         """
   124         if not isinstance(sections, list):
   127         if not isinstance(sections, list):
   125             raise TypeError("Argument 'sections' is not a list.")
   128             raise TypeError("Argument 'sections' is not a list.")
   126         # if [config] done = false (default at 1st run),
   129         # if [config] done = false (default at 1st run),
   127         # then set changes true
   130         # then set changes true
   165         return not errors
   168         return not errors
   166 
   169 
   167     def __chkOptions(self, section):
   170     def __chkOptions(self, section):
   168         """Checks if all configuration options in section are existing.
   171         """Checks if all configuration options in section are existing.
   169 
   172 
   170         Keyword arguments:
   173         Arguments:
   171         section -- the section to be checked
   174         section -- the section to be checked
   172         """
   175         """
   173         retval = True
   176         retval = True
   174         missing = []
   177         missing = []
   175         if section == 'database':
   178         if section == 'database':