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
105 lines (81 loc) · 3.06 KB
/
Copy path__init__.py
File metadata and controls
105 lines (81 loc) · 3.06 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
# -*- coding: utf-8 -*-
"""Set up timers. Lists all timers when triggered. Additional arguments in the form of \
[[hours:]minutes:]seconds 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."""
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.0"
__trigger__ = "timer "
__author__ = "Manuel Schneider"
__dependencies__ = []
iconPath = os.path.dirname(__file__)+"/time.svg"
soundPath = os.path.dirname(__file__)+"/bing.wav"
timers = []
class AlbertTimer(Timer):
def __init__(self, interval):
def timeout():
subprocess.Popen(["aplay", soundPath])
global timers
timers.remove(self)
super().__init__(interval=interval, function=timeout)
self.interval = interval
self.begin = int(time())
self.end = self.begin + interval
self.start()
def startTimer(interval):
global timers
timers.append(AlbertTimer(interval))
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():
fields = query.string.strip().split(":")
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:]'" % __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",
icon=iconPath,
completion=query.rawString,
actions=[FuncAction("Set timer", lambda sec=seconds: startTimer(sec))]
)
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)
items.append(Item(
id=__prettyname__,
text="Delete timer <i>[%s]</i>" % identifier,
subtext="Times out %s" % strftime("%X", localtime(timer.end)),
icon=iconPath,
completion=query.rawString,
actions=[FuncAction("Delete timer", lambda timer=timer: deleteTimer(timer))]
))
return items