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
127 lines (101 loc) · 4.08 KB
/
Copy path__init__.py
File metadata and controls
127 lines (101 loc) · 4.08 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
119
120
121
122
123
124
125
126
# -*- 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 albertv0 import *
from threading import Timer
from time import strftime, time, localtime
import os
import subprocess
__iid__ = "PythonInterface/v0.1"
__prettyname__ = "Timer"
__version__ = "1.1"
__trigger__ = "timer "
__author__ = "Manuel Schneider, Andreas Preikschat"
__dependencies__ = []
iconPath = os.path.dirname(__file__)+"/time.svg"
soundPath = os.path.dirname(__file__)+"/bing.wav"
timers = []
class AlbertTimer(Timer):
def __init__(self, interval, name):
def timeout():
subprocess.Popen(["aplay", soundPath])
global timers
timers.remove(self)
subtext="Timed out at %s" % strftime("%X", localtime(self.end))
if self.name:
subprocess.Popen(['notify-send', 'Timer "%s"' % self.name, '-t', '0', subtext])
else:
subprocess.Popen(['notify-send', 'Timer', '-t', '0', subtext])
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 formatSeconds(seconds):
m, s = divmod(seconds, 60)
h, m = divmod(m, 60)
return "%02d:%02d:%02d" % (h, m, s)
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=__prettyname__,
text="Invalid input",
subtext="Enter a query in the form of '%s[[hours:]minutes:]seconds [name]'" % __trigger__,
icon=iconPath,
completion=query.rawString
)
seconds = 0
fields.reverse()
for i in range(len(fields)):
seconds += int(fields[i] if fields[i] else 0)*(60**i)
return Item(
id=__prettyname__,
text=formatSeconds(seconds),
subtext='Set a timer with name "%s"' % name if name else 'Set a timer',
icon=iconPath,
completion=query.rawString,
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=__prettyname__,
text='Delete timer <i>%s [%s]</i>' % (timer_name_with_quotes, identifier),
subtext="Times out %s" % strftime("%X", localtime(timer.end)),
icon=iconPath,
completion=query.rawString,
actions=[FuncAction("Delete timer", lambda timer=timer: deleteTimer(timer))]
))
if items:
return items
# Display hint item
return Item(
id=__prettyname__,
text="Add timer",
subtext="Enter a query in the form of '%s[[hours:]minutes:]seconds [name]'" % __trigger__,
icon=iconPath,
completion=query.rawString
)