-
Notifications
You must be signed in to change notification settings - Fork 0
/
buzz.py
executable file
·85 lines (68 loc) · 1.91 KB
/
buzz.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
#!/usr/bin/python
import argparse
import os
import signal
import sys
import traceback
import threading
import config
import messagequeue
import output
import ledoutput
import inputregistry
VERSION="0.90"
def sighandler(signum, frame):
print "Received SIGHUP. Restarting."
# close GPIO, etc.
cleanup()
# restart
args=["python"]
args.extend(sys.argv)
os.execvp(args[0], args)
# should be unrechable
sys.exit(1)
def setup():
signal.signal(signal.SIGHUP, sighandler)
for i in inputregistry.getAll():
it = threading.Thread(target=i.run)
it.start()
ledoutput.setup()
messagequeue.main.send('Startup')
def loop(args):
if args.interactive:
# interactive mode - run output in thread and use this for input
print "Entering interactive mode..."
ot = threading.Thread(target=output.run)
ot.start()
while True:
s = raw_input('> ')
messagequeue.main.send(s)
else:
print "(non-interactive)"
output.run()
def cleanup():
print "Called cleanup()..."
for i in inputregistry.getAll():
i.stop()
ledoutput.stop()
output.stop()
##############################################################################
argp = argparse.ArgumentParser(description='Buzz.')
argp.add_argument('-c', dest='configfile', action='store',
default='buzz.cfg', help='config file (default: buzz.cfg)')
argp.add_argument('-v', '--version', dest='version', action='store_true',
default=False, help='Print version and exit.')
argp.add_argument('-i', '--interactive', dest='interactive', action='store_true',
default=False, help='Interactive mode.')
args = argp.parse_args()
if args.version:
print "buzz.py, version", VERSION
sys.exit(0)
config.load(args.configfile)
try:
setup()
loop(args)
except:
traceback.print_exc()
messagequeue.main.send('Shutdown')
cleanup()