-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
executable file
·70 lines (55 loc) · 1.73 KB
/
main.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
#!/usr/bin/python
import signal, os, glob, imp, sys, threading, logging
import director
logging.basicConfig(level=logging.DEBUG)
def load_scenes():
"""
Load all py files in scene directory
"""
print("Resetting Director")
director.reset()
_cwd = os.path.dirname(os.path.realpath(__file__)) if len(sys.argv) < 2 else sys.argv[1]
print "Loading Scenes from ", _cwd
scenes = glob.glob(os.path.join(_cwd, "scene_*.py"))
# Lets also reload contstants
scenes.insert(0, os.path.join(_cwd, "constants.py"))
for file_path in scenes:
print("loading scene %s" % file_path)
mod_name,file_ext = os.path.splitext(os.path.split(file_path)[-1])
try:
del sys.modules[mod_name]
except KeyError:
pass
scene_mod = imp.load_source(mod_name, file_path)
def process_keyboard():
try:
while True:
channel = sys.stdin.readline()
try:
channel = int(channel)
director.trigger(channel)
except ValueError:
if channel == "r\n":
load_scenes()
else:
pass
except KeyboardInterrupt: # If CTRL+C is pressed, exit cleanly:
print "Process DONE."
# setup a thread to monitor key input
key_thread = threading.Thread(target=process_keyboard)
key_thread.daemon = True
key_thread.start()
# handle SIGTERM
def terminate(signum, frame):
raise KeyboardInterrupt()
signal.signal(signal.SIGTERM, terminate)
# Load the scenes
load_scenes()
print("Press CTRL+C to exit")
try:
while 1:
director.tick()
except KeyboardInterrupt: # If CTRL+C is pressed, exit cleanly:
print "DONE."
finally:
director.cleanup()