-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalarmclock.py
More file actions
643 lines (592 loc) · 19.6 KB
/
Copy pathalarmclock.py
File metadata and controls
643 lines (592 loc) · 19.6 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
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
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
# coding=utf-8
import sublime
import sublime_plugin
import datetime
import time
import subprocess
import os
class AlarmClockCommand(sublime_plugin.TextCommand):
alarmSettingFile = "AlarmClock.sublime-settings"
storageFormat = "%a %b %d %H:%M:%S %Y"
displayFormat = "%a %b %d %H:%M:%S"
snoozeTimeMins = 9
settings = False
def run(self, edit, action=None, alarmId=None):
action = str(action).lower()
if action == "none" or action == "choose":
self.choose()
if action == "new_alarm":
self.new()
elif action == "edit_alarm":
self.edit(alarmId)
elif action == "list_alarms":
self.list(alarmId)
elif action == "enable_alarm":
self.enable(alarmId)
elif action == "enable_all_alarms":
self.enableAll()
elif action == "disable_alarm":
self.disable(alarmId)
elif action == "disable_all_alarms":
self.disableAll()
elif action == "delete_alarm":
self.delete(alarmId)
elif action == "delete_all_alarms":
self.deleteAll()
elif action == "on_app_start":
self.onAppStart()
else:
return sublime.status_message(
"Unknown action \"%s\"." % action
)
def choose(self):
items = [
"New alarm",
"Edit alarm",
"List alarms",
"Enable alarm",
"Enable all alarms",
"Disable alarm",
"Disable all alarms",
"Delete alarm",
"Delete all alarms"
]
self.show_quick_panel(items, self.chooseType)
def chooseType(self, selection):
if selection == 0:
self.new()
elif selection == 1:
self.edit(None)
elif selection == 2:
self.list(None)
elif selection == 3:
self.enable(None)
elif selection == 4:
self.enableAll()
elif selection == 5:
self.disable(None)
elif selection == 6:
self.disableAll()
elif selection == 7:
self.delete(None)
elif selection == 8:
self.deleteAll()
else:
print("Unknown selection: %s" % selection)
def new(self):
items = [
"Select minutes from now",
"Select hours / minutes from now",
"Select a time hours / minutes",
"Enter a time (HH:MM)"
]
self.show_quick_panel(items, self.handleNew)
def edit(self, alarmId):
if alarmId is -1:
return
if alarmId is None:
items = self.getItems()
if len(items):
self.show_quick_panel(items, self.edit)
else:
sublime.status_message("No alarms to edit")
return self.new()
else:
self.clearLocalVars()
self.command = "edit"
self.alarmId = alarmId
items = [
"Select minutes from now",
"Select hours / minutes from now",
"Select a time hours / minutes",
"Enter a time (HH:MM)"
]
self.show_quick_panel(items, self.handleEdit)
def list(self, alarmId):
if alarmId is -1:
return
if alarmId is None:
items = self.getItems()
if len(items):
self.show_quick_panel(items, self.list)
else:
sublime.status_message("No alarms to list")
else:
self.clearLocalVars()
self.command = "list"
self.alarmId = alarmId
alarms = self.getAlarmSettings()
enabled = "Enable"
try:
self.listEnable = alarms[alarmId]["enabled"] is False
enabled = "Disable" if alarms[alarmId]["enabled"] else "Enable"
except:
print("Can't find enabled.")
displayTime = self.display(alarms[alarmId]["time"])
items = [
"Edit: %s" % displayTime,
"%s: %s" % (enabled, displayTime),
"Delete: %s" % displayTime
]
self.show_quick_panel(items, self.handleList)
def enable(self, alarmId):
if alarmId is -1:
return
if alarmId is None:
items = self.getItems()
if len(items):
self.show_quick_panel(items, self.enable)
else:
sublime.status_message("No alarms to enable")
else:
alarms = self.getAlarmSettings()
try:
alarms[alarmId]["enabled"] = True
msg = "Enabling %s" % self.display(alarms[alarmId]["time"])
print(msg)
sublime.status_message(msg)
self.saveAlarmSettings(alarms)
except:
print("Unable to find alarm to enable")
def disable(self, alarmId):
if alarmId is -1:
return
if alarmId is None:
items = self.getItems()
if len(items):
self.show_quick_panel(items, self.disable)
else:
sublime.status_message("No alarms to disable")
else:
alarms = self.getAlarmSettings()
try:
alarms[alarmId]["enabled"] = False
msg = "Disabling %s" % self.display(alarms[alarmId]["time"])
print(msg)
sublime.status_message(msg)
self.saveAlarmSettings(alarms)
except:
print("Unable to find alarm to disable")
def enableAll(self):
alarms = self.getAlarmSettings()
key = 0
for alarm in alarms:
alarms[key]["enabled"] = True
key += 1
self.saveAlarmSettings(alarms)
msg = "Enabling all"
print(msg)
sublime.status_message(msg)
def disableAll(self):
alarms = self.getAlarmSettings()
key = 0
for alarm in alarms:
alarms[key]["enabled"] = False
key += 1
self.saveAlarmSettings(alarms)
msg = "Disabling all"
print(msg)
sublime.status_message(msg)
def delete(self, alarmId):
if alarmId is -1:
return
if alarmId is None:
items = self.getItems()
if len(items):
self.show_quick_panel(items, self.delete)
else:
msg = "No alarms to delete"
print(msg)
sublime.status_message(msg)
else:
alarms = self.getAlarmSettings()
try:
msg = "Deleting %s" % self.display(alarms[alarmId]["time"])
print(msg)
sublime.status_message(msg)
del alarms[alarmId]
self.saveAlarmSettings(alarms)
except:
print("Unable to find alarm to delete")
def deleteAll(self):
if sublime.ok_cancel_dialog(
"Are you sure you want to delete all alarms?",
"Delete all"
):
self.saveAlarmSettings([])
msg = "All alarms deleted"
print(msg)
sublime.status_message(msg)
def handleNew(self, selection):
self.clearLocalVars()
self.command = "new"
if selection == 0:
self.showMins()
elif selection == 1:
self.showHrs()
elif selection == 2:
self.showHrsDay()
elif selection == 3:
caption = "Enter time (HH:MM)"
self.show_input_panel(
caption,
"",
self.handleTime,
self.handleChange,
self.handleCancel
)
else:
self.clearLocalVars()
return
def handleEdit(self, selection):
if selection == 0:
self.showMins()
elif selection == 1:
self.showHrs()
elif selection == 2:
self.showHrsDay()
elif selection == 3:
caption = "Enter time (HH:MM)"
self.show_input_panel(
caption,
"",
self.handleTime,
self.handleChange,
self.handleCancel
)
else:
self.clearLocalVars()
return
def handleList(self, selection):
if selection == 0:
self.edit(self.alarmId)
elif selection == 1:
if self.listEnable:
self.enable(self.alarmId)
else:
self.disable(self.alarmId)
elif selection == 2:
self.delete(self.alarmId)
else:
self.clearLocalVars()
return
def showMins(self):
items = []
if self.hrs:
for min in range(1, 61):
items.append(
[
"%d minute%s" % (
min,
"" if min is 1 else "s"
),
"and %s hour%s from now" % (
self.hrs,
"" if self.hrs is 1 else "s"
)
]
)
else:
for min in range(1, 61):
items.append(
"%d minute%s from now" % (min, "" if min is 1 else "s")
)
self.show_quick_panel(items, self.handleMins)
def showMinsPast(self):
items = []
for min in range(0, 60):
items.append("%02d past" % (min))
self.show_quick_panel(items, self.handleMinsPast)
def showHrs(self):
items = []
for hr in range(1, 25):
items.append("%d hour%s from now" % (hr, "" if hr is 1 else "s"))
self.show_quick_panel(items, self.handleHrs)
def showHrsDay(self):
items = []
self.hrsNow = int(time.strftime("%H"))
for h in range(0, 24):
items.append("%02d:00" % ((h + self.hrsNow) % 24))
self.show_quick_panel(items, self.handleHrsDay)
def handleMins(self, selection):
if selection is -1:
return
self.mins = selection + 1
if self.hrs:
msg = "New alarm in %s hour%s, %s min%s from now" % (
self.hrs,
"" if self.hrs is 1 else "s",
self.mins,
"" if self.mins is 1 else "s"
)
print(msg)
sublime.status_message(msg)
self.setAlarm(self.hrs, self.mins, False)
else:
msg = "New alarm in %s min%s from now" % (
self.mins,
"" if self.mins is 1 else "s"
)
print(msg)
sublime.status_message(msg)
self.setAlarm(0, self.mins, False)
def handleMinsPast(self, selection):
if selection is -1:
return
self.minsAt = selection
msg = "New alarm at %02d:%02d" % (
self.hrsAt % 24,
self.minsAt
)
print(msg)
sublime.status_message(msg)
self.hrs = (self.hrsAt - int(time.strftime("%H"))) % 24
self.mins = self.minsAt - int(time.strftime("%M"))
if self.mins < 0:
self.mins += 60
self.hrs -= 1
if self.hrs < 0:
self.hrs += 24
if not self.mins + self.hrs:
self.mins = 1
self.setAlarm(self.hrs, self.mins, True)
def handleHrs(self, selection):
if selection is -1:
return
self.hrs = selection + 1
self.showMins()
def handleHrsDay(self, selection):
if selection is -1:
return
self.hrsAt = selection + self.hrsNow
self.showMinsPast()
def handleTime(self, timeOfDay):
if ":" in timeOfDay:
# If colon present then assume we have HH:MM
self.hrsAt = int(timeOfDay[0:timeOfDay.find(":")])
self.minsAt = int(timeOfDay[timeOfDay.find(":") + 1:])
else:
# If no colon then assume it's x mins past hour
if int(timeOfDay) < int(time.strftime("%M")):
self.hrsAt = int(time.strftime("%H")) + 1
else:
self.hrsAt = int(time.strftime("%H"))
self.minsAt = int(timeOfDay)
msg = "New alarm at %02d:%02d" % (
self.hrsAt % 24,
self.minsAt
)
print(msg)
sublime.status_message(msg)
self.hrs = self.hrsAt - int(time.strftime("%H"))
self.mins = self.minsAt - int(time.strftime("%M"))
if self.mins < 0:
self.mins += 60
self.hrs -= 1
if self.hrs < 0:
self.hrs += 24
self.setAlarm(self.hrs, self.mins, True)
def setAlarm(self, hrsD, minsD, at):
msg = "Setting alarm for %s hour%s and %s minute%s in the future." % (
hrsD,
"" if hrsD is 1 else "s",
minsD,
"" if minsD is 1 else "s"
)
print(msg)
sublime.status_message(msg)
secsAway = (hrsD * 3600) + (minsD * 60)
if at:
secsAway -= int(time.strftime("%S"))
sublime.set_timeout(
self.ringMyBell,
secsAway * 1000
)
# store the alarm
alarms = self.getAlarmSettings()
newTime = (
datetime.datetime.now() + datetime.timedelta(0, secsAway)
).strftime(self.storageFormat)
if self.command == "new":
alarms.append(
{"time": newTime, "enabled": True}
)
elif self.command == "edit":
alarms[self.alarmId]["time"] = newTime
self.saveAlarmSettings(alarms)
def getSettings(self):
if not self.settings:
self.settings = sublime.load_settings(self.alarmSettingFile)
return self.settings
def chdirToPluginPath(self):
pluginPath = os.path.dirname(os.path.abspath(__file__))
# ST2 on XP managed to get the path wrong with the above line
if not os.path.exists(os.path.join(pluginPath, "alarmclock.py")):
pluginPath = os.path.join(
sublime.packages_path(),
"AlarmClock"
)
os.chdir(pluginPath)
def getAlarmSettings(self):
return self.getSettings().get("alarms", [])
def saveAlarmSettings(self, alarms):
self.getSettings().set("alarms", alarms)
sublime.save_settings(self.alarmSettingFile)
def getItems(self):
alarms = self.getAlarmSettings()
items = []
if len(alarms):
for alarm in alarms:
items.append(
[
self.display(alarm["time"]),
"Enabled" if alarm["enabled"] else "Disabled"
]
)
return items
def ringMyBell(self):
# Remove from alarm list
alarms = self.getAlarmSettings()
found = False
key = 0
toDel = []
currentTime = time.time()
for alarm in alarms:
alarmTime = time.mktime(
time.strptime(alarm["time"], self.storageFormat)
)
if abs(alarmTime - currentTime) < 55:
if alarm["enabled"]:
found = True
toDel.append(key)
key += 1
if len(toDel):
for d in reversed(toDel):
del alarms[d]
self.saveAlarmSettings(alarms)
if found:
self.startBeeping()
if sublime.ok_cancel_dialog("Alarm time up", "Snooze"):
sublime.set_timeout(
self.snoozeMyBell,
(self.getSettings().get(
"snooze_mins",
self.snoozeTimeMins
) * 60) * 1000
)
self.stopBeeping()
else:
print("Ring my bell hit but no alarm set")
def snoozeMyBell(self):
self.startBeeping()
if sublime.ok_cancel_dialog("Snooze time up", "Snooze again"):
sublime.set_timeout(
self.snoozeMyBell,
(self.getSettings().get(
"snooze_mins",
self.snoozeTimeMins
) * 60) * 1000
)
self.stopBeeping()
def onAppStart(self):
alarms = self.getAlarmSettings()
alarms = self.removeOldAlarms(alarms)
key = 0
currentTime = time.time()
for alarm in alarms:
alarmTime = time.mktime(
time.strptime(alarm["time"], self.storageFormat)
)
sublime.set_timeout(
self.ringMyBell,
(alarmTime - currentTime) * 1000
)
key += 1
self.saveAlarmSettings(alarms)
self.chdirToPluginPath()
platform = sublime.platform()
if platform == "linux":
os.chmod("alarms/linux_alarm.sh", 0o777)
elif platform == "osx":
os.chmod("alarms/osx_alarm.sh", 0o777)
def removeOldAlarms(self, alarms):
key = 0
toDel = []
currentTime = time.time()
for alarm in alarms:
alarmTime = time.mktime(
time.strptime(alarm["time"], self.storageFormat)
)
if alarmTime < currentTime:
toDel.append(key)
key += 1
if len(toDel):
for d in reversed(toDel):
del alarms[d]
return alarms
def startBeeping(self):
self.beeper = False
if self.getSettings().get("audible", True):
self.chdirToPluginPath()
platform = sublime.platform()
if platform == "windows":
# Windows XP, vista, 7
beepCmd = self.getSettings().get("win_alarm_cmd", None)
elif platform == "linux":
# Alsa required
beepCmd = self.getSettings().get("linux_alarm_cmd", None)
elif platform == "osx":
if self.getSettings().get("osx_say", None):
beepCmd = ["say", self.getSettings().get("osx_say", None)]
else:
# OSX 10.5+?
beepCmd = self.getSettings().get("osx_alarm_cmd", None)
else:
print("Platform not supported")
return
self.beeper = subprocess.Popen(beepCmd)
def stopBeeping(self):
if self.beeper:
try:
self.beeper.terminate()
except:
pass
def display(self, t):
return time.strftime(
self.displayFormat,
time.strptime(t, self.storageFormat)
)
def show_quick_panel(self, options, done):
sublime.set_timeout(
lambda: self.view.window().show_quick_panel(options, done),
10
)
def show_input_panel(self, caption, initialtext, done, change, cancel):
sublime.set_timeout(
lambda: self.view.window().show_input_panel(
caption,
initialtext,
done,
change,
cancel
),
10
)
def clearLocalVars(self):
self.command = None
self.mins = None
self.hrs = None
self.minsAt = None
self.hrsAt = None
self.hrsNow = None
def handleChange(self, selection):
return
def handleCancel(self):
return
def plugin_loaded():
sublime.active_window().run_command(
"alarm_clock",
{"action": "on_app_start"}
)
if int(sublime.version()) < 3000:
sublime.set_timeout(lambda: plugin_loaded(), 2000)