VMM/{,cli/}Config: fixed imports. Small code cleanups and cosmetic. v0.6.x
authorPascal Volk <neverseen@users.sourceforge.net>
Sat, 13 Feb 2010 03:20:42 +0000
branchv0.6.x
changeset 204 83938336c518
parent 203 4d601240b7db
child 205 bc9726c9ad85
VMM/{,cli/}Config: fixed imports. Small code cleanups and cosmetic.
VirtualMailManager/Config.py
VirtualMailManager/cli/Config.py
--- a/VirtualMailManager/Config.py	Fri Feb 12 09:10:54 2010 +0000
+++ b/VirtualMailManager/Config.py	Sat Feb 13 03:20:42 2010 +0000
@@ -36,7 +36,6 @@
 """
 
 
-from shutil import copy2
 from ConfigParser import \
      Error, MissingSectionHeaderError, NoOptionError, NoSectionError, \
      ParsingError, RawConfigParser
@@ -61,8 +60,8 @@
     """Raised when the requested option has no default value."""
 
     def __init__(self, section, option):
-        Error.__init__(self, 'Option %r in section %r has no default value' %(
-                       option, section))
+        Error.__init__(self, 'Option %r in section %r has no default value' %
+                             (option, section))
 
 
 class LazyConfig(RawConfigParser):
@@ -84,6 +83,7 @@
     combined to a single string in the form
     "``section``\ **.**\ ``option``".
     """
+
     def __init__(self):
         RawConfigParser.__init__(self)
         self._modified = False
@@ -107,16 +107,16 @@
         if value.lower() in self._boolean_states:
             return self._boolean_states[value.lower()]
         else:
-            raise ConfigValueError(_(u'Not a boolean: “%s”') %
+            raise ConfigValueError(_(u"Not a boolean: '%s'") %
                                    get_unicode(value))
 
     def get_boolean(self, section, option):
         # if the setting was not written to the configuration file, it may
         # be still a boolean value - lets see
         if self._modified:
-           tmp = self.get(section, option)
-           assert isinstance(tmp, bool), 'Oops, not a boolean: %r' % tmp
-           return tmp
+            tmp = self.get(section, option)
+            assert isinstance(tmp, bool), 'Oops, not a boolean: %r' % tmp
+            return tmp
         return self.getboolean(section, option)
 
     def _get_section_option(self, section_option):
@@ -137,8 +137,8 @@
         # TODO: cache it
         if len(sect_opt) != 2:# do we need a regexp to check the format?
             raise BadOptionError(
-                        _(u'Bad format: “%s” - expected: section.option') %
-                        get_unicode(section_option))
+                        _(u"Bad format: '%s' - expected: section.option") %
+                                 get_unicode(section_option))
         if not sect_opt[0] in self._cfg:
             raise NoSectionError(sect_opt[0])
         if not sect_opt[1] in self._cfg[sect_opt[0]]:
@@ -159,7 +159,8 @@
         d = dict((k, self._cfg[section][k].default) \
                  for k in self._cfg[section].iterkeys())
         d.update(d2)
-        if '__name__' in d: del d['__name__']
+        if '__name__' in d:
+            del d['__name__']
         return d.iteritems()
 
     def dget(self, option):
--- a/VirtualMailManager/cli/Config.py	Fri Feb 12 09:10:54 2010 +0000
+++ b/VirtualMailManager/cli/Config.py	Sat Feb 13 03:20:42 2010 +0000
@@ -8,16 +8,21 @@
     Adds some interactive stuff to the Config class.
 """
 
+from shutil import copy2
+
 from VirtualMailManager import ENCODING
-from VirtualMailManager.Config import Config 
+from VirtualMailManager.Config import Config, ConfigValueError, LazyConfig
+from VirtualMailManager.Exceptions import VMMConfigException
 from VirtualMailManager.cli import w_std
 from VirtualMailManager.constants.ERROR import VMM_TOO_MANY_FAILURES
 
+
 class CliConfig(Config):
     """Adds the interactive ``configure`` method to the `Config` class
     and overwrites `LazyConfig.set(), in order to update a single option
     in the configuration file with a single command line command.
     """
+
     def configure(self, sections):
         """Interactive method for configuring all options of the given
         iterable ``sections`` object."""
@@ -27,15 +32,15 @@
 
         w_std(_(u'Using configuration file: %s\n') % self._cfgFileName)
         for s in sections:
-            w_std(_(u'* Configuration section: “%s”') % s )
+            w_std(_(u'* Configuration section: %r') % s)
             for opt, val in self.items(s):
                 failures = 0
                 while True:
-                    newval = raw_input(input_fmt.encode(ENCODING,'replace') %{
-                                       'option': opt, 'current_value': val})
+                    newval = raw_input(input_fmt.encode(ENCODING, 'replace') %
+                                       {'option': opt, 'current_value': val})
                     if newval and newval != val:
                         try:
-                            LazyConfig.set('%s.%s' % (s, opt), newval)
+                            LazyConfig.set(self, '%s.%s' % (s, opt), newval)
                             break
                         except (ValueError, ConfigValueError), e:
                             w_std(_(u'Warning: %s') % e)
@@ -43,7 +48,7 @@
                             if failures > 2:
                                 raise VMMConfigException(
                                     _(u'Too many failures - try again later.'),
-                                    VMM_TOO_MANY_FAILURES)
+                                                         VMM_TOO_MANY_FAILURES)
                     else:
                         break
             print
@@ -60,7 +65,7 @@
         ``LazyConfigOption.cls``"""
         section, option_ = self._get_section_option(option)
         val = self._cfg[section][option_].cls(value)
-        if not self._cfg[section][option_].validate is None:
+        if self._cfg[section][option_].validate:
             val = self._cfg[section][option_].validate(val)
         # Do not write default values also skip identical values
         if not self._cfg[section][option_].default is None:
@@ -76,7 +81,7 @@
 
     def __saveChanges(self):
         """Writes changes to the configuration file."""
-        copy2(self._cfgFileName, self._cfgFileName+'.bak')
+        copy2(self._cfgFileName, self._cfgFileName + '.bak')
         self._cfgFile = open(self._cfgFileName, 'w')
         self.write(self._cfgFile)
         self._cfgFile.close()