-
Notifications
You must be signed in to change notification settings - Fork 444
/
Copy pathpycutext.py
420 lines (332 loc) · 11.9 KB
/
pycutext.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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
# -*- coding: utf-8 -*-
#
# Copyright (C) 2014 GNS3 Technologies Inc.
#
# 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 3 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/>.
"""
This module implements a QT4 Python interpreter widget.
It is inspired by PyCute : http://gerard.vermeulen.free.fr
"""
import sys
from .qt import QtCore, QtGui, QtWidgets
class MultipleRedirection(QtCore.QObject):
writed = QtCore.pyqtSignal(str)
def __init__(self, console, stdout):
super().__init__()
self.console = console
self.stdout = stdout
def write(self, str):
self.writed.emit(str)
if self.stdout.encoding is None:
str = str.encode("ascii", "ignore").decode("ascii", "ignore")
elif self.stdout.encoding != "UTF-8":
str = str.encode(self.stdout.encoding, "ignore").decode(self.stdout.encoding, "ignore")
self.stdout.write(str)
def isatty(self):
return self.console.isatty() or self.stdout.isatty()
def flush(self):
self.console.flush()
try:
self.stdout.flush()
# On OSX when frozen flush raise a BrokenPipeError
except BrokenPipeError:
pass
class PyCutExt(QtWidgets.QTextEdit):
"""
PyCute is a Python shell for PyQt.
Creating, displaying and controlling PyQt widgets from the Python command
line interpreter is very hard, if not, impossible. PyCute solves this
problem by interfacing the Python interpreter to a PyQt widget.
"""
def __init__(self, interpreter, message="", log="", parent=None):
super().__init__(parent)
self.interpreter = interpreter
self.colorizer = SyntaxColor()
# session log
self.log = log or ''
# to exit the main interpreter by a Ctrl-D if PyCute has no parent
if parent is None:
self.eofKey = QtCore.Qt.Key_D
else:
self.eofKey = None
# capture all interactive input/output
self._old_stdout = sys.stdout
sys.stdout = MultipleRedirection(self, sys.stdout)
sys.stdout.writed.connect(self.write)
# sys.stderr = MultipleRedirection((sys.stderr, self))
self._old_stdin = sys.stdin
sys.stdin = self
# last line + last incomplete lines
self.line = ""
self.lines = []
# the cursor position in the last line
self.point = 0
# flag: the interpreter needs more input to run the last lines.
self.more = 0
# flag: readline() is being used for e.g. raw_input() and input()
self.reading = 0
# history
self.history = []
self.pointer = 0
self.cursor_pos = 0
self.setLineWrapMode(QtWidgets.QTextEdit.NoWrap)
try:
sys.ps1
except AttributeError:
sys.ps1 = ">>> "
try:
sys.ps2
except AttributeError:
sys.ps2 = "... "
self.write(message + '\n\n')
self.write(sys.ps1)
def closeIO(self):
"""
Call when parent is closing we need to stop writing in the windows
and go back to the standard output
"""
sys.stdout = self._old_stdout
sys.stdin = self._old_stdin
def get_interpreter(self):
"""
Return the interpreter object
"""
return self.interpreter
def moveCursor(self, operation, mode=QtGui.QTextCursor.MoveAnchor):
"""
Convenience function to move the cursor
This function will be present in PyQT4.2
"""
cursor = self.textCursor()
cursor.movePosition(operation, mode)
self.setTextCursor(cursor)
def flush(self):
"""
Simulate stdin, stdout, and stderr.
"""
pass
def isatty(self):
"""
Simulate stdin, stdout, and stderr.
"""
return 1
def readline(self):
"""
Simulate stdin, stdout, and stderr.
"""
self.reading = 1
self._clearLine()
self.moveCursor(QtGui.QTextCursor.End)
while self.reading:
QtWidgets.QApplication.processEvents(QtCore.QEventLoop.AllEvents, 1000)
if len(self.line) == 0:
return '\n'
else:
return self.line
def write(self, text, error=False, warning=False):
"""
Simulates stdin, stdout, and stderr.
"""
cursor = self.textCursor()
cursor.movePosition(QtGui.QTextCursor.End)
pos1 = cursor.position()
cursor.insertText(text)
self.cursor_pos = cursor.position()
self.setTextCursor(cursor)
self.ensureCursorVisible()
# Set the format
cursor.setPosition(pos1, QtGui.QTextCursor.KeepAnchor)
char_format = cursor.charFormat()
if error:
color = QtGui.QColor(255, 0, 0) # red
elif warning:
color = QtGui.QColor(255, 128, 0) # orange
else:
color = QtGui.QColor(0, 0, 0) # black
char_format.setForeground(QtGui.QBrush(color))
cursor.setCharFormat(char_format)
def writelines(self, text):
"""
Simulate stdin, stdout, and stderr.
"""
map(self.write, text)
def _run(self):
"""
Append the last line to the history list, let the interpreter execute
the last line(s), and clean up accounting for the interpreter results:
(1) the interpreter succeeds
(2) the interpreter fails, finds no errors and wants more line(s)
(3) the interpreter fails, finds errors and writes them to sys.stderr
"""
self.pointer = 0
self.history.append(self.line)
try:
self.lines.append(self.line)
except Exception as e:
print(e)
source = '\n'.join(self.lines)
self.more = self.interpreter.runsource(source)
if self.more:
self.write(sys.ps2)
else:
self.write(sys.ps1)
self.lines = []
self._clearLine()
def _clearLine(self):
"""
Clear input line buffer
"""
self.line = ""
self.point = 0
def _insertText(self, text):
"""
Inserts text at the current cursor position.
"""
self.line = self.line[:self.point] + text + self.line[self.point:]
self.point += len(text)
cursor = self.textCursor()
cursor.insertText(text)
self.color_line()
def keyPressEvent(self, e):
"""
Handle user input a key at a time.
"""
text = e.text()
key = e.key()
if e.modifiers() == QtCore.Qt.ControlModifier:
return super().keyPressEvent(e)
# Keep the cursor after the last prompt.
self.moveCursor(QtGui.QTextCursor.End)
if key == QtCore.Qt.Key_Backspace:
if self.point:
cursor = self.textCursor()
cursor.movePosition(QtGui.QTextCursor.PreviousCharacter, QtGui.QTextCursor.KeepAnchor)
cursor.removeSelectedText()
self.color_line()
self.point -= 1
self.line = self.line[:-1]
elif key == QtCore.Qt.Key_Delete:
cursor = self.textCursor()
cursor.movePosition(QtGui.QTextCursor.NextCharacter, QtGui.QTextCursor.KeepAnchor)
cursor.removeSelectedText()
self.color_line()
self.line = self.line[:-1]
elif key == QtCore.Qt.Key_Return or key == QtCore.Qt.Key_Enter:
self.write("\n")
if self.reading:
self.reading = 0
else:
self._run()
elif key == QtCore.Qt.Key_Tab:
self.onKeyPress_Tab()
elif key == QtCore.Qt.Key_Left:
if self.point:
self.moveCursor(QtGui.QTextCursor.Left)
self.point -= 1
elif key == QtCore.Qt.Key_Right:
if self.point < len(self.line):
self.moveCursor(QtGui.QTextCursor.Right)
self.point += 1
elif key == QtCore.Qt.Key_Home:
cursor = self.textCursor()
cursor.setPosition(self.cursor_pos)
self.setTextCursor(cursor)
self.point = 0
elif key == QtCore.Qt.Key_End:
self.moveCursor(QtGui.QTextCursor.EndOfLine)
self.point = len(self.line)
elif key == QtCore.Qt.Key_Up:
if len(self.history):
if self.pointer == 0:
self.pointer = len(self.history)
self.pointer -= 1
self._recall()
elif key == QtCore.Qt.Key_Down:
if len(self.history):
self.pointer += 1
if self.pointer == len(self.history):
self.pointer = 0
self._recall()
elif len(text):
self._insertText(text)
return
def _recall(self):
"""
Display the current item from the command history.
"""
cursor = self.textCursor()
cursor.select(QtGui.QTextCursor.LineUnderCursor)
cursor.removeSelectedText()
if self.more:
self.write(sys.ps2)
else:
self.write(sys.ps1)
self._clearLine()
self._insertText(self.history[self.pointer])
def contentsContextMenuEvent(self, ev):
"""
Suppress the right button context menu.
"""
return
def color_line(self):
"""
Color the current line.
"""
cursor = self.textCursor()
cursor.movePosition(QtGui.QTextCursor.StartOfLine)
newpos = cursor.position()
pos = -1
while (newpos != pos):
cursor.movePosition(QtGui.QTextCursor.NextWord)
pos = newpos
newpos = cursor.position()
cursor.select(QtGui.QTextCursor.WordUnderCursor)
word = cursor.selectedText()
if not word:
continue
(R, G, B) = self.colorizer.get_color(word)
char_format = cursor.charFormat()
char_format.setForeground(QtGui.QBrush(QtGui.QColor(R, G, B)))
cursor.setCharFormat(char_format)
class SyntaxColor:
"""
Allows to color python keywords.
"""
keywords = set(["and", "del", "from", "not", "while",
"as", "elif", "global", "or", "with",
"assert", "else", "if", "pass", "yield",
"break", "except", "import", "print",
"class", "exec", "in", "raise",
"continue", "finally", "is", "return",
"def", "for", "lambda", "try"])
def get_color(self, word):
""" Return a color tuple (R,G,B) depending of the string word """
stripped = word.strip()
if(stripped in self.keywords):
return (165, 42, 42) # brown
elif(self.is_python_string(stripped)):
return (61, 120, 9) # dark green
else:
return (0, 0, 0)
def is_python_string(self, string):
"""
Return True if string is enclosed by a string mark
"""
# return (
# (string.startswith("'''") and string.endswith("'''")) or
# (string.startswith('"""') and string.endswith('"""')) or
# (string.startswith("'") and string.endswith("'")) or
# (string.startswith('"') and string.endswith('"'))
# )
return False