VirtualMailManager/Config.py
branchv0.6.x
changeset 204 83938336c518
parent 201 dbb0f7ed7858
child 206 da07dd944ad1
equal deleted inserted replaced
203:4d601240b7db 204:83938336c518
    34     The class for the configuration objects in the ``Config`` class'
    34     The class for the configuration objects in the ``Config`` class'
    35     ``_cfg`` dictionary.
    35     ``_cfg`` dictionary.
    36 """
    36 """
    37 
    37 
    38 
    38 
    39 from shutil import copy2
       
    40 from ConfigParser import \
    39 from ConfigParser import \
    41      Error, MissingSectionHeaderError, NoOptionError, NoSectionError, \
    40      Error, MissingSectionHeaderError, NoOptionError, NoSectionError, \
    42      ParsingError, RawConfigParser
    41      ParsingError, RawConfigParser
    43 from cStringIO import StringIO# TODO: move interactive stff to cli
    42 from cStringIO import StringIO# TODO: move interactive stff to cli
    44 
    43 
    59 
    58 
    60 class NoDefaultError(Error):
    59 class NoDefaultError(Error):
    61     """Raised when the requested option has no default value."""
    60     """Raised when the requested option has no default value."""
    62 
    61 
    63     def __init__(self, section, option):
    62     def __init__(self, section, option):
    64         Error.__init__(self, 'Option %r in section %r has no default value' %(
    63         Error.__init__(self, 'Option %r in section %r has no default value' %
    65                        option, section))
    64                              (option, section))
    66 
    65 
    67 
    66 
    68 class LazyConfig(RawConfigParser):
    67 class LazyConfig(RawConfigParser):
    69     """The **lazy** derivate of the `RawConfigParser`.
    68     """The **lazy** derivate of the `RawConfigParser`.
    70 
    69 
    82     `LazyConfig.set()` differs from ``RawConfigParser``'s ``set()`` method.
    81     `LazyConfig.set()` differs from ``RawConfigParser``'s ``set()`` method.
    83     ``LazyConfig.set()`` takes the ``section`` and ``option`` arguments
    82     ``LazyConfig.set()`` takes the ``section`` and ``option`` arguments
    84     combined to a single string in the form
    83     combined to a single string in the form
    85     "``section``\ **.**\ ``option``".
    84     "``section``\ **.**\ ``option``".
    86     """
    85     """
       
    86 
    87     def __init__(self):
    87     def __init__(self):
    88         RawConfigParser.__init__(self)
    88         RawConfigParser.__init__(self)
    89         self._modified = False
    89         self._modified = False
    90         self._cfg = {
    90         self._cfg = {
    91             'sectionname': {
    91             'sectionname': {
   105         if isinstance(value, bool):
   105         if isinstance(value, bool):
   106             return value
   106             return value
   107         if value.lower() in self._boolean_states:
   107         if value.lower() in self._boolean_states:
   108             return self._boolean_states[value.lower()]
   108             return self._boolean_states[value.lower()]
   109         else:
   109         else:
   110             raise ConfigValueError(_(u'Not a boolean: ā€œ%sā€') %
   110             raise ConfigValueError(_(u"Not a boolean: '%s'") %
   111                                    get_unicode(value))
   111                                    get_unicode(value))
   112 
   112 
   113     def get_boolean(self, section, option):
   113     def get_boolean(self, section, option):
   114         # if the setting was not written to the configuration file, it may
   114         # if the setting was not written to the configuration file, it may
   115         # be still a boolean value - lets see
   115         # be still a boolean value - lets see
   116         if self._modified:
   116         if self._modified:
   117            tmp = self.get(section, option)
   117             tmp = self.get(section, option)
   118            assert isinstance(tmp, bool), 'Oops, not a boolean: %r' % tmp
   118             assert isinstance(tmp, bool), 'Oops, not a boolean: %r' % tmp
   119            return tmp
   119             return tmp
   120         return self.getboolean(section, option)
   120         return self.getboolean(section, option)
   121 
   121 
   122     def _get_section_option(self, section_option):
   122     def _get_section_option(self, section_option):
   123         """splits ``section_option`` (section\ **.**\ option) in two parts
   123         """splits ``section_option`` (section\ **.**\ option) in two parts
   124         and returns them as list ``[section, option]``, if:
   124         and returns them as list ``[section, option]``, if:
   135         """
   135         """
   136         sect_opt = section_option.lower().split('.')
   136         sect_opt = section_option.lower().split('.')
   137         # TODO: cache it
   137         # TODO: cache it
   138         if len(sect_opt) != 2:# do we need a regexp to check the format?
   138         if len(sect_opt) != 2:# do we need a regexp to check the format?
   139             raise BadOptionError(
   139             raise BadOptionError(
   140                         _(u'Bad format: ā€œ%sā€ - expected: section.option') %
   140                         _(u"Bad format: '%s' - expected: section.option") %
   141                         get_unicode(section_option))
   141                                  get_unicode(section_option))
   142         if not sect_opt[0] in self._cfg:
   142         if not sect_opt[0] in self._cfg:
   143             raise NoSectionError(sect_opt[0])
   143             raise NoSectionError(sect_opt[0])
   144         if not sect_opt[1] in self._cfg[sect_opt[0]]:
   144         if not sect_opt[1] in self._cfg[sect_opt[0]]:
   145             raise NoOptionError(sect_opt[1], sect_opt[0])
   145             raise NoOptionError(sect_opt[1], sect_opt[0])
   146         return sect_opt
   146         return sect_opt
   157                     for k in self._cfg[section].iterkeys())
   157                     for k in self._cfg[section].iterkeys())
   158         # still here? Get defaults and merge defaults with configured setting
   158         # still here? Get defaults and merge defaults with configured setting
   159         d = dict((k, self._cfg[section][k].default) \
   159         d = dict((k, self._cfg[section][k].default) \
   160                  for k in self._cfg[section].iterkeys())
   160                  for k in self._cfg[section].iterkeys())
   161         d.update(d2)
   161         d.update(d2)
   162         if '__name__' in d: del d['__name__']
   162         if '__name__' in d:
       
   163             del d['__name__']
   163         return d.iteritems()
   164         return d.iteritems()
   164 
   165 
   165     def dget(self, option):
   166     def dget(self, option):
   166         """Returns the value of the `option`.
   167         """Returns the value of the `option`.
   167 
   168