[hamster-applet] working around the problem that iteration over sqlite3.Row was added only in python 2.6 (fixes bug 6



commit 018932f9d7eaf6b019eaf66da6b0f25cc10c09e4
Author: Toms Bauģis <toms baugis gmail com>
Date:   Mon Jul 19 14:38:02 2010 +0100

    working around the problem that iteration over sqlite3.Row was added only in python 2.6 (fixes bug 624715)

 src/hamster/client.py  |   13 ++++++++-----
 src/hamster/db.py      |    5 +++--
 src/hamster/storage.py |   36 ++++++++++++++----------------------
 3 files changed, 25 insertions(+), 29 deletions(-)
---
diff --git a/src/hamster/client.py b/src/hamster/client.py
index 7ace197..68127d6 100644
--- a/src/hamster/client.py
+++ b/src/hamster/client.py
@@ -76,6 +76,9 @@ class Storage(gobject.GObject):
         bus.add_signal_receiver(self._on_activities_changed, 'ActivitiesChanged', 'org.gnome.Hamster')
         bus.add_signal_receiver(self._on_toggle_called, 'ToggleCalled', 'org.gnome.Hamster')
 
+    @staticmethod
+    def _to_dict(columns, result_list):
+        return [dict(zip(columns, row)) for row in result_list]
 
     def _on_tags_changed(self):
         self.emit("tags-changed")
@@ -119,15 +122,15 @@ class Storage(gobject.GObject):
            results are sorted by most recent usage.
            search is case insensitive
         """
-        return self.conn.GetActivities(search)
+        return self._to_dict(('name', 'category'), self.conn.GetActivities(search))
 
     def get_categories(self):
         """returns list of categories"""
-        return self.conn.GetCategories()
+        return self._to_dict(('id', 'name'), self.conn.GetCategories())
 
     def get_tags(self, only_autocomplete = False):
         """returns list of all tags. by default only those that have been set for autocomplete"""
-        return self.conn.GetTags(only_autocomplete)
+        return self._to_dict(('id', 'name', 'autocomplete'), self.conn.GetTags(only_autocomplete))
 
 
     def get_tag_ids(self, tags):
@@ -137,7 +140,7 @@ class Storage(gobject.GObject):
            be created.
            on database changes the `tags-changed` signal is emitted.
         """
-        return self.conn.GetTagIds(tags)
+        return self._to_dict(('id', 'name', 'autocomplete'), self.conn.GetTagIds(tags))
 
     def update_autocomplete_tags(self, tags):
         """update list of tags that should autocomplete. this list replaces
@@ -218,7 +221,7 @@ class Storage(gobject.GObject):
         """Return activities for category. If category is not specified, will
         return activities that have no category"""
         category_id = category_id or -1
-        return self.conn.GetCategoryActivities(category_id)
+        return self._to_dict(('id', 'name', 'category_id', 'category'), self.conn.GetCategoryActivities(category_id))
 
     def get_category_id(self, category_name):
         """returns category id by name"""
diff --git a/src/hamster/db.py b/src/hamster/db.py
index 6b7822b..aac0532 100644
--- a/src/hamster/db.py
+++ b/src/hamster/db.py
@@ -241,7 +241,8 @@ class Storage(storage.Storage):
             res = self.fetchone(query, (_("Unsorted"), name, ))
 
         if res:
-            res = dict(res)
+            keys = ('id', 'name', 'deleted', 'category')
+            res = dict([(key, res[key]) for key in keys])
             res['deleted'] = res['deleted'] or False
 
             # if the activity was marked as deleted, resurrect on first call
@@ -450,7 +451,7 @@ class Storage(storage.Storage):
         tags = [tag.strip() for tag in tags.split(",") if tag.strip()]  # split by comma
         tags = tags or activity.tags # explicitly stated tags take priority
 
-        tags = self.GetTagIds(tags) #this will create any missing tags too
+        tags = [dict(zip(('id', 'name', 'autocomplete'), row)) for row in self.GetTagIds(tags)] #this will create any missing tags too
 
 
         if category_name:
diff --git a/src/hamster/storage.py b/src/hamster/storage.py
index 53e26fb..469f74f 100644
--- a/src/hamster/storage.py
+++ b/src/hamster/storage.py
@@ -235,9 +235,9 @@ class Storage(dbus.service.Object):
         self.ActivitiesChanged()
 
 
-    @dbus.service.method("org.gnome.Hamster", out_signature='aa{sv}')
+    @dbus.service.method("org.gnome.Hamster", out_signature='a(is)')
     def GetCategories(self):
-        return [dict(category) for category in self.__get_categories()]
+        return [(category['id'], category['name']) for category in self.__get_categories()]
 
 
     # activities
@@ -262,27 +262,19 @@ class Storage(dbus.service.Object):
         self.ActivitiesChanged()
         return result
 
-    @dbus.service.method("org.gnome.Hamster", in_signature='i', out_signature='aa{sv}')
+    @dbus.service.method("org.gnome.Hamster", in_signature='i', out_signature='a(isis)')
     def GetCategoryActivities(self, category_id = -1):
-        res = []
-        for activity in self.__get_category_activities(category_id = category_id):
-            activity = dict(activity)
-            activity['category'] = activity['category'] or ''
 
-            res.append(activity)
-
-        return res
+        return [(row['id'],
+                 row['name'],
+                 row['category_id'],
+                 row['name'] or '') for row in
+                      self.__get_category_activities(category_id = category_id)]
 
 
-    @dbus.service.method("org.gnome.Hamster", in_signature='s', out_signature='aa{sv}')
+    @dbus.service.method("org.gnome.Hamster", in_signature='s', out_signature='a(ss)')
     def GetActivities(self, search = ""):
-        res = []
-        for activity in self.__get_activities(search):
-            activity = dict(activity)
-            activity['category'] = activity['category'] or ''
-            res.append(activity)
-
-        return res
+        return [(row['name'], row['category'] or '') for row in self.__get_activities(search)]
 
 
     @dbus.service.method("org.gnome.Hamster", in_signature='ii', out_signature = 'b')
@@ -303,17 +295,17 @@ class Storage(dbus.service.Object):
             return {}
 
     # tags
-    @dbus.service.method("org.gnome.Hamster", in_signature='b', out_signature='aa{sv}')
+    @dbus.service.method("org.gnome.Hamster", in_signature='b', out_signature='a(isb)')
     def GetTags(self, only_autocomplete):
-        return [dict(tag) for tag in self.__get_tags(only_autocomplete)]
+        return [(tag['id'], tag['name'], tag['autocomplete']) for tag in self.__get_tags(only_autocomplete)]
 
 
-    @dbus.service.method("org.gnome.Hamster", in_signature='as', out_signature='aa{sv}')
+    @dbus.service.method("org.gnome.Hamster", in_signature='as', out_signature='a(isb)')
     def GetTagIds(self, tags):
         tags, new_added = self.__get_tag_ids(tags)
         if new_added:
             self.TagsChanged()
-        return [dict(tag) for tag in tags]
+        return [(tag['id'], tag['name'], tag['autocomplete']) for tag in tags]
 
 
     @dbus.service.method("org.gnome.Hamster", in_signature='s')



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