Make config file parsing more robust.
authorNeal H. Walfield <neal@walfield.org>
Sun, 27 Nov 2011 16:20:26 +0000 (17:20 +0100)
committerNeal H. Walfield <neal@walfield.org>
Sun, 27 Nov 2011 17:00:57 +0000 (18:00 +0100)
src/config.py

index 9f7ec81..21aae9c 100644 (file)
@@ -2,6 +2,7 @@
 
 # 
 # Copyright (c) 2007-2008 INdT.
+# Copyright (c) 2011 Neal H. Walfield.
 # This program is free software: you can redistribute it and/or modify
 # it under the terms of the GNU Lesser General Public License as published by
 # the Free Software Foundation, either version 3 of the License, or
@@ -190,43 +191,36 @@ class Config():
         
     def loadConfig(self):
         self.config = {}
+
+        configParser = RawConfigParser()
         try:
-            configParser = RawConfigParser()
             configParser.read(self.configFilename)
-            self.config["fontSize"] = configParser.getint(section, "fontSize")
-            self.config["artFontSize"] = configParser.getint(section, "artFontSize")
-            self.config["expiry"] = configParser.getint(section, "expiry")
-            self.config["autoupdate"] = configParser.getboolean(section, "autoupdate")
-            self.config["updateInterval"] = configParser.getfloat(section, "updateInterval")
-            self.config["orientation"] = configParser.get(section, "orientation")
-            self.config["imageCache"] = configParser.getboolean(section, "imageCache")
-        except:
-            self.config["fontSize"] = 17
-            self.config["artFontSize"] = 14
-            self.config["expiry"] = 24
-            self.config["autoupdate"] = False
-            self.config["updateInterval"] = 4
-            self.config["orientation"] = "Automatic"
-            self.config["imageCache"] = False
-        try:
-            self.config["proxy"] = configParser.getboolean(section, "proxy")
-        except:
-            self.config["proxy"] = True
-        try:
-            self.config["hidereadfeeds"] = configParser.getboolean(section, "hidereadfeeds")
-            self.config["hidereadarticles"] = configParser.getboolean(section, "hidereadarticles")
-        except:
-            self.config["hidereadfeeds"] = False
-            self.config["hidereadarticles"] = False
-        try:
-            self.config["extBrowser"] = configParser.getboolean(section, "extBrowser")
-        except:
-            self.config["extBrowser"] = False
-        try:
-            self.config["feedsort"] = configParser.get(section, "feedsort")
-        except:
-            self.config["feedsort"] = "Manual"
-        
+        except Exception:
+            logger.exception("Reading %s", self.configFilename)
+
+        # The function to use to fetch the parameter, the parameter's
+        # name and the default value.
+        values = ((configParser.getint, "fontSize", 17),
+                  (configParser.getint, "artFontSize", 14),
+                  (configParser.getint, "expiry", 24),
+                  (configParser.getboolean, "autoupdate", False),
+                  (configParser.getint, "updateInterval", 4),
+                  (configParser.get, "orientation", "Automatic"),
+                  (configParser.getboolean, "imageCache", False),
+                  (configParser.getboolean, "proxy", True),
+                  (configParser.getboolean, "hidereadfeeds", False),
+                  (configParser.getboolean, "hidereadarticles", False),
+                  (configParser.getboolean, "extBrowser", False),
+                  (configParser.get, "feedsort", "Manual"))
+
+        for fetcher, name, default in values:
+            try:
+                v = fetcher(section, name)
+            except Exception:
+                logger.exception("Reading config variable %s", name)
+                v = default
+            self.config[name] = v
+
     def saveConfig(self):
         configParser = RawConfigParser()
         configParser.add_section(section)