Better configuration accessor
authorEnrico Zini <enrico@enricozini.org>
Mon, 15 Jun 2009 14:35:54 +0000 (22:35 +0800)
committerEnrico Zini <enrico@enricozini.org>
Mon, 15 Jun 2009 14:36:07 +0000 (22:36 +0800)
src/zavai
zavai/__init__.py
zavai/config.py [new file with mode: 0644]
zavai/gps.py

index b1dcfb7152603f66f2c80a7a41aa98b2c4fa6860..93109994f6de7232288927912a94a15f7dac2891 100755 (executable)
--- a/src/zavai
+++ b/src/zavai
@@ -51,7 +51,7 @@ if not opts.verbose:
 
 # Read configuration
 zavai.info("Loading configuration")
-conf = zavai.read_config(nick="zavai")
+conf = zavai.Config()
 
 # Set up dbus
 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
index 97dd160384eeb8b18b807f639e7d2b993be8fbd2..a510b5229b8b47e2d372bc8a53e951eed0957873 100644 (file)
@@ -17,6 +17,7 @@
 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
 from conf import read_config
+from config import Config
 from plugins import load_plugins
 from registry import Registry, Resource, get_parent, default_label
 from menu import Menu, MenuButton, LinkButton, ToggleButton
diff --git a/zavai/config.py b/zavai/config.py
new file mode 100644 (file)
index 0000000..e328cb8
--- /dev/null
@@ -0,0 +1,23 @@
+import os.path
+import zavai
+
+class Config:
+    def __init__(self):
+        self.conf = zavai.read_config(nick="zavai")
+
+    def get(self, section, name, default=None):
+        if self.conf.has_section(section):
+            if self.conf.has_option(section, name):
+                return self.conf.get(section, name)
+        return None
+
+    def _get_homedir(self):
+        res = self.get("global", "home")
+        if res is None:
+            res = os.path.expanduser("~/.zavai")
+        if not os.path.isdir(res):
+            zavai.info("Creating directory", res)
+            os.makedirs(res)
+        return res
+
+    homedir = property(_get_homedir)
index 47af68fb9f76e6b7970c3cba824be53e12ffa571..fb483aff56429cefdc2d504ad012c10fe4e1e91a 100755 (executable)
@@ -249,13 +249,7 @@ class GPX(zavai.Resource):
         self.last_pos = None
         self.requestors = set()
         conf = registry.resource("conf")
-        self.trackdir = os.path.expanduser("~/.zavai")
-        if conf.has_section("gps"):
-            if conf.has_option("gps", "trackdir"):
-                self.trackdir = conf.get("gps", "trackdir")
-        if not os.path.isdir(self.trackdir):
-            zavai.info("Creating directory", self.trackdir)
-            os.makedirs(self.trackdir)
+        self.trackdir = conf.homedir
         self.activity_monitors = set()
 
     def add_activity_monitor(self, cb):