Skip to content

Commit

Permalink
Migrate hamster plugin to use dbus
Browse files Browse the repository at this point in the history
  • Loading branch information
Neui committed May 14, 2021
1 parent d0ad295 commit c622a5d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
17 changes: 9 additions & 8 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 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,12 +86,12 @@ def get_records(self, task):
valid_ids = []
for i in ids:
try:
fact = self.hamster.GetFact(i)
fact = self.hamster.GetFact('(i)', i)
if fact and i not in valid_ids:
records.append(fact)
valid_ids.append(i)
continue
except dbus.DBusException:
except Gio.DBusError:
pass
modified = True
if modified:
Expand Down Expand Up @@ -122,7 +121,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 +151,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, None,
'/org/gnome/Hamster', 'org.gnome.Hamster', None)

# add button
if plugin_api.is_browser():
Expand Down
10 changes: 5 additions & 5 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 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,11 +78,11 @@ 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:
except Gio.DBusError:
# old hamster version, doesn't support tags
pass
tag_str = "".join([" #" + x for x in tag_candidates])
Expand Down

0 comments on commit c622a5d

Please sign in to comment.