[PATCH 08/13] Improve CustomConfig (mainly documentation)

Sebastian Spaeth Sebastian at SSpaeth.de
Wed Nov 24 09:56:45 GMT 2010

1) Inherit from SafeConfigParser
2) Improve documenation about what CustomConfigHelperMixin does
3) Rename CustomConfigDefault and make it class local rather than

Signed-off-by: Sebastian Spaeth <Sebastian at SSpaeth.de>
 offlineimap/CustomConfig.py |   41 +++++++++++++++++++++++++++--------------
 1 files changed, 27 insertions(+), 14 deletions(-)

diff --git a/offlineimap/CustomConfig.py b/offlineimap/CustomConfig.py
index 07be0f6..681f003 100644
--- a/offlineimap/CustomConfig.py
+++ b/offlineimap/CustomConfig.py
@@ -15,11 +15,11 @@
 #    along with this program; if not, write to the Free Software
 #    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
-from ConfigParser import ConfigParser
+from ConfigParser import SafeConfigParser
 from offlineimap.localeval import LocalEval
 import os
-class CustomConfigParser(ConfigParser):
+class CustomConfigParser(SafeConfigParser):
     def getdefault(self, section, option, default, *args, **kwargs):
         """Same as config.get, but returns the "default" option if there
         is no such option specified."""
@@ -70,35 +70,48 @@ class CustomConfigParser(ConfigParser):
         return [x[len(key):] for x in self.sections() \
                 if x.startswith(key)]
-def CustomConfigDefault():
-    """Just a sample constant that won't occur anywhere else to use for the
-    default."""
-    pass
 class ConfigHelperMixin:
+    """Allow comfortable retrieving of config values pertaining to a section.
+    If a class inherits from this class:`ConfigHelperMixin`, it needs
+    to provide 2 functions: method:`getconfig` (returning a
+    ConfigParser object) and method:`getsection` (returning a string
+    which represents the section to look up). All calls to getconf*
+    will then return the configuration values for the ConfigParser
+    object in the specific section."""
+    def ConfigMixin_no_default():
+        """Just a constant that won't occur anywhere else.
+        This allows us to differentiate if the user has given any default
+        value to the getconf* functions in ConfigHelperMixin."""
+        pass
     def _confighelper_runner(self, option, default, defaultfunc, mainfunc):
-        if default != CustomConfigDefault:
-            return apply(defaultfunc, [self.getsection(), option, default])
-        else:
+        """Return config value for getsection()"""
+        if default == ConfigHelperMixin.ConfigMixin_no_default:
             return apply(mainfunc, [self.getsection(), option])
+        else:
+            return apply(defaultfunc, [self.getsection(), option, default])
-    def getconf(self, option, default = CustomConfigDefault):
+    def getconf(self, option,
+                default = ConfigMixin_no_default):
         return self._confighelper_runner(option, default,
-    def getconfboolean(self, option, default = CustomConfigDefault):
+    def getconfboolean(self, option, default = ConfigMixin_no_default):
         return self._confighelper_runner(option, default,
-    def getconfint(self, option, default = CustomConfigDefault):
+    def getconfint(self, option, default = ConfigMixin_no_default):
         return self._confighelper_runner(option, default,
-    def getconffloat(self, option, default = CustomConfigDefault):
+    def getconffloat(self, option, default = ConfigMixin_no_default):
         return self._confighelper_runner(option, default,

More information about the OfflineIMAP-project mailing list