forked from albertlauncher/python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__init__.py
More file actions
118 lines (96 loc) · 3.94 KB
/
Copy path__init__.py
File metadata and controls
118 lines (96 loc) · 3.94 KB
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
# -*- coding: utf-8 -*-
"""Set up timers.
Lists all timers when triggered. Additional arguments in the form of "[[hours:]minutes:]seconds
[name]" let you set triggers. Empty field resolve to 0, e.g. "96::" starts a 96 hours timer.
Fields exceeding the maximum amount of the time interval are automatically refactorized, e.g.
"9:120:3600" resolves to 12 hours.
Synopsis: <trigger> [[[hours]:][minutes]:]seconds [name]"""
from albert import warning, Item, FuncAction
from threading import Timer
from time import strftime, time, localtime
import dbus
import os
from datetime import timedelta
import subprocess
__title__ = "Timer"
__version__ = "0.4.3"
__triggers__ = "timer "
__authors__ = ["manuelschneid3r", "googol42"]
__py_deps__ = ["dbus"]
iconPath = os.path.dirname(__file__)+"/time.svg"
soundPath = os.path.dirname(__file__)+"/bing.wav"
timers = []
bus_name = "org.freedesktop.Notifications"
object_path = "/org/freedesktop/Notifications"
interface = bus_name
class AlbertTimer(Timer):
def __init__(self, interval, name):
def timeout():
subprocess.Popen(["aplay", soundPath])
global timers
timers.remove(self)
title = 'Timer "%s"' % self.name if self.name else 'Timer'
text = "Timed out at %s" % strftime("%X", localtime(self.end))
notify = dbus.Interface(dbus.SessionBus().get_object(bus_name, object_path), interface)
notify.Notify(__title__, 0, iconPath, title, text, [], {"urgency":2}, 0)
super().__init__(interval=interval, function=timeout)
self.interval = interval
self.name = name
self.begin = int(time())
self.end = self.begin + interval
self.start()
def startTimer(interval, name):
global timers
timers.append(AlbertTimer(interval, name))
def deleteTimer(timer):
global timers
timers.remove(timer)
timer.cancel()
def handleQuery(query):
if query.isTriggered:
if query.string.strip():
args = query.string.strip().split(maxsplit=1)
fields = args[0].split(":")
name = args[1] if 1 < len(args) else ''
if not all(field.isdigit() or field == '' for field in fields):
return Item(
id=__title__,
text="Invalid input",
subtext="Enter a query in the form of '%s[[hours:]minutes:]seconds [name]'" % __triggers__,
icon=iconPath
)
seconds = 0
fields.reverse()
for i in range(len(fields)):
seconds += int(fields[i] if fields[i] else 0)*(60**i)
return Item(
id=__title__,
text=str(timedelta(seconds=seconds)),
subtext='Set a timer with name "%s"' % name if name else 'Set a timer',
icon=iconPath,
actions=[FuncAction("Set timer", lambda sec=seconds: startTimer(sec, name))]
)
else:
# List timers
items = []
for timer in timers:
m, s = divmod(timer.interval, 60)
h, m = divmod(m, 60)
identifier = "%d:%02d:%02d" % (h, m, s)
timer_name_with_quotes = '"%s"' % timer.name if timer.name else ''
items.append(Item(
id=__title__,
text='Delete timer <i>%s [%s]</i>' % (timer_name_with_quotes, identifier),
subtext="Times out %s" % strftime("%X", localtime(timer.end)),
icon=iconPath,
actions=[FuncAction("Delete timer", lambda timer=timer: deleteTimer(timer))]
))
if items:
return items
# Display hint item
return Item(
id=__title__,
text="Add timer",
subtext="Enter a query in the form of '%s[[hours:]minutes:]seconds [name]'" % __triggers__,
icon=iconPath
)