[hamster-applet] monitoring db file for changes and refreshing when appropriate



commit 9f7398895ba399a6b31e12eb65c2e4b009abdab8
Author: Toms Bauģis <toms baugis gmail com>
Date:   Thu Dec 24 12:11:29 2009 +0000

    monitoring db file for changes and refreshing when appropriate

 hamster/configuration.py |   70 +++++++++++++++++++++++++++++++++------------
 hamster/db.py            |    6 ++-
 hamster/storage.py       |    6 +++-
 3 files changed, 60 insertions(+), 22 deletions(-)
---
diff --git a/hamster/configuration.py b/hamster/configuration.py
index a2ab538..5a24b55 100644
--- a/hamster/configuration.py
+++ b/hamster/configuration.py
@@ -26,6 +26,7 @@ from dispatcher import Dispatcher
 from xdg.BaseDirectory import xdg_data_home
 import logging
 import datetime as dt
+import gio
 
 class Singleton(object):
      def __new__(cls, *args, **kwargs):
@@ -37,10 +38,13 @@ class RuntimeStore(Singleton):
     """
     Handles one-shot configuration that is not stored between sessions
     """
-    database_file = ""
+    database_path = ""
+    database_file = None
+    last_etag = None
     data_dir = ""
     dispatcher = None
     storage = None
+    
 
     def __init__(self):
         gettext.install("hamster-applet", unicode = True)
@@ -54,29 +58,57 @@ class RuntimeStore(Singleton):
         self.dispatcher = Dispatcher()
         self.storage = Storage(self.dispatcher)
 
-    def get_art_dir(self):
+        # figure out the correct database file
+        old_db_file = os.path.expanduser("~/.gnome2/hamster-applet/hamster.db")
+        new_db_file = os.path.join(xdg_data_home, "hamster-applet", "hamster.db")
+        
+        if os.path.exists(old_db_file):
+            db_path, _ = os.path.split(os.path.realpath(new_db_file))
+            if not os.path.exists(db_path):
+                try:
+                    os.makedirs(db_path, 0744)
+                except Exception, msg:
+                    logging.error("could not create user dir (%s): %s" % (db_path, msg))
+            if os.path.exists(new_db_file):
+                logging.info("Have two database %s and %s" % (new_db_file, old_db_file))
+            else:
+                os.rename(old_db_file, new_db_file)
+        
+        self.database_path = new_db_file
+
+
+        # add file monitoring so the app does not have to be restarted
+        # when db file is rewritten
+        def on_db_file_change(monitor, gio_file, event_uri, event):
+            if event == gio.FILE_MONITOR_EVENT_CHANGES_DONE_HINT:
+                if gio_file.query_info(gio.FILE_ATTRIBUTE_ETAG_VALUE).get_etag() == self.last_etag:
+                    # ours
+                    return
+                
+                logging.info("DB file has been modified externally. Calling all stations")
+                self.storage.dispatch_overwrite()
+
+
+        
+        self.database_file = gio.File(self.database_path)
+        self.db_monitor = self.database_file.monitor_file()
+        self.db_monitor.connect("changed", on_db_file_change)
+
+
+    def register_modification(self):
+        # db.execute calls this so we know that we were the ones
+        # that modified the DB and no extra refesh is not needed
+        self.last_etag = self.database_file.query_info(gio.FILE_ATTRIBUTE_ETAG_VALUE).get_etag()
+
+
+    @property
+    def art_dir(self):
         return os.path.join(self.data_dir, "art")
 
-    art_dir = property(get_art_dir, None)
 
 runtime = RuntimeStore()
 
-old_db_file = os.path.expanduser("~/.gnome2/hamster-applet/hamster.db")
-new_db_file = os.path.join(xdg_data_home, "hamster-applet", "hamster.db")
-
-if os.path.exists(old_db_file):
-    db_path, _ = os.path.split(os.path.realpath(new_db_file))
-    if not os.path.exists(db_path):
-        try:
-            os.makedirs(db_path, 0744)
-        except Exception, msg:
-            logging.error("could not create user dir (%s): %s" % (db_path, msg))
-    if os.path.exists(new_db_file):
-        logging.info("Have two database %s and %s" % (new_db_file, old_db_file))
-    else:
-        os.rename(old_db_file, new_db_file)
-
-runtime.database_file = new_db_file
+
 
 class GconfStore(Singleton):
     """
diff --git a/hamster/db.py b/hamster/db.py
index 114845e..d924424 100644
--- a/hamster/db.py
+++ b/hamster/db.py
@@ -56,7 +56,7 @@ class Storage(storage.Storage):
 
         from configuration import runtime, GconfStore
 
-        db_file = runtime.database_file
+        db_file = runtime.database_path
         db_path, _ = os.path.split(os.path.realpath(db_file))
 
         if not os.path.exists(db_path):
@@ -814,7 +814,7 @@ class Storage(storage.Storage):
     def get_connection(self):
         from configuration import runtime
         if self.con is None:
-            db_file = runtime.database_file
+            db_file = runtime.database_path
             self.con = sqlite.connect(db_file, detect_types=sqlite.PARSE_DECLTYPES|sqlite.PARSE_COLNAMES)
             self.con.row_factory = sqlite.Row
 
@@ -872,6 +872,8 @@ class Storage(storage.Storage):
         con.commit()
         cur.close()
         
+        runtime.register_modification()
+        
     def run_fixtures(self):
         # defaults
         work_category = {"name": _("Work"),
diff --git a/hamster/storage.py b/hamster/storage.py
index a6f3fea..8cf81a2 100644
--- a/hamster/storage.py
+++ b/hamster/storage.py
@@ -30,7 +30,11 @@ class Storage(object):
 
     def dispatch(self, event, data):
         self.parent.dispatch(event, data)
-        
+
+    def dispatch_overwrite(self):        
+        self.dispatch('new_tags_added', ())
+        self.dispatch('day_updated', ())
+        self.dispatch('activity_updated', ())
 
     def get_tags(self, autocomplete = None):
         return self.__get_tags(autocomplete)



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]