This repository was archived by the owner on Oct 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 90
/
Copy pathPythonConsole.py
390 lines (324 loc) · 11.6 KB
/
PythonConsole.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
# Copyright (c) 2011-2015 Rusty Wagner
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from PySide.QtCore import *
from PySide.QtGui import *
from Fonts import *
import code
import sys
import threading
import Threads
class PythonConsoleOutput():
def __init__(self, orig, error):
self.stdout = orig
self.error = error
def write(self, data):
console = None
if "value" in dir(_python_console):
console = _python_console.value
if console is None:
self.stdout.write(data)
else:
if self.error:
Threads.run_on_gui_thread(lambda: console.write_stderr(data))
else:
Threads.run_on_gui_thread(lambda: console.write_stdout(data))
class PythonConsoleInput():
def __init__(self, orig):
self.stdin = orig
def read(self, size):
console = None
if "value" in dir(_python_console):
console = _python_console.value
if console is None:
return self.stdin.read(size)
else:
return console.read_stdin(size)
def readline(self):
console = None
if "value" in dir(_python_console):
console = _python_console.value
if console is None:
return self.stdin.readline()
else:
return console.readline_stdin()
class PythonConsoleThread(threading.Thread):
def __init__(self, console):
threading.Thread.__init__(self)
self.console = console
self.globals = {"__name__":"__console__", "__doc__":None}
self.code = None
self.event = threading.Event()
self.done = threading.Event()
self.exit = False
self.interpreter = code.InteractiveInterpreter(self.globals)
# There is no way to interrupt a thread that isn't the main thread, so
# to avoid not being able to close the app, create the thread as
# as daemon thread.
self.daemon = True
# Set up environment with useful variables and functions
self.globals["data"] = Threads.GuiObjectProxy(self.console.view.data)
self.globals["exe"] = Threads.GuiObjectProxy(self.console.view.exe)
self.globals["view"] = Threads.GuiObjectProxy(self.console.view)
self.globals["current_view"] = Threads.GuiObjectProxy(lambda: self.console.view.view)
self.globals["change_view"] = Threads.GuiObjectProxy(lambda type: self.console.view.setViewType(type))
self.globals["navigate"] = Threads.GuiObjectProxy(lambda type, pos: self.console.view.navigate(type, pos))
self.globals["create_file"] = Threads.GuiObjectProxy(lambda data: Threads.create_file(data))
self.globals["cursor"] = Threads.GuiObjectProxy(lambda: self.console.view.view.get_cursor_pos())
self.globals["set_cursor"] = Threads.GuiObjectProxy(lambda pos: self.console.view.view.set_cursor_pos(pos))
self.globals["selection_range"] = Threads.GuiObjectProxy(lambda: self.console.view.view.get_selection_range())
self.globals["set_selection_range"] = Threads.GuiObjectProxy(lambda start, end: self.console.view.view.set_selection_range(start, end))
self.globals["selection"] = Threads.GuiObjectProxy(lambda: self.get_selection())
self.globals["replace_selection"] = Threads.GuiObjectProxy(lambda value: self.replace_selection(value))
self.globals["write_at_cursor"] = Threads.GuiObjectProxy(lambda value: self.write_at_cursor(value))
self.globals["undo"] = Threads.GuiObjectProxy(lambda: self.console.view.undo())
self.globals["redo"] = Threads.GuiObjectProxy(lambda: self.console.view.redo())
self.globals["commit"] = Threads.GuiObjectProxy(lambda: self.console.view.commit_undo())
self.globals["copy"] = Threads.GuiObjectProxy(lambda value: self.copy(value))
self.globals["paste"] = Threads.GuiObjectProxy(lambda: self.console.view.view.paste())
self.globals["clipboard"] = Threads.GuiObjectProxy(lambda: self.get_clipboard())
# Helper APIs
def get_selection(self):
data = self.console.view.view.data
range = self.console.view.view.get_selection_range()
return data.read(range[0], range[1] - range[0])
def replace_selection(self, value):
data = self.console.view.view.data
range = self.console.view.view.get_selection_range()
if (range[1] - range[0]) == len(value):
result = data.write(range[0], value)
else:
data.remove(range[0], range[1] - range[0])
result = data.insert(range[0], value)
self.console.view.view.set_cursor_pos(range[0] + result)
return result
def write_at_cursor(self, value):
data = self.console.view.view.data
pos = self.console.view.view.get_cursor_pos()
result = data.write(pos, value)
self.console.view.view.set_cursor_pos(pos + result)
return result
def copy(self, data):
if type(data) != str:
data = str(data)
clipboard = QApplication.clipboard()
clipboard.clear()
mime = QMimeData()
mime.setText(data.encode("string_escape").replace("\"", "\\\""))
mime.setData("application/octet-stream", QByteArray(data))
clipboard.setMimeData(mime)
def get_clipboard(self):
clipboard = QApplication.clipboard()
mime = clipboard.mimeData()
if mime.hasFormat("application/octet-stream"):
return mime.data("application/octet-stream").data()
elif mime.hasText():
return mime.text().encode("utf8")
else:
return None
# Thread run loop
def run(self):
_python_console.value = self.console
while not self.exit:
self.event.wait()
self.event.clear()
if self.exit:
break
if self.code:
self.interpreter.runsource(self.code)
self.code = None
self.done.set()
class PythonConsoleLineEdit(QLineEdit):
prevHistory = Signal(())
nextHistory = Signal(())
def __init__(self, *args):
super(PythonConsoleLineEdit, self).__init__(*args)
def event(self, event):
if (event.type() == QEvent.KeyPress) and (event.key() == Qt.Key_Tab):
self.insert("\t")
return True
if (event.type() == QEvent.KeyPress) and (event.key() == Qt.Key_Up):
self.prevHistory.emit()
return True
if (event.type() == QEvent.KeyPress) and (event.key() == Qt.Key_Down):
self.nextHistory.emit()
return True
return QLineEdit.event(self, event)
class PythonConsole(QWidget):
def __init__(self, view):
super(PythonConsole, self).__init__(view)
self.view = view
font = getMonospaceFont()
layout = QVBoxLayout()
layout.setContentsMargins(0, 0, 0, 0)
layout.setSpacing(0)
self.output = QTextEdit()
self.output.setFont(font)
self.output.setReadOnly(True)
layout.addWidget(self.output, 1)
input_layout = QHBoxLayout()
input_layout.setContentsMargins(4, 4, 4, 4)
input_layout.setSpacing(4)
self.prompt = QLabel(">>>")
self.prompt.setFont(font)
input_layout.addWidget(self.prompt)
self.input = PythonConsoleLineEdit()
self.input.setFont(font)
self.input.returnPressed.connect(self.process_input)
self.input.prevHistory.connect(self.prev_history)
self.input.nextHistory.connect(self.next_history)
input_layout.addWidget(self.input)
layout.addLayout(input_layout, 0)
self.setLayout(layout)
self.setFocusPolicy(Qt.NoFocus)
self.setMinimumSize(100, 100)
size_policy = self.sizePolicy()
size_policy.setVerticalStretch(1)
self.setSizePolicy(size_policy)
self.thread = PythonConsoleThread(self)
self.thread.start()
self.completion_timer = QTimer()
self.completion_timer.setInterval(100)
self.completion_timer.setSingleShot(False)
self.completion_timer.timeout.connect(self.completion_timer_event)
self.completion_timer.start()
self.source = None
self.running = False
self.input_requested = False
self.input_result = ""
self.input_event = threading.Event()
self.input_history = []
self.input_history_pos = None
def stop(self):
self.thread.exit = True
self.thread.event.set()
# Can't join here, as it might be stuck in user code
def process_input(self):
self.input_history += [str(self.input.text())]
self.input_history_pos = None
input = str(self.input.text()) + "\n"
self.input.setText("")
self.output.textCursor().movePosition(QTextCursor.End)
fmt = QTextCharFormat()
fmt.setForeground(QBrush(Qt.black))
if len(self.prompt.text()) > 0:
self.output.textCursor().insertText(self.prompt.text() + " " + input, fmt)
else:
self.output.textCursor().insertText(input, fmt)
self.output.ensureCursorVisible()
if self.input_requested:
# Request for data from stdin
self.input_requested = False
self.input.setEnabled(False)
self.input_result = input
self.input_event.set()
return
if self.source is not None:
self.source = self.source + input
if input != "\n":
# Don't end multiline input until a blank line
return
input = self.source
try:
result = code.compile_command(input)
except:
result = False
if result is None:
if self.source is None:
self.source = input
else:
self.source += input
self.prompt.setText("...")
return
self.source = None
self.prompt.setText(">>>")
self.thread.code = input
self.thread.event.set()
self.running = True
self.thread.done.wait(0.05)
if self.thread.done.is_set():
self.thread.done.clear()
self.running = False
else:
self.input.setEnabled(False)
def prev_history(self):
if len(self.input_history) == 0:
return
if self.input_history_pos is None:
self.input_history_pos = len(self.input_history)
if self.input_history_pos == 0:
return
self.input_history_pos -= 1
self.input.setText(self.input_history[self.input_history_pos])
def next_history(self):
if self.input_history_pos is None:
return
if (self.input_history_pos + 1) >= len(self.input_history):
self.input_history_pos = None
self.input.setText("")
return
self.input_history_pos += 1
self.input.setText(self.input_history[self.input_history_pos])
def completion_timer_event(self):
if self.thread.done.is_set():
self.thread.done.clear()
self.running = False
self.input.setEnabled(True)
self.input.setFocus(Qt.OtherFocusReason)
self.prompt.setText(">>>")
def request_input(self):
self.input_requested = True
self.input.setEnabled(True)
self.input.setFocus(Qt.OtherFocusReason)
self.prompt.setText("")
def write_stdout(self, data):
self.output.textCursor().movePosition(QTextCursor.End)
fmt = QTextCharFormat()
fmt.setForeground(QBrush(Qt.blue))
self.output.textCursor().insertText(data, fmt)
self.output.ensureCursorVisible()
def write_stderr(self, data):
self.output.textCursor().movePosition(QTextCursor.End)
fmt = QTextCharFormat()
fmt.setForeground(QBrush(Qt.red))
self.output.textCursor().insertText(data, fmt)
self.output.ensureCursorVisible()
def read_stdin(self, size):
if Threads.is_gui_thread():
raise RuntimeError, "Cannot call read_stdin from GUI thread"
if len(self.input_result) == 0:
Threads.run_on_gui_thread(self.request_input)
self.input_event.wait()
self.input_event.clear()
if len(self.input_result) > size:
result = self.input_result[0:size]
self.input_result = self.input_result[size:]
return result
result = self.input_result
self.input_result = ""
return
def readline_stdin(self):
if Threads.is_gui_thread():
raise RuntimeError, "Cannot call readline_stdin from GUI thread"
if len(self.input_result) == 0:
Threads.run_on_gui_thread(self.request_input)
self.input_event.wait()
self.input_event.clear()
result = self.input_result
self.input_result = ""
return result
sys.stderr = PythonConsoleOutput(sys.stderr, True)
sys.stdout = PythonConsoleOutput(sys.stdout, False)
sys.stdin = PythonConsoleInput(sys.stdin)
_python_console = threading.local()