forked from libmapper/webmapper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webmapper.py
executable file
·145 lines (116 loc) · 4.66 KB
/
webmapper.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
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
#!/usr/bin/env python
import webmapper_http_server as server
import mapper
import mapperstorage
import sys, os, os.path, threading, json, re
from random import randint
os.chdir(os.path.dirname(__file__))
if 'tracing' in sys.argv[1:]:
server.tracing = True
def open_gui(port):
url = 'http://localhost:%d'%port
apps = ['~\\AppData\\Local\\Google\\Chrome\\Application\\chrome.exe --app=%s',
'/usr/bin/chromium-browser --app=%s',
]
if 'darwin' in sys.platform:
# Dangerous to run 'open' on platforms other than OS X, so
# check for OS explicitly in this case.
apps = ['open -n -a "Google Chrome" --args --app=%s']
def launch():
try:
import webbrowser, time
time.sleep(0.2)
for a in apps:
a = os.path.expanduser(a)
a = a.replace('\\','\\\\')
if webbrowser.get(a).open(url):
return
webbrowser.open(url)
except:
print 'Error opening web browser, continuing anyway.'
launcher = threading.Thread(target=launch)
launcher.start()
monitor = mapper.monitor()
def on_device(dev, action):
if action == mapper.MDB_NEW:
server.send_command("new_device", dev)
if action == mapper.MDB_REMOVE:
server.send_command("del_device", dev)
def on_signal(sig, action):
if action == mapper.MDB_NEW:
server.send_command("new_signal", sig)
if action == mapper.MDB_REMOVE:
server.send_command("del_signal", sig)
def on_link(link, action):
if action == mapper.MDB_NEW:
server.send_command("new_link", link)
if action == mapper.MDB_REMOVE:
server.send_command("del_link", link)
def on_connection(con, action):
if action == mapper.MDB_NEW:
server.send_command("new_connection", con)
if action == mapper.MDB_MODIFY:
server.send_command("mod_connection", con)
if action == mapper.MDB_REMOVE:
server.send_command("del_connection", con)
def set_connection(con):
if con.has_key('mode'):
con['mode'] = {'bypass': mapper.MO_BYPASS,
'linear': mapper.MO_LINEAR,
'calibrate': mapper.MO_CALIBRATE,
'expression': mapper.MO_EXPRESSION}[con['mode']]
monitor.modify(con)
def on_refresh(arg):
global monitor
del monitor
monitor = mapper.monitor()
init_monitor()
def on_save(arg):
ds = list(monitor.db.match_devices_by_name(arg['dev']))
fn = '/'.join(ds[0]['name'].split('/')[1:])
fn.replace('/','_')
fn = '.'.join(fn.split('.')[:-1]+['json'])
return fn, mapperstorage.serialise(monitor, arg['dev'])
def on_load(mapping_json):
mapperstorage.deserialise(monitor, mapping_json)
def init_monitor():
monitor.request_devices()
monitor.db.add_device_callback(on_device)
monitor.db.add_signal_callback(on_signal)
monitor.db.add_link_callback(on_link)
monitor.db.add_connection_callback(on_connection)
init_monitor()
server.add_command_handler("all_devices",
lambda x: ("all_devices",
list(monitor.db.all_devices())))
server.add_command_handler("all_signals",
lambda x: ("all_signals",
list(monitor.db.all_inputs())
+ list(monitor.db.all_outputs())))
server.add_command_handler("all_links",
lambda x: ("all_links",
list(monitor.db.all_links())))
server.add_command_handler("all_connections",
lambda x: ("all_connections",
list(monitor.db.all_connections())))
server.add_command_handler("set_connection", set_connection)
server.add_command_handler("link",
lambda x: monitor.link(*map(str,x)))
server.add_command_handler("unlink",
lambda x: monitor.unlink(*map(str,x)))
server.add_command_handler("connect",
lambda x: monitor.connect(*map(str,x)))
server.add_command_handler("disconnect",
lambda x: monitor.disconnect(*map(str,x)))
server.add_command_handler("refresh", on_refresh)
server.add_command_handler("save", on_save)
server.add_command_handler("load", on_load)
try:
port = int(sys.argv[sys.argv.index('--port'):][1])
except:
port = randint(49152,65535)
on_open = lambda: ()
if not '--no-browser' in sys.argv and not '-n' in sys.argv:
on_open = lambda: open_gui(port)
server.serve(port=port, poll=lambda: monitor.poll(100), on_open=on_open,
quit_on_disconnect=not '--stay-alive' in sys.argv)