VirtualMailManager/Config.py
changeset 2 9b39f828aa8a
parent 0 bb0aa2102206
child 4 f3c30b7421b9
equal deleted inserted replaced
1:d08cda9d7c1a 2:9b39f828aa8a
    14 __date__ = '$Date$'.split()[1]
    14 __date__ = '$Date$'.split()[1]
    15 
    15 
    16 import sys
    16 import sys
    17 from shutil import copy2
    17 from shutil import copy2
    18 from ConfigParser import ConfigParser
    18 from ConfigParser import ConfigParser
       
    19 from cStringIO import StringIO
    19 
    20 
    20 from Exceptions import VMMConfigException
    21 from Exceptions import VMMConfigException
    21 import constants.EXIT as EXIT
    22 import constants.ERROR as ERR
    22 
    23 
    23 class VMMConfig(ConfigParser):
    24 class VMMConfig(ConfigParser):
    24     """This class is for configure the mailadmin.
    25     """This class is for configure the Virtual Mail Manager.
    25 
    26 
    26     You can specify settings for the database connection
    27     You can specify settings for the database connection
    27     and maildirectories.
    28     and maildirectories.
    28 
    29 
    29     """
    30     """
    30     missingOptCtr = -1
       
    31 
    31 
    32     def __init__(self, filename):
    32     def __init__(self, filename):
    33         """Creates a new VMMConfig instance
    33         """Creates a new VMMConfig instance
    34 
    34 
    35         Keyword arguments:
    35         Keyword arguments:
    39         self.__cfgFileName = filename
    39         self.__cfgFileName = filename
    40         self.__cfgFile = None
    40         self.__cfgFile = None
    41         self.__VMMsections = ['database', 'maildir', 'domdir', 'bin', 'misc',
    41         self.__VMMsections = ['database', 'maildir', 'domdir', 'bin', 'misc',
    42                 'config']
    42                 'config']
    43         self.__changes = False
    43         self.__changes = False
    44         self.__missingSect = []
    44         self.__missing = {}
    45         self.__dbopts = [
    45         self.__dbopts = [
    46                 ['host', 'localhot'],
    46                 ['host', 'localhot'],
    47                 ['user', 'vmm'],
    47                 ['user', 'vmm'],
    48                 ['pass', 'your secret password'],
    48                 ['pass', 'your secret password'],
    49                 ['name', 'mailsys']
    49                 ['name', 'mailsys']
    75             self.__cfgFile = file(self.__cfgFileName, 'r')
    75             self.__cfgFile = file(self.__cfgFileName, 'r')
    76         except:
    76         except:
    77             raise
    77             raise
    78         self.readfp(self.__cfgFile)
    78         self.readfp(self.__cfgFile)
    79         self.__cfgFile.close()
    79         self.__cfgFile.close()
       
    80 
       
    81     def check(self):
       
    82         if not self.__chkSections():
       
    83             errmsg = StringIO()
       
    84             for k,v in self.__missing.items():
       
    85                 if v[0] is True:
       
    86                     errmsg.write("missing section: %s\n" % k)
       
    87                 else:
       
    88                     errmsg.write("missing options in section %s:\n" % k)
       
    89                     for o in v:
       
    90                         errmsg.write(" * %s\n" % o)
       
    91             raise VMMConfigException((errmsg.getvalue(), ERR.CONF_ERROR))
    80 
    92 
    81     def getsections(self):
    93     def getsections(self):
    82         """Return a list with all configurable sections."""
    94         """Return a list with all configurable sections."""
    83         return self.__VMMsections[:-1]
    95         return self.__VMMsections[:-1]
    84 
    96 
   120         self.write(self.__cfgFile)
   132         self.write(self.__cfgFile)
   121         self.__cfgFile.close()
   133         self.__cfgFile.close()
   122 
   134 
   123     def __chkSections(self):
   135     def __chkSections(self):
   124         """Checks if all configuration sections are existing."""
   136         """Checks if all configuration sections are existing."""
   125         retval = False
   137         errors = False
   126         for s in self.__VMMsections:
   138         for s in self.__VMMsections:
   127             if not self.has_section(s):
   139             if not self.has_section(s):
   128                 self.__missingSect.append(s)
   140                 self.__missing[s] = [True]
   129             else:
   141             elif not self.__chkOptions(s):
   130                 retval = self.__chkOptions(s)
   142                 errors = True
   131         return retval
   143         return not errors
   132 
   144 
   133     def __chkOptions(self, section):
   145     def __chkOptions(self, section):
   134         """Checks if all configuration options in section are existing.
   146         """Checks if all configuration options in section are existing.
   135 
   147 
   136         Keyword arguments:
   148         Keyword arguments:
   137         section -- the section to be checked
   149         section -- the section to be checked
   138         """
   150         """
   139         retval = True
   151         retval = True
   140         VMMConfig.missingOptCtr += 1
   152         missing = []
   141         self.__missingOpt.append([])
       
   142         if section == 'database':
   153         if section == 'database':
   143             opts = self.__dbopts
   154             opts = self.__dbopts
   144         elif section == 'maildir':
   155         elif section == 'maildir':
   145             opts = self.__mdopts
   156             opts = self.__mdopts
       
   157         elif section == 'domdir':
       
   158             opts = self.__domdopts
   146         elif section == 'bin':
   159         elif section == 'bin':
   147             opts = self.__binopts
   160             opts = self.__binopts
   148         elif section == 'misc':
   161         elif section == 'misc':
   149             opts = self.__miscopts
   162             opts = self.__miscopts
       
   163         elif section == 'config':
       
   164             opts = [['done', 'false']]
   150         for o, v in opts:
   165         for o, v in opts:
   151             if not self.has_option(section, o):
   166             if not self.has_option(section, o):
   152                 self.__missingOpt[VMMConfig.missingOptCtr].append(o)
   167                 missing.append(o)
   153                 retval = False
   168                 retval = False
       
   169         if len(missing):
       
   170             self.__missing[section] = missing
   154         return retval
   171         return retval