-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhamster.py
585 lines (427 loc) · 16.1 KB
/
hamster.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
__kupfer_name__ = _("Hamster")
__description__ = _("Control the Hamster time tracker")
__version__ = "2017.03.06"
__author__ = "Jeroen Budts"
__kupfer_actions__ = ("Toggle", "StartActivity", "StartActivityWithTags", "StartActivityWithDescription",
"Overview", "Statistics", "Preferences",)
__kupfer_sources__ = ("HamsterSource", )
import dbus
from kupfer.objects import Action, AppLeaf, Source, Leaf, RunnableLeaf, SourceLeaf, TextLeaf
from kupfer import pretty, plugin_support, icons, uiutils
from kupfer.obj.apps import AppLeafContentMixin
from kupfer.objects import OperationError
from kupfer.weaklib import dbus_signal_connect_weakly
from kupfer import utils
import time
__kupfer_settings__ = plugin_support.PluginSettings(
{
"key": "toplevel_activities",
"label": _("Include activities in top level"),
"type": bool,
"value": True,
},
{
"key": "return_started_facts",
"label": _("When starting an activity immediately re-open Kupfer with the new activity focused. "
"This will let you easily further modify the start and endtime, tags and description."),
"type": bool,
"value": True,
}
)
HAMSTER_APPNAMES = ("hamster-indicator", "hamster-time-tracker", )
plugin_support.check_dbus_connection()
def get_hamster():
try:
bus = dbus.SessionBus()
dbusObj = bus.get_object('org.gnome.Hamster', '/org/gnome/Hamster')
return dbus.Interface(dbusObj, dbus_interface='org.gnome.Hamster')
except dbus.exceptions.DBusException as err:
pretty.print_debug(err)
return None
def format_duration(seconds):
hours, remainder = divmod(seconds, 3600)
minutes, seconds = divmod(remainder, 60)
timestr = '%dmin' % minutes
if hours > 0:
timestr = ('%dh ' % hours) + timestr
return timestr
def format_time(seconds):
tm = time.gmtime(seconds)
return time.strftime("%H:%M", tm)
def format_fact_string(activity, category=None, description=None, tags=None):
fact = activity
if category:
fact += "@" + category
if description or tags:
fact += ','
if description:
fact += description
if tags:
tags = ['#' + str(t) for t in tags]
fact += ' ' + ' '.join(tags)
return fact
def parse_time(timestr):
parsed = time.strptime(timestr, "%H:%M")
now = time.localtime()
result = time.struct_time((now.tm_year, now.tm_mon, now.tm_mday, parsed.tm_hour, parsed.tm_min,
0, now.tm_wday, now.tm_yday, now.tm_isdst))
return get_timestamp(result)
def get_timestamp(time_struct=None):
if not time_struct:
time_struct = time.gmtime()
secs = time.time()
else:
secs = time.mktime(time_struct)
if time.daylight and time_struct.tm_isdst:
secs -= time.altzone
else:
secs -= time.timezone
return secs
class Toggle (Action):
def __init__(self):
Action.__init__(self, _("Open / Close"))
def item_types(self):
yield AppLeaf
def valid_for_item(self, item):
return item.get_id() in HAMSTER_APPNAMES and get_hamster() is not None
def activate(self, leaf):
get_hamster().Toggle()
def get_description(self):
return _("Open or close Hamster")
def get_icon_name(self):
return 'go-jump'
class HamsterCmdAction (Action):
def __init__(self, cmd, name):
Action.__init__(self, name)
self.cmd = cmd
def item_types(self):
yield AppLeaf
def valid_for_item(self, item):
return item.get_id() in HAMSTER_APPNAMES and get_hamster() is not None
def activate(self, leaf):
try:
args = ['hamster-time-tracker', self.cmd]
utils.spawn_async_raise(args)
except utils.SpawnError as exc:
raise OperationError(exc)
class Overview (HamsterCmdAction):
def __init__(self):
HamsterCmdAction.__init__(self, 'overview', _("Show Overview"))
def get_description(self):
return _("Open the overview window of Hamster")
def get_icon_name(self):
return "applications-versioncontrol"
class Statistics (HamsterCmdAction):
def __init__(self):
HamsterCmdAction.__init__(self, 'statistics', _("Show Statistics"))
def get_description(self):
return _("Show the Hamster statistics window")
def get_icon_name(self):
return "emblem-sales"
class Preferences (HamsterCmdAction):
def __init__(self):
HamsterCmdAction.__init__(self, 'preferences', _("Show Preferences"))
def get_description(self):
return _("Show the Hamster preferences window")
def get_icon_name(self):
return "emblem-system"
class StartActivity (Action):
def __init__(self):
Action.__init__(self, _("Start activity"))
def item_types(self):
yield TextLeaf
yield ActivityLeaf
def activate(self, leaf):
fact_id = get_hamster().AddFact(leaf.object, get_timestamp(), 0, False)
if __kupfer_settings__["return_started_facts"]:
fact = get_hamster().GetFact(fact_id)
return FactLeaf(fact)
def get_description(self):
return _("Start tracking the activity in Hamster")
def get_icon_name(self):
return "media-playback-start"
def has_result(self):
return __kupfer_settings__["return_started_facts"]
class StartActivityWithTags (Action):
def __init__(self):
Action.__init__(self, _("Start activity with tags"))
def item_types(self):
yield TextLeaf
yield ActivityLeaf
def activate(self, leaf, iobj):
return self.activate_multiple([leaf], [iobj])
def activate_multiple(self, leafs, iobjs):
# use the first direct object, as it makes no sense to use more than one
# direct object for this action
leaf = leafs[0]
tags = ['#' + str(io.object) for io in iobjs]
fact = leaf.object + ', ' + ' '.join(tags)
pretty.print_debug(__name__, "Adding fact: " + fact)
fact_id = get_hamster().AddFact(fact, get_timestamp(), 0, False)
if __kupfer_settings__["return_started_facts"]:
fact = get_hamster().GetFact(fact_id)
return FactLeaf(fact)
def get_description(self):
return _("Start tracking the activity with tags in Hamster")
def get_icon_name(self):
return "media-playback-start"
def get_gicon(self):
return icons.ComposedIconSmall(self.get_icon_name(), "tag-new")
def requires_object(self):
return True
def object_types(self):
yield TagLeaf
yield TextLeaf
def object_source(self, for_item):
return TagsSource()
def has_result(self):
return __kupfer_settings__["return_started_facts"]
class StartActivityWithDescription (Action):
def __init__(self):
Action.__init__(self, _("Start activity with description"))
def item_types(self):
yield TextLeaf
yield ActivityLeaf
def activate(self, leaf, iobj):
fact_id = get_hamster().AddFact(leaf.object + ', ' + iobj.object, get_timestamp(), 0, False)
if __kupfer_settings__["return_started_facts"]:
fact = get_hamster().GetFact(fact_id)
return FactLeaf(fact)
def get_description(self):
return _("Start tracking the activity with description in Hamster")
def get_icon_name(self):
return "media-playback-start"
def get_gicon(self):
return icons.ComposedIconSmall(self.get_icon_name(), "txt")
def requires_object(self):
return True
def object_types(self):
yield TextLeaf
def has_result(self):
return __kupfer_settings__["return_started_facts"]
class FactEditAction (Action):
'''abstract action to edit properties of a fact'''
def item_types(self):
yield FactLeaf
def has_result(self):
return True
def requires_object(self):
return True
def object_types(self):
yield TextLeaf
def get_icon_name(self):
return "gtk-edit"
def update_fact(self, leaf):
fact = format_fact_string(leaf.activity, leaf.category, leaf.description, leaf.tags)
pretty.print_debug(__name__, "Going to update fact %d: %s" % (leaf.fact_id, fact))
leaf.fact_id = get_hamster().UpdateFact(leaf.fact_id, fact, leaf.starttime, leaf.endtime, False)
return leaf
class ChangeStartTime (FactEditAction):
def __init__(self):
Action.__init__(self, _("Change start time"))
def get_description(self):
return _("Change the start time (format: hh:mm) of a Hamster activty")
def activate(self, leaf, iobj):
leaf.starttime = parse_time(iobj.object)
return self.update_fact(leaf)
def get_gicon(self):
return icons.ComposedIconSmall(self.get_icon_name(), "media-playback-start")
class ChangeEndTime (FactEditAction):
def __init__(self):
Action.__init__(self, _("Change end time"))
def get_description(self):
return _("Change the end time (format: hh:mm) of a Hamster activty")
def activate(self, leaf, iobj):
leaf.endtime = parse_time(iobj.object)
return self.update_fact(leaf)
def get_gicon(self):
return icons.ComposedIconSmall(self.get_icon_name(), "media-playback-stop")
class ChangeDescription (FactEditAction):
def __init__(self):
Action.__init__(self, _("Change the description"))
def get_description(self):
return _("Change the description of a Hamster activity")
def get_gicon(self):
return icons.ComposedIconSmall(self.get_icon_name(), "txt")
def activate(self, leaf, iobj):
leaf.description = iobj.object
return self.update_fact(leaf)
class ChangeTags (FactEditAction):
def __init__(self):
Action.__init__(self, _("Change the tags"))
def get_description(self):
return _("Change the tags of a Hamster activity")
def get_gicon(self):
return icons.ComposedIconSmall(self.get_icon_name(), "tag-new")
def activate(self, leaf, iobj):
return self.activate_multiple([leaf], [iobj])
def activate_multiple(self, leafs, iobjs):
# we only care about the first selected leaf
leaf = leafs[0]
leaf.tags = [str(io.object) for io in iobjs]
return self.update_fact(leaf)
def object_types(self):
yield TagLeaf
yield TextLeaf
def object_source(self, for_item):
return TagsSource()
class Remove (Action):
def __init__(self):
Action.__init__(self, _("Remove"))
def get_description(self):
return _("Remove the Hamster activity")
def get_icon_name(self):
return "remove"
def item_types(self):
yield FactLeaf
def activate(self, leaf):
get_hamster().RemoveFact(leaf.fact_id)
class StopTrackingLeaf (RunnableLeaf):
def __init__(self):
RunnableLeaf.__init__(self, name=_("Stop tracking"))
def get_description(self):
return _("Stop tracking the current activity")
def get_gicon(self):
return icons.ComposedIconSmall("hamster-applet", self.get_icon_name())
def get_icon_name(self):
return "media-playback-stop"
def run(self):
get_hamster().StopTracking(get_timestamp())
class ShowHamsterInfo (RunnableLeaf):
notification_id = 0
def __init__(self):
RunnableLeaf.__init__(self, name=_("Show Hamster Info"))
def get_description(self):
return _("Show hamster information for today")
def get_icon_name(self):
return "info"
def run(self):
facts = get_hamster().GetTodaysFacts()
total = 0
current = None
for f in facts:
end = f[2]
if end == 0:
current = f
end = get_timestamp()
duration = end - f[1]
total += duration
notification_body = "Total time today: %s" % format_duration(total)
if current:
notification_body += "\nCurrent: %s@%s (%s)" % (current[4], current[6],
format_duration(get_timestamp() - current[1]))
ShowHamsterInfo.notification_id = uiutils.show_notification('Hamster Info',
notification_body, 'hamster-indicator', ShowHamsterInfo.notification_id)
class ActivityLeaf (Leaf):
def __init__(self, activity):
Leaf.__init__(self, activity, activity)
def get_actions(self):
yield StartActivity()
yield StartActivityWithTags()
yield StartActivityWithDescription()
def get_icon_name(self):
return "hamster-indicator"
def get_description(self):
return self.name
class TagLeaf (Leaf):
def __init__(self, tag_name):
Leaf.__init__(self, tag_name, tag_name)
def get_icon_name(self):
return "tag-new"
class FactLeaf (Leaf):
def __init__(self, fact):
name = fact[4]
if fact[6]:
name += "@" + fact[6]
Leaf.__init__(self, fact[0], name)
pretty.print_debug(__name__, "creating fact %d: %s" % (fact[0], name))
self.fact_id = fact[0]
self.activity = fact[4]
self.category = fact[6]
self.starttime = fact[1]
self.endtime = fact[2]
self.description = fact[3]
self.tags = fact[7]
def get_icon_name(self):
return "hamster-indicator"
def get_actions(self):
yield ChangeStartTime()
yield ChangeEndTime()
yield ChangeDescription()
yield ChangeTags()
yield Remove()
def get_description(self):
start = format_time(self.starttime)
end = ''
if self.endtime:
end = format_time(self.endtime)
return "%s - %s" % (start, end)
class ActivitiesSource (Source):
def __init__(self):
Source.__init__(self, _("Hamster Activities"))
self.activities = get_hamster().GetActivities('')
def provides(self):
yield ActivityLeaf
def get_items(self):
for act in self.activities:
activity = str(act[0])
if act[1]:
activity += '@' + str(act[1])
yield ActivityLeaf(activity)
def get_icon_name(self):
return "hamster-applet"
def get_description(self):
return _("All known activities in Hamster time tracker")
def get_actions(self):
return ()
class TagsSource (Source):
def __init__(self):
Source.__init__(self, _("Hamster Tags"))
def provides(self):
yield TagLeaf
def get_items(self):
return [TagLeaf(t[1]) for t in get_hamster().GetTags(True)]
class FactsSource (Source):
def __init__(self):
Source.__init__(self, _("Hamster Facts"))
def get_description(self):
return _("Facts for today")
def get_icon_name(self):
return "hamster-applet"
def provides(self):
yield FactLeaf
def get_actions(self):
return ()
def get_items(self):
for fact in get_hamster().GetTodaysFacts():
leaf = FactLeaf(fact)
yield leaf
class HamsterSource (AppLeafContentMixin, Source):
appleaf_content_id = HAMSTER_APPNAMES
def __init__(self):
Source.__init__(self, _("Hamster"))
def _facts_changed(self, *args):
pretty.print_debug(__name__, 'facts changed')
self.mark_for_update()
def initialize(self):
dbus_signal_connect_weakly(dbus.Bus(), 'FactsChanged', self._facts_changed,
dbus_interface='org.gnome.Hamster')
def provides(self):
yield StopTrackingLeaf
yield ShowHamsterInfo
yield SourceLeaf
yield ActivityLeaf
def get_items(self):
yield StopTrackingLeaf()
yield ShowHamsterInfo()
activities_source = ActivitiesSource()
yield SourceLeaf(activities_source)
facts_source = FactsSource()
yield SourceLeaf(facts_source)
if __kupfer_settings__["toplevel_activities"]:
for leaf in activities_source.get_leaves():
yield leaf
def get_description(self):
return _("Hamster time tracker")
def get_icon_name(self):
return "hamster-indicator"