VirtualMailManager/Config.py
branchv0.6.x
changeset 216 0c8c053b451c
parent 215 33f727efa7c4
child 229 0fb2f12648a7
equal deleted inserted replaced
215:33f727efa7c4 216:0c8c053b451c
    14      ParsingError, RawConfigParser
    14      ParsingError, RawConfigParser
    15 from cStringIO import StringIO# TODO: move interactive stff to cli
    15 from cStringIO import StringIO# TODO: move interactive stff to cli
    16 
    16 
    17 from VirtualMailManager import exec_ok, get_unicode, is_dir
    17 from VirtualMailManager import exec_ok, get_unicode, is_dir
    18 from VirtualMailManager.constants.ERROR import CONF_ERROR
    18 from VirtualMailManager.constants.ERROR import CONF_ERROR
    19 from VirtualMailManager.Exceptions import VMMConfigException
    19 from VirtualMailManager.errors import ConfigError
    20 
    20 
    21 
    21 
    22 _ = lambda msg: msg
    22 _ = lambda msg: msg
    23 
    23 
    24 
    24 
   186 
   186 
   187         Throws a `ValueError` if `value` couldn't be converted using
   187         Throws a `ValueError` if `value` couldn't be converted using
   188         `LazyConfigOption.cls`.
   188         `LazyConfigOption.cls`.
   189 
   189 
   190         """
   190         """
       
   191         # pylint: disable-msg=W0221
       
   192         # @pylint: _L A Z Y_
   191         section, option = self._get_section_option(option)
   193         section, option = self._get_section_option(option)
   192         val = self._cfg[section][option].cls(value)
   194         val = self._cfg[section][option].cls(value)
   193         if self._cfg[section][option].validate:
   195         if self._cfg[section][option].validate:
   194             val = self._cfg[section][option].validate(val)
   196             val = self._cfg[section][option].validate(val)
   195         if not RawConfigParser.has_section(self, section):
   197         if not RawConfigParser.has_section(self, section):
   204     def has_option(self, option):
   206     def has_option(self, option):
   205         """Checks if the option (section.option) is a known
   207         """Checks if the option (section.option) is a known
   206         configuration option.
   208         configuration option.
   207 
   209 
   208         """
   210         """
       
   211         # pylint: disable-msg=W0221
       
   212         # @pylint: _L A Z Y_
   209         try:
   213         try:
   210             self._get_section_option(option)
   214             self._get_section_option(option)
   211             return True
   215             return True
   212         except(BadOptionError, NoSectionError, NoOptionError):
   216         except(BadOptionError, NoSectionError, NoOptionError):
   213             return False
   217             return False
   347         }
   351         }
   348 
   352 
   349     def load(self):
   353     def load(self):
   350         """Loads the configuration, read only.
   354         """Loads the configuration, read only.
   351 
   355 
   352         Raises a VMMConfigException if the configuration syntax is
   356         Raises a ConfigError if the configuration syntax is
   353         invalid.
   357         invalid.
   354 
   358 
   355         """
   359         """
   356         try:
   360         try:
   357             self._cfg_file = open(self._cfg_filename, 'r')
   361             self._cfg_file = open(self._cfg_filename, 'r')
   358             self.readfp(self._cfg_file)
   362             self.readfp(self._cfg_file)
   359         except (MissingSectionHeaderError, ParsingError), err:
   363         except (MissingSectionHeaderError, ParsingError), err:
   360             raise VMMConfigException(str(err), CONF_ERROR)
   364             raise ConfigError(str(err), CONF_ERROR)
   361         finally:
   365         finally:
   362             if self._cfg_file and not self._cfg_file.closed:
   366             if self._cfg_file and not self._cfg_file.closed:
   363                 self._cfg_file.close()
   367                 self._cfg_file.close()
   364 
   368 
   365     def check(self):
   369     def check(self):
   366         """Performs a configuration check.
   370         """Performs a configuration check.
   367 
   371 
   368         Raises a VMMConfigException if the check fails.
   372         Raises a ConfigError if the check fails.
   369 
   373 
   370         """
   374         """
   371         # TODO: There are only two settings w/o defaults.
   375         # TODO: There are only two settings w/o defaults.
   372         #       So there is no need for cStringIO
   376         #       So there is no need for cStringIO
   373         if not self.__chk_cfg():
   377         if not self.__chk_cfg():
   377                          self._cfg_filename)
   381                          self._cfg_filename)
   378             for section, options in self.__missing.iteritems():
   382             for section, options in self.__missing.iteritems():
   379                 errmsg.write(_(u'* Section: %s\n') % section)
   383                 errmsg.write(_(u'* Section: %s\n') % section)
   380                 for option in options:
   384                 for option in options:
   381                     errmsg.write((u'    %s\n') % option)
   385                     errmsg.write((u'    %s\n') % option)
   382             raise VMMConfigException(errmsg.getvalue(), CONF_ERROR)
   386             raise ConfigError(errmsg.getvalue(), CONF_ERROR)
   383 
   387 
   384     def known_scheme(self, scheme):
   388     def known_scheme(self, scheme):
   385         """Converts `scheme` to upper case and checks if is known by
   389         """Converts `scheme` to upper case and checks if is known by
   386         Dovecot (listed in VirtualMailManager.SCHEMES).
   390         Dovecot (listed in VirtualMailManager.SCHEMES).
   387 
   391