Make config file parsing more robust.
[feedingit] / src / config.py
index 2d361ac..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
 # Version     : 0.6.1
 # Description : Simple RSS Reader
 # ============================================================================
+#try:
+#    import gtk
+#    import hildon
+#    from gobject import idle_add
+#except:
+#    pass
 
-import gtk
-import hildon
 from ConfigParser import RawConfigParser
-from gobject import idle_add
 from gconf import client_get_default
 from urllib2 import ProxyHandler
+from mainthread import mainthread
+import logging
+logger = logging.getLogger(__name__)
 
-VERSION = "48"
+VERSION = "52"
 
 section = "FeedingIt"
-ranges = { "updateInterval":[0.5, 1, 2, 4, 12, 24], "expiry":[24, 48, 72], "fontSize":range(12,24), "orientation":["Automatic", "Landscape", "Portrait"], "artFontSize":[10, 12, 14, 16, 18, 20], "feedsort":["Manual", "Most unread", "Least unread", "Most recent", "Least recent"] }
+ranges = { "updateInterval":[0.5, 1, 2, 4, 12, 24], "expiry":[24, 48, 72, 144, 288], "fontSize":range(12,24), "orientation":["Automatic", "Landscape", "Portrait"], "artFontSize":[10, 12, 14, 16, 18, 20], "feedsort":["Manual", "Most unread", "Least unread", "Most recent", "Least recent"] }
 titles = {"updateInterval":"Auto-update interval", "expiry":"Delete articles", "fontSize":"List font size", "orientation":"Display orientation", "artFontSize":"Article font size","feedsort":"Feed sort order"}
 subtitles = {"updateInterval":"Every %s hours", "expiry":"After %s hours", "fontSize":"%s pixels", "orientation":"%s", "artFontSize":"%s pixels", "feedsort":"%s"}
 
@@ -53,7 +60,9 @@ class Config():
         self.window.destroy()
 
     def createDialog(self):
-        
+        import gtk
+        import hildon
+        from gobject import idle_add
         self.window = gtk.Dialog("Settings", self.parent)
         self.window.set_geometry_hints(min_height=600)
 
@@ -154,7 +163,7 @@ class Config():
         # When the dialog is closed without hitting
         # the "Save" button, restore the configuration
         if self.do_restore_backup:
-            print 'Restoring configuration'
+            logger.debug('Restoring configuration')
             self.config = self.config_backup
 
         self.saveConfig()
@@ -170,6 +179,7 @@ class Config():
         self.saveConfig()
         
     def selection_changed(self, selector, button, setting):
+        from gobject import idle_add
         current_selection = selector.get_current_text()
         if current_selection:
             self.config[setting] = current_selection
@@ -181,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)
@@ -240,6 +243,9 @@ class Config():
         file.close()
 
     def create_selector(self, choices, setting):
+        import gtk
+        import hildon
+        from gobject import idle_add
         #self.pickerDialog = hildon.PickerDialog(self.parent)
         selector = hildon.TouchSelector(text=True)
         index = 0
@@ -271,6 +277,7 @@ class Config():
         return ranges["orientation"].index(self.config["orientation"])
     def getImageCache(self):
         return self.config["imageCache"]
+    @mainthread
     def getProxy(self):
         if self.config["proxy"] == False:
             return (False, None)