-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
executable file
·1184 lines (984 loc) · 39.2 KB
/
server.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
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
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright 2017-present, Facebook, Inc.
# All rights reserved.
#
# This source code is licensed under the license found in the
# LICENSE file in the root directory of this source tree.
"""Server"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
import argparse
import copy
import getpass
import hashlib
import inspect
import json
import logging
import math
import os
import time
import traceback
from os.path import expanduser
import visdom
from zmq.eventloop import ioloop
ioloop.install() # Needs to happen before any tornado imports!
import tornado.ioloop # noqa E402: gotta install ioloop first
import tornado.web # noqa E402: gotta install ioloop first
import tornado.websocket # noqa E402: gotta install ioloop first
import tornado.escape # noqa E402: gotta install ioloop first
LAYOUT_FILE = 'layouts.json'
DEFAULT_ENV_PATH = '%s/.visdom/' % expanduser("~")
DEFAULT_PORT = 8097
here = os.path.abspath(os.path.dirname(__file__))
def get_rand_id():
return str(hex(int(time.time() * 10000000))[2:])
def ensure_dir_exists(path):
"""Make sure the parent dir exists for path so we can write a file."""
try:
os.makedirs(os.path.dirname(path))
except OSError as e1:
assert e1.errno == 17 # errno.EEXIST
pass
def get_path(filename):
"""Get the path to an asset."""
cwd = os.path.dirname(
os.path.abspath(inspect.getfile(inspect.currentframe())))
return os.path.join(cwd, filename)
def escape_eid(eid):
"""Replace slashes with underscores, to avoid recognizing them
as directories.
"""
return eid.replace('/', '_')
def extract_eid(args):
"""Extract eid from args. If eid does not exist in args,
it returns 'main'."""
eid = 'main' if args.get('eid') is None else args.get('eid')
return escape_eid(eid)
def set_cookie():
"""Create cookie secret key for authentication"""
cookie_secret = input("Please input your cookie secret key here: ")
with open(DEFAULT_ENV_PATH + "COOKIE_SECRET", "w") as cookie_file:
cookie_file.write(cookie_secret)
def hash_password(password):
"""Hashing Password with SHA-256"""
return hashlib.sha256(password.encode("utf-8")).hexdigest()
tornado_settings = {
"autoescape": None,
"debug": "/dbg/" in __file__,
"static_path": get_path('static'),
"template_path": get_path('static'),
"compiled_template_cache": False
}
def serialize_env(state, eids, env_path=DEFAULT_ENV_PATH):
env_ids = [i for i in eids if i in state]
for env_id in env_ids:
env_path_file = os.path.join(env_path, "{0}.json".format(env_id))
open(env_path_file, 'w').write(json.dumps(state[env_id]))
return env_ids
def serialize_all(state, env_path=DEFAULT_ENV_PATH):
serialize_env(state, list(state.keys()), env_path=env_path)
class Application(tornado.web.Application):
def __init__(self, port=DEFAULT_PORT, env_path=DEFAULT_ENV_PATH,
readonly=False, user_credential=None):
self.state = {}
self.subs = {}
self.sources = {}
self.env_path = env_path
self.port = port
self.readonly = readonly
self.user_credential = user_credential
self.login_enabled = False
if user_credential:
self.login_enabled = True
tornado_settings["cookie_secret"] = \
open(DEFAULT_ENV_PATH + "COOKIE_SECRET", "r").read()
# reload state
ensure_dir_exists(env_path)
env_jsons = [i for i in os.listdir(env_path) if '.json' in i]
for env_json in env_jsons:
env_path_file = os.path.join(env_path, env_json)
env_data = tornado.escape.json_decode(open(env_path_file, 'r').read())
eid = env_json.replace('.json', '')
self.state[eid] = {'jsons': env_data['jsons'],
'reload': env_data['reload']}
if 'main' not in self.state and 'main.json' not in env_jsons:
self.state['main'] = {'jsons': {}, 'reload': {}}
serialize_env(self.state, ['main'], env_path=self.env_path)
handlers = [
(r"/events", PostHandler, {'app': self}),
(r"/update", UpdateHandler, {'app': self}),
(r"/close", CloseHandler, {'app': self}),
(r"/socket", SocketHandler, {'app': self}),
(r"/vis_socket", VisSocketHandler, {'app': self}),
(r"/env/(.*)", EnvHandler, {'app': self}),
(r"/compare/(.*)", CompareHandler, {'app': self}),
(r"/save", SaveHandler, {'app': self}),
(r"/error/(.*)", ErrorHandler, {'app': self}),
(r"/win_exists", ExistsHandler, {'app': self}),
(r"/win_data", DataHandler, {'app': self}),
(r"/delete_env", DeleteEnvHandler, {'app': self}),
(r"/win_hash", HashHandler, {'app': self}),
(r"/(.*)", IndexHandler, {'app': self}),
]
super(Application, self).__init__(handlers, **tornado_settings)
def broadcast_envs(handler, target_subs=None):
if target_subs is None:
target_subs = handler.subs.values()
for sub in target_subs:
sub.write_message(json.dumps(
{'command': 'env_update', 'data': list(handler.state.keys())}
))
def send_to_sources(handler, msg):
target_sources = handler.sources.values()
for source in target_sources:
source.write_message(json.dumps(msg))
class VisSocketHandler(tornado.websocket.WebSocketHandler):
def initialize(self, app):
self.state = app.state
self.subs = app.subs
self.sources = app.sources
self.port = app.port
self.env_path = app.env_path
def check_origin(self, origin):
return True
def open(self):
self.sid = str(hex(int(time.time() * 10000000))[2:])
if self not in list(self.sources.values()):
self.eid = 'main'
self.sources[self.sid] = self
logging.info('Opened visdom socket from ip: {}'.format(
self.request.remote_ip))
self.write_message(
json.dumps({'command': 'alive', 'data': 'vis_alive'}))
def on_message(self, message):
logging.info('from visdom client: {}'.format(message))
msg = tornado.escape.json_decode(tornado.escape.to_basestring(message))
cmd = msg.get('cmd')
if cmd == 'echo':
for sub in self.sources.values():
sub.write_message(json.dumps(msg))
def on_close(self):
if self in list(self.sources.values()):
self.sources.pop(self.sid, None)
class SocketHandler(tornado.websocket.WebSocketHandler):
def initialize(self, app):
self.port = app.port
self.env_path = app.env_path
self.layouts = self.load_layouts()
self.state = app.state
self.subs = app.subs
self.sources = app.sources
self.broadcast_layouts()
self.readonly = app.readonly
def check_origin(self, origin):
return True
def broadcast_layouts(self, target_subs=None):
if target_subs is None:
target_subs = self.subs.values()
for sub in target_subs:
sub.write_message(json.dumps(
{'command': 'layout_update', 'data': self.layouts}
))
def save_layouts(self):
layout_filepath = os.path.join(self.env_path, 'view', LAYOUT_FILE)
with open(layout_filepath, 'w') as fn:
fn.write(self.layouts)
def load_layouts(self):
layout_filepath = os.path.join(self.env_path, 'view', LAYOUT_FILE)
ensure_dir_exists(layout_filepath)
if os.path.isfile(layout_filepath):
with open(layout_filepath, 'r') as fn:
return fn.read()
else:
return ""
def open(self):
self.sid = str(hex(int(time.time() * 10000000))[2:])
if self not in list(self.subs.values()):
self.eid = 'main'
self.subs[self.sid] = self
logging.info(
'Opened new socket from ip: {}'.format(self.request.remote_ip))
self.write_message(
json.dumps({'command': 'register', 'data': self.sid,
'readonly': self.readonly}))
self.broadcast_layouts([self])
broadcast_envs(self, [self])
def on_message(self, message):
logging.info('from web client: {}'.format(message))
msg = tornado.escape.json_decode(tornado.escape.to_basestring(message))
cmd = msg.get('cmd')
if self.readonly:
return
if cmd == 'close':
if 'data' in msg and 'eid' in msg:
logging.info('closing window {}'.format(msg['data']))
p_data = self.state[msg['eid']]['jsons'].pop(msg['data'], None)
event = {
'event_type': 'Close',
'target': msg['data'],
'eid': msg['eid'],
'pane_data': p_data,
}
send_to_sources(self, event)
elif cmd == 'save':
# save localStorage window metadata
if 'data' in msg and 'eid' in msg:
msg['eid'] = escape_eid(msg['eid'])
self.state[msg['eid']] = \
copy.deepcopy(self.state[msg['prev_eid']])
self.state[msg['eid']]['reload'] = msg['data']
self.eid = msg['eid']
serialize_env(self.state, [self.eid], env_path=self.env_path)
elif cmd == 'delete_env':
if 'eid' in msg:
logging.info('closing environment {}'.format(msg['eid']))
del self.state[msg['eid']]
p = os.path.join(self.env_path, "{0}.json".format(msg['eid']))
os.remove(p)
broadcast_envs(self)
elif cmd == 'save_layouts':
if 'data' in msg:
self.layouts = msg.get('data')
self.save_layouts()
self.broadcast_layouts()
elif cmd == 'forward_to_vis':
packet = msg.get('data')
environment = self.state[packet['eid']]
packet['pane_data'] = environment['jsons'][packet['target']]
send_to_sources(self, msg.get('data'))
def on_close(self):
if self in list(self.subs.values()):
self.subs.pop(self.sid, None)
class BaseHandler(tornado.web.RequestHandler):
def __init__(self, *request, **kwargs):
self.include_host = False
super(BaseHandler, self).__init__(*request, **kwargs)
def get_current_user(self):
"""
This method determines the self.current_user
based the value of cookies that set in POST method
at IndexHandler by self.set_secure_cookie
"""
try:
return self.get_secure_cookie("user_password")
except Exception: # Not using secure cookies
return None
def write_error(self, status_code, **kwargs):
logging.error("ERROR: %s: %s" % (status_code, kwargs))
if self.settings.get("debug") and "exc_info" in kwargs:
logging.error("rendering error page")
import traceback
exc_info = kwargs["exc_info"]
# exc_info is a tuple consisting of:
# 1. The class of the Exception
# 2. The actual Exception that was thrown
# 3. The traceback opbject
try:
params = {
'error': exc_info[1],
'trace_info': traceback.format_exception(*exc_info),
'request': self.request.__dict__
}
self.render("error.html", **params)
logging.error("rendering complete")
except Exception as e:
logging.error(e)
def update_window(p, args):
"""Adds new args to a window if they exist"""
content = p['content']
layout = content['layout']
layout.update(args.get('layout', {}))
opts = args.get('opts', {})
for opt_name, opt_val in opts.items():
if opt_name in p:
p[opt_name] = opt_val
if 'legend' in opts:
pdata = p['content']['data']
for i, d in enumerate(pdata):
d['name'] = opts['legend'][i]
return p
def window(args):
""" Build a window dict structure for sending to client """
uid = args.get('win', 'window_' + get_rand_id())
if uid is None:
uid = 'window_' + get_rand_id()
opts = args.get('opts', {})
ptype = args['data'][0]['type']
p = {
'command': 'window',
'id': str(uid),
'title': opts.get('title', ''),
'inflate': opts.get('inflate', True),
'width': opts.get('width'),
'height': opts.get('height'),
'contentID': get_rand_id(), # to detected updated windows
}
if ptype in ['image', 'text', 'properties']:
p.update({'content': args['data'][0]['content'], 'type': ptype})
else:
p['content'] = {'data': args['data'], 'layout': args['layout']}
p['type'] = 'plot'
return p
def broadcast(self, msg, eid):
for s in self.subs:
if self.subs[s].eid == eid:
self.subs[s].write_message(msg)
def register_window(self, p, eid):
# in case env doesn't exist
is_new_env = False
if eid not in self.state:
is_new_env = True
self.state[eid] = {'jsons': {}, 'reload': {}}
env = self.state[eid]['jsons']
if p['id'] in env:
p['i'] = env[p['id']]['i']
else:
p['i'] = len(env)
env[p['id']] = p
broadcast(self, p, eid)
if is_new_env:
broadcast_envs(self)
self.write(p['id'])
def unpack_lua(req_args):
if req_args['is_table']:
if isinstance(req_args['val'], dict):
return {k: unpack_lua(v) for (k, v) in req_args['val'].items()}
else:
return [unpack_lua(v) for v in req_args['val']]
elif req_args['is_tensor']:
return visdom.from_t7(req_args['val'], b64=True)
else:
return req_args['val']
class PostHandler(BaseHandler):
def initialize(self, app):
self.state = app.state
self.subs = app.subs
self.sources = app.sources
self.port = app.port
self.env_path = app.env_path
self.vis = visdom.Visdom(
port=self.port, send=False, use_incoming_socket=False
)
self.handlers = {
'update': UpdateHandler,
'save': SaveHandler,
'close': CloseHandler,
'win_exists': ExistsHandler,
'delete_env': DeleteEnvHandler,
}
def func(self, req):
args, kwargs = req['args'], req.get('kwargs', {})
args = (unpack_lua(a) for a in args)
for k in kwargs:
v = kwargs[k]
kwargs[k] = unpack_lua(v)
func = getattr(self.vis, req['func'])
return func(*args, **kwargs)
def post(self):
req = tornado.escape.json_decode(
tornado.escape.to_basestring(self.request.body)
)
if req.get('func') is not None:
try:
req, endpoint = self.func(req)
if (endpoint != 'events'):
# Process the request using the proper handler
self.handlers[endpoint].wrap_func(self, req)
return
except Exception:
# get traceback and send it back
print(traceback.format_exc())
return self.write(traceback.format_exc())
eid = extract_eid(req)
p = window(req)
register_window(self, p, eid)
class ExistsHandler(BaseHandler):
def initialize(self, app):
self.state = app.state
self.subs = app.subs
self.sources = app.sources
self.port = app.port
self.env_path = app.env_path
@staticmethod
def wrap_func(handler, args):
eid = extract_eid(args)
if args['win'] in handler.state[eid]['jsons']:
handler.write('true')
else:
handler.write('false')
def post(self):
args = tornado.escape.json_decode(
tornado.escape.to_basestring(self.request.body)
)
self.wrap_func(self, args)
class UpdateHandler(BaseHandler):
def initialize(self, app):
self.state = app.state
self.subs = app.subs
self.sources = app.sources
self.port = app.port
self.env_path = app.env_path
@staticmethod
def update(p, args):
# Update text in window, separated by a line break
if p['type'] == 'text':
p['content'] += "<br>" + args['data'][0]['content']
return p
pdata = p['content']['data']
new_data = args.get('data')
p = update_window(p, args)
name = args.get('name')
if name is None and new_data is None:
return p # we only updated the opts or layout
append = args.get('append')
idxs = list(range(len(pdata)))
if name is not None:
assert len(new_data) == 1 or args.get('delete')
idxs = [i for i in idxs if pdata[i]['name'] == name]
# Delete a trace
if args.get('delete'):
for idx in idxs:
del pdata[idx]
return p
# inject new trace
if len(idxs) == 0:
idx = len(pdata)
pdata.append(dict(pdata[0])) # plot is not empty, clone an entry
idxs = [idx]
append = False
pdata[idx] = new_data[0]
for k, v in new_data[0].items():
pdata[idx][k] = v
pdata[idx]['name'] = name
return p
# Update traces
for n, idx in enumerate(idxs):
if all(math.isnan(i) or i is None for i in new_data[n]['x']):
continue
# handle data for plotting
for axis in ['x', 'y']:
pdata[idx][axis] = (pdata[idx][axis] + new_data[n][axis]) \
if append else new_data[n][axis]
# handle marker properties
if 'marker' not in new_data[n]:
continue
if 'marker' not in pdata[idx]:
pdata[idx]['marker'] = {}
pdata_marker = pdata[idx]['marker']
for marker_prop in ['color']:
if marker_prop not in new_data[n]['marker']:
continue
if marker_prop not in pdata[idx]['marker']:
pdata[idx]['marker'][marker_prop] = []
pdata_marker[marker_prop] = (
pdata_marker[marker_prop] +
new_data[n]['marker'][marker_prop]) if append else \
new_data[n]['marker'][marker_prop]
return p
@staticmethod
def wrap_func(handler, args):
eid = extract_eid(args)
if args['win'] not in handler.state[eid]['jsons']:
handler.write('win does not exist')
return
p = handler.state[eid]['jsons'][args['win']]
if not (p['type'] == 'text' or
p['content']['data'][0]['type'] in ['scatter', 'scattergl', 'custom']):
handler.write('win is not scatter, custom, or text; was {}'.format(
p['content']['data'][0]['type']))
return
p = UpdateHandler.update(p, args)
p['contentID'] = get_rand_id()
broadcast(handler, p, eid)
handler.write(p['id'])
def post(self):
args = tornado.escape.json_decode(
tornado.escape.to_basestring(self.request.body)
)
self.wrap_func(self, args)
class CloseHandler(BaseHandler):
def initialize(self, app):
self.state = app.state
self.subs = app.subs
self.sources = app.sources
self.port = app.port
self.env_path = app.env_path
@staticmethod
def wrap_func(handler, args):
eid = extract_eid(args)
win = args.get('win')
keys = \
list(handler.state[eid]['jsons'].keys()) if win is None else [win]
for win in keys:
handler.state[eid]['jsons'].pop(win, None)
broadcast(
handler, json.dumps({'command': 'close', 'data': win}), eid
)
def post(self):
args = tornado.escape.json_decode(
tornado.escape.to_basestring(self.request.body)
)
self.wrap_func(self, args)
class DeleteEnvHandler(BaseHandler):
def initialize(self, app):
self.state = app.state
self.subs = app.subs
self.sources = app.sources
self.port = app.port
self.env_path = app.env_path
@staticmethod
def wrap_func(handler, args):
eid = extract_eid(args)
if eid is not None:
del handler.state[eid]
p = os.path.join(handler.env_path, "{0}.json".format(eid))
os.remove(p)
broadcast_envs(handler)
def post(self):
args = tornado.escape.json_decode(
tornado.escape.to_basestring(self.request.body)
)
self.wrap_func(self, args)
class HashHandler(BaseHandler):
def initialize(self, app):
self.app = app
self.state = app.state
@staticmethod
def wrap_func(handler, args):
eid = extract_eid(args)
handler_json = handler.state[eid]['jsons']
if args['win'] in handler_json:
window_json = handler_json[args['win']]
json_string = json.dumps(
window_json, indent = 2
).encode("utf-8")
hashed = hashlib.md5(json_string).hexdigest()
handler.write(hashed)
else:
handler.write('false')
def post(self):
args = tornado.escape.json_decode(
tornado.escape.to_basestring(self.request.body)
)
self.wrap_func(self, args)
def load_env(state, eid, socket, env_path=DEFAULT_ENV_PATH):
""" load an environment to a client by socket """
env = {}
if eid in state:
env = state.get(eid)
else:
p = os.path.join(env_path, eid.strip(), '.json')
if os.path.exists(p):
env = tornado.escape.json_decode(open(p, 'r').read())
state[eid] = env
if 'reload' in env:
socket.write_message(
json.dumps({'command': 'reload', 'data': env['reload']})
)
jsons = list(env.get('jsons', {}).values())
windows = sorted(jsons, key=lambda k: ('i' not in k, k.get('i', None)))
for v in windows:
socket.write_message(v)
socket.write_message(json.dumps({'command': 'layout'}))
socket.eid = eid
def gather_envs(state, env_path=DEFAULT_ENV_PATH):
items = [i.replace('.json', '') for i in os.listdir(env_path)
if '.json' in i]
return sorted(list(set(items + list(state.keys()))))
def compare_envs(state, eids, socket, env_path=DEFAULT_ENV_PATH):
logging.info('comparing envs')
eidNums = {e: str(i) for i, e in enumerate(eids)}
env = {}
envs = {}
for eid in eids:
if eid in state:
envs[eid] = state.get(eid)
else:
p = os.path.join(env_path, eid.strip(), '.json')
if os.path.exists(p):
env = tornado.escape.json_decode(open(p, 'r').read())
state[eid] = env
envs[eid] = env
res = copy.deepcopy(envs[list(envs.keys())[0]])
name2Wid = {res['jsons'][wid].get('title', None): wid + '_compare'
for wid in res.get('jsons', {})
if 'title' in res['jsons'][wid]}
for wid in list(res['jsons'].keys()):
res['jsons'][wid + '_compare'] = res['jsons'][wid]
res['jsons'][wid] = None
res['jsons'].pop(wid)
for ix, eid in enumerate(envs.keys()):
env = envs[eid]
for wid in env.get('jsons', {}).keys():
win = env['jsons'][wid]
if win.get('type', None) != 'plot':
continue
if 'content' not in win:
continue
if 'title' not in win:
continue
title = win['title']
if title not in name2Wid or title == '':
continue
destWid = name2Wid[title]
destWidJson = res['jsons'][destWid]
# Combine plots with the same window title. If plot data source was
# labeled "name" in the legend, rename to "envId_legend" where
# envId is enumeration of the selected environments (not the long
# environment id string). This makes plot lines more readable.
if ix == 0:
if 'name' not in destWidJson['content']['data'][0]:
continue # Skip windows with unnamed data
destWidJson['has_compare'] = False
destWidJson['content']['layout']['showlegend'] = True
for dataIdx, data in enumerate(destWidJson['content']['data']):
if 'name' not in data:
break # stop working with this plot, not right format
destWidJson['content']['data'][dataIdx]['name'] = \
'{}_{}'.format(eidNums[eid], data['name'])
else:
if 'name' not in destWidJson['content']['data'][0]:
continue # Skip windows with unnamed data
# has_compare will be set to True only if the window title is
# shared by at least 2 envs.
destWidJson['has_compare'] = True
for _dataIdx, data in enumerate(win['content']['data']):
data = copy.deepcopy(data)
if 'name' not in data:
destWidJson['has_compare'] = False
break # stop working with this plot, not right format
data['name'] = '{}_{}'.format(eidNums[eid], data['name'])
destWidJson['content']['data'].append(data)
# Make sure that only plots that are shared by at least two envs are shown.
# Check has_compare flag
for destWid in list(res['jsons'].keys()):
if ('has_compare' not in res['jsons'][destWid]) or \
(not res['jsons'][destWid]['has_compare']):
del res['jsons'][destWid]
# create legend mapping environment names to environment numbers so one can
# look it up for the new legend
tableRows = ["<tr> <td> {} </td> <td> {} </td> </tr>".format(v, eidNums[v])
for v in eidNums]
tbl = """"<style>
table, th, td {{
border: 1px solid black;
}}
</style>
<table> {} </table>""".format(' '.join(tableRows))
res['jsons']['window_compare_legend'] = {
"command": "window",
"id": "window_compare_legend",
"title": "compare_legend",
"inflate": True,
"width": None,
"height": None,
"contentID": "compare_legend",
"content": tbl,
"type": "text",
"layout": {"title": "compare_legend"},
"i": 1
}
if 'reload' in res:
socket.write_message(
json.dumps({'command': 'reload', 'data': res['reload']})
)
jsons = list(res.get('jsons', {}).values())
windows = sorted(jsons, key=lambda k: ('i' not in k, k.get('i', None)))
for v in windows:
socket.write_message(v)
socket.write_message(json.dumps({'command': 'layout'}))
socket.eid = eid
class EnvHandler(BaseHandler):
def initialize(self, app):
self.state = app.state
self.subs = app.subs
self.sources = app.sources
self.port = app.port
self.env_path = app.env_path
def get(self, eid):
items = gather_envs(self.state, env_path=self.env_path)
active = '' if eid not in items else eid
self.render(
'index.html',
user=getpass.getuser(),
items=items,
active_item=active
)
def post(self, args):
msg_args = tornado.escape.json_decode(
tornado.escape.to_basestring(self.request.body)
)
if 'sid' in msg_args:
sid = msg_args['sid']
if sid in self.subs:
load_env(self.state, args, self.subs[sid],
env_path=self.env_path)
if 'eid' in msg_args:
eid = msg_args['eid']
if eid not in self.state:
self.state[eid] = {'jsons': {}, 'reload': {}}
broadcast_envs(self)
class CompareHandler(BaseHandler):
def initialize(self, app):
self.state = app.state
self.subs = app.subs
self.sources = app.sources
self.env_path = app.env_path
def get(self, eids):
items = gather_envs(self.state)
eids = eids.split('+')
# Filter out eids that don't exist
eids = [x for x in eids if x in items]
eids = '+'.join(eids)
self.render(
'index.html',
user=getpass.getuser(),
items=items,
active_item=eids
)
def post(self, args):
sid = tornado.escape.json_decode(
tornado.escape.to_basestring(self.request.body)
)['sid']
if sid in self.subs:
compare_envs(self.state, args.split('+'), self.subs[sid],
self.env_path)
class SaveHandler(BaseHandler):
def initialize(self, app):
self.state = app.state
self.subs = app.subs
self.sources = app.sources
self.port = app.port
self.env_path = app.env_path
@staticmethod
def wrap_func(handler, args):
envs = args['data']
envs = [escape_eid(eid) for eid in envs]
# this drops invalid env ids
ret = serialize_env(handler.state, envs, env_path=handler.env_path)
handler.write(json.dumps(ret))
def post(self):
args = tornado.escape.json_decode(
tornado.escape.to_basestring(self.request.body)
)
self.wrap_func(self, args)
class DataHandler(BaseHandler):
def initialize(self, app):
self.state = app.state
self.port = app.port
self.env_path = app.env_path
@staticmethod
def wrap_func(handler, args):
eid = extract_eid(args)
if 'win' in args and args['win'] is not None:
assert args['win'] in handler.state[eid]['jsons'], \
"Window {} doesn't exist in env {}".format(args['win'], eid)
handler.write(json.dumps(handler.state[eid]['jsons'][args['win']]))
else:
handler.write(json.dumps(handler.state[eid]['jsons']))
def post(self):
args = tornado.escape.json_decode(
tornado.escape.to_basestring(self.request.body)
)
self.wrap_func(self, args)
class IndexHandler(BaseHandler):
def initialize(self, app):
self.state = app.state
self.port = app.port
self.env_path = app.env_path
self.login_enabled = app.login_enabled
self.user_credential = app.user_credential
def get(self, args, **kwargs):
items = gather_envs(self.state, env_path=self.env_path)
if (not self.login_enabled) or self.current_user:
"""self.current_user is an authenticated user provided by Tornado,
available when we set self.get_current_user in BaseHandler,
and the default value of self.current_user is None
"""
self.render(
'index.html',
user=getpass.getuser(),
items=items,
active_item=''
)
elif self.login_enabled:
self.render(
'login.html',
user=getpass.getuser(),
items=items,
active_item=''
)
def post(self, arg, **kwargs):
json_obj = tornado.escape.json_decode(self.request.body)
username = json_obj["username"]
password = hash_password(json_obj["password"])
if ((username == self.user_credential["username"]) and
(password == self.user_credential["password"])):
self.set_secure_cookie("user_password", username + password)
else:
self.set_status(400)
class ErrorHandler(BaseHandler):
def get(self, text):
error_text = text or "test error"
raise Exception(error_text)
# function that downloads and installs javascript, css, and font dependencies: