forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathpython_server.py
192 lines (147 loc) · 5.45 KB
/
python_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
import ast
import contextlib
import io
import json
import sys
import traceback
import uuid
from typing import Dict, List, Optional, Union
STDIN = sys.stdin
STDOUT = sys.stdout
STDERR = sys.stderr
USER_GLOBALS = {}
def _send_message(msg: str):
# Content-Length is the data size in bytes.
length_msg = len(msg.encode())
STDOUT.buffer.write(f"Content-Length: {length_msg}\r\n\r\n{msg}".encode())
STDOUT.buffer.flush()
def send_message(**kwargs):
_send_message(json.dumps({"jsonrpc": "2.0", **kwargs}))
def print_log(msg: str):
send_message(method="log", params=msg)
def send_response(
response: str,
response_id: int,
execution_status: bool = True, # noqa: FBT001, FBT002
):
send_message(
id=response_id,
result={"status": execution_status, "output": response},
)
def send_request(params: Optional[Union[List, Dict]] = None):
request_id = uuid.uuid4().hex
if params is None:
send_message(id=request_id, method="input")
else:
send_message(id=request_id, method="input", params=params)
return request_id
original_input = input
def custom_input(prompt=""):
try:
send_request({"prompt": prompt})
headers = get_headers()
# Content-Length is the data size in bytes.
content_length = int(headers.get("Content-Length", 0))
if content_length:
message_text = STDIN.buffer.read(content_length).decode()
message_json = json.loads(message_text)
return message_json["result"]["userInput"]
except Exception:
print_log(traceback.format_exc())
# Set input to our custom input
USER_GLOBALS["input"] = custom_input
input = custom_input # noqa: A001
def handle_response(request_id):
while not STDIN.closed:
try:
headers = get_headers()
# Content-Length is the data size in bytes.
content_length = int(headers.get("Content-Length", 0))
if content_length:
message_text = STDIN.buffer.read(content_length).decode()
message_json = json.loads(message_text)
our_user_input = message_json["result"]["userInput"]
if message_json["id"] == request_id:
send_response(our_user_input, message_json["id"])
elif message_json["method"] == "exit":
sys.exit(0)
except Exception: # noqa: PERF203
print_log(traceback.format_exc())
def exec_function(user_input):
try:
compile(user_input, "<stdin>", "eval")
except SyntaxError:
return exec
return eval
def check_valid_command(request):
try:
user_input = request["params"]
ast.parse(user_input[0])
send_response("True", request["id"])
except SyntaxError:
send_response("False", request["id"])
def execute(request, user_globals):
str_output = CustomIO("<stdout>", encoding="utf-8")
str_error = CustomIO("<stderr>", encoding="utf-8")
str_input = CustomIO("<stdin>", encoding="utf-8", newline="\n")
with contextlib.redirect_stdout(str_output), contextlib.redirect_stderr(str_error):
original_stdin = sys.stdin
try:
sys.stdin = str_input
execution_status = exec_user_input(request["params"], user_globals)
finally:
sys.stdin = original_stdin
send_response(str_output.get_value(), request["id"], execution_status)
def exec_user_input(user_input, user_globals) -> bool:
user_input = user_input[0] if isinstance(user_input, list) else user_input
try:
callable_ = exec_function(user_input)
retval = callable_(user_input, user_globals)
if retval is not None:
print(retval)
return True
except KeyboardInterrupt:
print(traceback.format_exc())
return False
except Exception:
print(traceback.format_exc())
return False
class CustomIO(io.TextIOWrapper):
"""Custom stream object to replace stdio."""
def __init__(self, name, encoding="utf-8", newline=None):
self._buffer = io.BytesIO()
self._custom_name = name
super().__init__(self._buffer, encoding=encoding, newline=newline)
def close(self):
"""Provide this close method which is used by some tools."""
# This is intentionally empty.
def get_value(self) -> str:
"""Returns value from the buffer as string."""
self.seek(0)
return self.read()
def get_headers():
headers = {}
while True:
line = STDIN.buffer.readline().decode().strip()
if not line:
break
name, value = line.split(":", 1)
headers[name] = value.strip()
return headers
if __name__ == "__main__":
while not STDIN.closed:
try:
headers = get_headers()
# Content-Length is the data size in bytes.
content_length = int(headers.get("Content-Length", 0))
if content_length:
request_text = STDIN.buffer.read(content_length).decode()
request_json = json.loads(request_text)
if request_json["method"] == "execute":
execute(request_json, USER_GLOBALS)
if request_json["method"] == "check_valid_command":
check_valid_command(request_json)
elif request_json["method"] == "exit":
sys.exit(0)
except Exception: # noqa: PERF203
print_log(traceback.format_exc())