forked from Submersible/node-python-bridge
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode_python_bridge.py
More file actions
72 lines (60 loc) · 2.04 KB
/
node_python_bridge.py
File metadata and controls
72 lines (60 loc) · 2.04 KB
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
from __future__ import unicode_literals
import six
import os
import sys
import json
import traceback
NODE_CHANNEL_FD = int(os.environ['NODE_CHANNEL_FD'])
def format_exception(t=None, e=None, tb=None):
return dict(
exception=dict(
type=dict(
name=t.__name__,
module=t.__module__
) if t else None,
message=str(e),
args=getattr(e, 'args', None),
format=traceback.format_exception_only(t, e)
) if e else None,
traceback=dict(
lineno=traceback.tb_lineno(tb) if hasattr(traceback, 'tb_lineno') else tb.tb_lineno,
strack=traceback.format_stack(tb.tb_frame),
format=traceback.format_tb(tb)
) if tb else None,
format=traceback.format_exception(t, e, tb)
)
if __name__ == '__main__':
writer = os.fdopen(NODE_CHANNEL_FD, 'w')
reader = os.fdopen(NODE_CHANNEL_FD, 'r')
while True:
try:
# Read new command
line = reader.readline()
if not line:
break
data = json.loads(line)
# Assert data saneness
if data['type'] not in ['execute', 'evaluate']:
raise Exception('Python bridge call `type` must be `execute` or `evaluate`')
if not isinstance(data['code'], six.string_types):
raise Exception('Python bridge call `code` must be a string.')
# Run Python code
if data['type'] == 'execute':
exec(data['code'])
response = dict(type='success')
else:
response = dict(type='success', value=eval(data['code']))
except:
t, e, tb = sys.exc_info()
response = dict(type='exception', value=format_exception(t, e, tb))
writer.write(json.dumps(response, separators=(',', ':')) + '\n')
writer.flush()
# Closing is messy
try:
reader.close()
except IOError:
pass
try:
writer.close()
except IOError:
pass