-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpybw_repl.py
153 lines (122 loc) · 4.56 KB
/
pybw_repl.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
import code
import subprocess
import sys
import threading
from SimpleXMLRPCServer import SimpleXMLRPCServer
import xmlrpclib
import socket
import time
PS1 = 'pybw> '
PS2 = 'pybw| '
class ThreadSharedData():
pass
def xmlrpc_server_thread(shared_data):
try:
def get_buffer():
with shared_data.commands_buffer_lock:
buffer = list( shared_data.commands_buffer )
del shared_data.commands_buffer[:]
return buffer
def write_to_console(text):
sys.stdout.write ('%s' % text)
shared_data.waiting_for_output = 0
return 0
server = SimpleXMLRPCServer(("localhost", 8000), logRequests=False)
shared_data.shutdown = server.shutdown
server.register_introspection_functions()
server.register_function(get_buffer)
server.register_function(write_to_console)
server.serve_forever()
except Exception, e:
print "Thread Error", e
def main():
try:
commands_buffer = []
shared_data = ThreadSharedData() # the thread will populate it later
shared_data.commands_buffer = commands_buffer
shared_data.commands_buffer_lock = threading.Lock()
shared_data.waiting_for_output = 0
t = threading.Thread( target=xmlrpc_server_thread, args=(shared_data,) )
t.start()
try:
i = code.InteractiveConsole()
code_list = []
while True:
while shared_data.waiting_for_output: # remove this for asynchronous REPL
time.sleep(0.01)
try:
input = i.raw_input(PS2 if len(code_list) else PS1 )
except EOFError:
if not code_list:
raise
code_list = []
continue
code_list +=[ input ]
code_str = '\n'.join(code_list)
try:
res = i.compile(code_str)
except SyntaxError, e:
res = None
code_list = []
print e
continue
if res is not None: # this code is complete and without syntax error
if code_str:
with shared_data.commands_buffer_lock:
commands_buffer += [code_str]
shared_data.waiting_for_output = 1
code_list = []
finally:
shared_data.shutdown()
t.join()
except EOFError:
pass
except Exception, e:
print "Error", e
raw_input()
class FakeFile(object):
def __init__(self):
self.buffer = []
def write(self, t):
self.buffer.append( t )
def writelines(self, l):
self.buffer += l
class FakeSys(object):
pass
class ConsoleClient(object):
def _new_process(self):
self.cmd = sys.prefix + '\\python.exe' + ' -c "from pybw_repl import main; main()"'
self.process = subprocess.Popen(self.cmd, close_fds=True )
self.proxy = xmlrpclib.ServerProxy('http://localhost:8000')
def __init__(self):
self._new_process()
def start_repl(self, env_dict):
self.exec_env = code.InteractiveConsole(env_dict)
def kill_server(self):
try:
self.process.kill()
except AttributeError:
pass
def __del__(self):
self.kill_server()
def on_frame(self):
if self.proxy is not None:
try:
commands_buffer = self.proxy.get_buffer()
for command in commands_buffer:
old_stdout = sys.stdout
old_stderr = sys.stderr
sys.stdout = FakeFile()
sys.stderr = FakeFile()
try:
self.exec_env.runsource(command)
except Exception, e:
result = "Exception: %s" % e
finally:
result = ''.join( sys.stdout.buffer ) + ''.join( sys.stderr.buffer )
sys.stdout = old_stdout
sys.stderr = old_stderr
self.proxy.write_to_console( result )
except socket.error:
print "Socket Error"
self.proxy = None