Skip to content

Commit

Permalink
Migrate hamster plugin to use Gio dbus
Browse files Browse the repository at this point in the history
  • Loading branch information
Neui committed Jan 30, 2022
1 parent 2b8297f commit da26160
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 16 deletions.
27 changes: 18 additions & 9 deletions GTG/plugins/hamster/hamster.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@
from calendar import timegm
from gettext import gettext as _

import dbus
from gi.repository import Gtk
from gi.repository import GLib, Gtk, Gio

from GTG.core.task import Task
from GTG.plugins.hamster.helper import FactBuilder
Expand Down Expand Up @@ -72,7 +71,7 @@ def send_task(self, task):
return
fact = FactBuilder(self.hamster, self.preferences).build(task)
start_time = timegm(datetime.datetime.now().timetuple())
hamster_id = self.hamster.AddFact(fact, start_time, 0, False)
hamster_id = self.hamster.AddFact('(siib)', fact, start_time, 0, False)

ids = self.get_hamster_ids(task)
ids.append(str(hamster_id))
Expand All @@ -87,13 +86,21 @@ def get_records(self, task):
valid_ids = []
for i in ids:
try:
fact = self.hamster.GetFact(i)
fact = self.hamster.GetFact('(i)', int(i))
if fact and i not in valid_ids:
records.append(fact)
valid_ids.append(i)
continue
except dbus.DBusException:
pass
except GLib.Error as e:
dbus_error_domain = GLib.quark_to_string(Gio.DBusError.quark())
if e.matches(Gio.io_error_quark(), Gio.IOErrorEnum.DBUS_ERROR) \
or e.domain == dbus_error_domain:
# Imitating the previous code that just caught DBus errors
# Not sure what is was catching for. Trying to give an
# invalid ID results in using that Gio DBUS_ERROR error
pass
else:
raise e
modified = True
if modified:
self.set_hamster_ids(task, valid_ids)
Expand Down Expand Up @@ -122,7 +129,7 @@ def stop_task(self, task_id):
# Hamster deletes an activity if it's finish time is set earlier
# than current time. Hence, we are setting finish time
# some buffer secs from now
self.hamster.StopTracking(now + self.BUFFER_TIME)
self.hamster.StopTracking('(i)', now + self.BUFFER_TIME)
self.tracked_task_id = None

# Datastore ###
Expand Down Expand Up @@ -152,8 +159,10 @@ def on_task_modified(self, task_id, path):
# Plugin api methods ###
def activate(self, plugin_api):
self.plugin_api = plugin_api
self.hamster = dbus.SessionBus().get_object('org.gnome.Hamster',
'/org/gnome/Hamster')
dbus = Gio.bus_get_sync(Gio.BusType.SESSION, None)
self.hamster = Gio.DBusProxy.new_sync(
dbus, Gio.DBusProxyFlags.NONE, None, 'org.gnome.Hamster',
'/org/gnome/Hamster', 'org.gnome.Hamster', None)

# add button
if plugin_api.is_browser():
Expand Down
17 changes: 10 additions & 7 deletions GTG/plugins/hamster/helper.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import re

import dbus
from gi.repository import GLib, Gio


class FactBuilder():
Expand All @@ -25,7 +25,7 @@ def _build_activity_title(self, task):
if self.preferences['activity'] == 'tag':
hamster_activities = {
str(x[0]).lower()
for x in self.hamster.GetActivities('')
for x in self.hamster.GetActivities('(s)', '')
}
activity_candidates = hamster_activities.intersection(gtg_tags)
if len(activity_candidates) >= 1:
Expand All @@ -45,7 +45,7 @@ def _build_category(self, task):
if self.preferences['category'] == 'auto_tag':
hamster_activities = {
str(activity[0]): activity[1]
for activity in self.hamster.GetActivities('')
for activity in self.hamster.GetActivities('(s)', '')
}
if gtg_title in hamster_activities or \
gtg_title.replace(",", "") in hamster_activities:
Expand Down Expand Up @@ -78,13 +78,16 @@ def _build_tags(self, task):
tag_candidates = []
try:
if self.preferences['tags'] == 'existing':
hamster_tags = {str(x[1]) for x in self.hamster.GetTags(False)}
hamster_tags = {str(x[1]) for x in self.hamster.GetTags('(b)', False)}
tag_candidates = list(hamster_tags.intersection(set(gtg_tags)))
elif self.preferences['tags'] == 'all':
tag_candidates = gtg_tags
except dbus.exceptions.DBusException:
# old hamster version, doesn't support tags
pass
except GLib.Error as e:
if e.matches(Gio.DBusError.quark(),
Gio.DBusError.UNKNOWN_METHOD):
pass # old hamster version, doesn't support tags
else:
raise e
tag_str = "".join([" #" + x for x in tag_candidates])
return tag_str

Expand Down

0 comments on commit da26160

Please sign in to comment.