/
teaman.py
executable file
·96 lines (85 loc) · 2.67 KB
/
teaman.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
#!/usr/bin/env python
"""
Teaman
Simple console app that alerts you (with sound) when your cup of tea is ready.
by Patrick Mylund Nielsen
https://github.com/pmylund/teaman
"""
__version__ = '1.0'
import sys
import time
import datetime
play_ready_sound = True
ready_sound_file = 'ready.wav'
def main():
argument = None
duration = None
if len(sys.argv) == 2:
argument = sys.argv[1]
if argument:
try:
duration = int(argument)
except:
pass
if not duration:
duration = int(raw_input("How long (in seconds) until your cup of tea is ready? "))
try:
countdownToTea(duration)
except KeyboardInterrupt:
return
def countdownToTea(duration):
print "\r\nYour tea will be ready at %s." % (datetime.datetime.now() + datetime.timedelta(seconds=duration)).strftime('%X')
time.sleep(duration)
print "\r\nYour tea is ready!"
if play_ready_sound:
try:
playSound(ready_sound_file)
except:
pass
answer = raw_input("\r\nHaving another cup? (Y/N/secs) ").lower()
if answer == 'y' or not answer:
countdownToTea(duration)
elif answer == 'n':
return
else:
try:
duration = int(answer)
except:
pass
else:
countdownToTea(duration)
def isLinux():
return 'linux' in sys.platform
def isWindows():
return sys.platform == 'win32'
def playSound(sound_file):
if isWindows():
import winsound
winsound.PlaySound(sound_file, winsound.SND_FILENAME)
elif isLinux():
import subprocess
import errno
try:
FNULL = open('/dev/null', 'w')
subprocess.Popen(['aplay', '-q', sound_file], stdin=FNULL, stdout=FNULL, stderr=FNULL)
except OSError, e:
if e.errno == errno.ENOENT:
from wave import open as waveOpen
from ossaudiodev import open as ossOpen
s = waveOpen(sound_file, 'rb')
(nc, sw, fr, nf, comptype, compname) = s.getparams()
dsp = ossOpen('/dev/dsp', 'w')
try:
from ossaudiodev import AFMT_S16_NE
except ImportError:
if byteorder == "little":
AFMT_S16_NE = ossaudiodev.AFMT_S16_LE
else:
AFMT_S16_NE = ossaudiodev.AFMT_S16_BE
dsp.setparameters(AFMT_S16_NE, nc, fr)
data = s.readframes(nf)
s.close()
dsp.write(data)
dsp.close()
if __name__ == '__main__':
main()