-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathjs_reimpl_common.py
More file actions
106 lines (79 loc) · 2.35 KB
/
Copy pathjs_reimpl_common.py
File metadata and controls
106 lines (79 loc) · 2.35 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
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
import socket
import json
from common import DUMMY, EMPTY
none_info = {
"type": "None",
"hash": str(hash(None))
}
def dump_simple_py_obj(obj):
if obj is DUMMY:
return {
"type": "DUMMY"
}
elif obj is EMPTY:
return None
elif obj is None:
return none_info
elif isinstance(obj, int):
return {
'type': 'int',
'value': str(obj)
}
return obj
def dump_pairs(pairs):
res = []
for k, v in pairs:
res.append([dump_simple_py_obj(k), dump_simple_py_obj(v)])
return res
def dump_array(array):
return list(map(dump_simple_py_obj, array))
def parse_array(array):
return list(map(parse_simple_py_obj, array))
def parse_simple_py_obj(obj):
if isinstance(obj, dict):
assert obj["type"] in ["DUMMY", "None", "int"]
if obj["type"] == "DUMMY":
return DUMMY
if obj["type"] == "None":
return None
return int(obj["value"])
elif obj is None:
return EMPTY
return obj
sock = None
sockfile = None
def _init_sock_stuff():
global sock
global sockfile
# TODO: unhardcode?
SOCK_FILENAME = 'pynode.sock'
if sock is None:
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
sock.connect(SOCK_FILENAME)
sockfile = sock.makefile('r')
return sock, sockfile
def run_op_chapter1_chapter2(chapter, hash_codes, keys, op, **kwargs):
_init_sock_stuff()
for name in kwargs:
if name != 'array':
kwargs[name] = dump_simple_py_obj(kwargs[name])
else:
kwargs[name] = dump_array(kwargs[name])
data = {
"dict": chapter,
"op": op,
"args": kwargs,
"hashCodes": dump_array(hash_codes) if hash_codes is not None else None,
"keys": dump_array(keys) if keys is not None else None,
}
sock.send(bytes(json.dumps(data) + "\n", 'UTF-8'))
response = json.loads(sockfile.readline())
if "exception" in response and response["exception"]:
raise KeyError()
if 'result' in response and response['result'] is not None:
# TODO: this is pretty hacky
return response["result"]
elif "hashCodes" in response:
return parse_array(response["hashCodes"]), parse_array(response["keys"])
else:
return parse_array(response["keys"])