-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPyInterpreter.py
More file actions
392 lines (338 loc) · 13.3 KB
/
Copy pathPyInterpreter.py
File metadata and controls
392 lines (338 loc) · 13.3 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
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
import Utility as Util
from Utility import *
from kivy.uix.carousel import Carousel
from glob import glob
from Tutorial import TutorialLayout
from Editor import EditorLayout
from FileBrowser import FileBrowser
from Constants import *
#---------------------#
# CLASS : REPL
#---------------------#
class REPL(Singleton):
name = "Python REPL"
result = ""
code = []
lastCode = ""
myGlobals = {}
isIndentMode = False
ui = None
def __init__(self, ui):
self.ui = ui
def onConsoleInput(self, inputText):
result = 0
try:
result = self.run_script(inputText)
except:
log("error")
# insert result
if result:
self.ui.displayText(result, 0)
if self.lastCode:
temp = self.lastCode
self.lastCode = None
self.onConsoleInput(temp)
def run_script(self, code):
# run script
stripCode = code.strip()
if self.code and (stripCode == "" or code[0] != " " and code[0] != "\t"):
self.lastCode = code
code = "\n".join(self.code)
self.isIndentMode = False
self.code = []
# inner indent mode
elif self.code or stripCode and stripCode[-1] == ":":
self.code.append(code)
self.isIndentMode = True
return None
try:
self.old_stdout = sys.stdout
self.redirected_output = sys.stdout = StringIO()
try:
print eval(code, self.myGlobals)
except:
exec(code, self.myGlobals)
sys.stdout = self.old_stdout
self.result = self.redirected_output.getvalue()
except Exception, e:
self.errorstring = traceback.format_exc()
self.result = ("ERROR: " + self.errorstring)
return self.result[:-1]
#---------------------#
# CLASS : PyInterpreter
#---------------------#
class PyInterpreter(Singleton):
isInit = False
bExitOnTouchPrev = True
outputWidth = W
prevMode = ""
currentMode = "" # console, tutorial, editor
emptyWidget = Widget(size_hint=(None,None), size=(0,0))
def init(self):
if self.isInit:
return
# add screens
self.screen = Screen(name=szConsole)
self.editorLayout = EditorLayout(self)
self.tutorialLayout = TutorialLayout(self)
self.fileBrowser = FileBrowser(self)
self.repl = REPL(self)
self.history = ["",]
self.historyIndex = -1
self.historyCount = 100
self.lastIndentSpace = ""
self.bIndentMode = False
self.reFocusInputText = False
self.oldTouchPrev = None
self.textInputWidth = W * (4.0/5.0)
# console menu layout
self.consoleMenuLayout = BoxLayout(orientation="horizontal", size_hint=(1, None), height="35dp")
btn_clear = Button(text="Clear", background_color=darkGray)
btn_clear.bind(on_release = lambda inst:self.clearOutput())
btn_prev = Button(text="<<", background_color=darkGray)
btn_next = Button(text=">>", background_color=darkGray)
self.consoleMenuLayout.add_widget(btn_clear)
self.consoleMenuLayout.add_widget(btn_prev)
self.consoleMenuLayout.add_widget(btn_next)
self.screen.add_widget(self.consoleMenuLayout)
# screen menu layout
self.screenMenuLayout = BoxLayout(orientation="horizontal", size_hint=(1, None), height="35dp", pos=(0,0))
btn_console = Button(text="Console", background_color=[1.5,0.8,0.8,2])
btn_editor = Button(text="Code Editor", background_color=[0.8,1.5,0.8,2])
btn_tutorial = Button(text="Python Tutotial", background_color=[0.8,0.8,1.5,2])
btn_editor.bind(on_release=lambda inst:self.setMode(szEditor))
btn_tutorial.bind(on_release=lambda inst:self.setMode(szTutorial))
self.screenMenuLayout.add_widget(btn_console)
self.screenMenuLayout.add_widget(btn_editor)
self.screenMenuLayout.add_widget(btn_tutorial)
self.screen.add_widget(self.screenMenuLayout)
# text input
self.consoleInput = TextInput(text = "text", multiline=False, size_hint=(None, None), auto_indent = True, font_name=defaultFont,
background_color=(.1, .1, .1, 1), foreground_color=(1,1,1,1), text_size=(0,0), font_size="14dp", padding_x="20dp", padding_y="15dp")
self.consoleInput.size = (W, self.consoleInput.minimum_height)
self.consoleInput.text = ""
'''
def paste():
self.consoleInput.insert_text(gMyRoot.getClipboard())
self.refreshLayout()
self.consoleInput.paste = paste
'''
self.consoleInput.bind(on_text_validate = self.onConsoleInput)
self.consoleInput.bind(focus = self.inputBoxFocus)
# textinput scroll view
self.textInputSV = ScrollView(size_hint=(None, None), size = (W, self.consoleInput.minimum_height))
self.textInputSV.scroll_y = 0
self.textInputSV.add_widget(self.consoleInput)
# add input widget
self.screen.add_widget(self.textInputSV)
# run button
self.btn_run = Button(text="Run", size_hint=(None,None), size =(W-self.consoleInput.size[0], self.consoleInput.size[1]),\
background_color=(1.3,1.3,2,2))
self.btn_run.bind(on_release = lambda inst:self.onConsoleInput(self.consoleInput, True))
self.btn_run.pos = (W - self.btn_run.size[0], 0)
self.screen.add_widget(self.btn_run)
# output
self.outputSV = ScrollView(size_hint=(None, None))
self.screen.add_widget(self.outputSV)
self.outputLayout = BoxLayout(orientation="vertical", size_hint=(None,None))
self.outputSV.add_widget(self.outputLayout)
def func_prev(inst):
if len(self.history) > 0:
self.historyIndex -= 1
if self.historyIndex < 0:
self.historyIndex = len(self.history) - 1
text = self.history[self.historyIndex]
if text.find("\n") > -1:
self.bIndentMode = True
self.setInputText(text)
btn_prev.bind(on_release = func_prev)
def func_next(inst):
if len(self.history) > 0:
self.historyIndex += 1
if self.historyIndex >= len(self.history):
self.historyIndex = 0
text = self.history[self.historyIndex]
if text.find("\n") > -1:
self.bIndentMode = True
self.setInputText(text)
btn_next.bind(on_release = func_next)
# show output layout
self.displayText("Python " + sys.version.strip(), 0)
self.isInit = True
def setMode(self, mode):
if mode == self.currentMode:
return
self.prevMode = self.currentMode
self.currentMode = mode
# restore..
if self.prevMode == szConsole:
self.reFocusInputText = False
self.inputBoxForceFocus(False)
# set mode layout
if mode == szConsole:
gMyRoot.current_screen(self.screen)
self.refreshLayout()
elif mode == szEditor:
gMyRoot.current_screen(self.editorLayout.screen)
elif mode == szTutorial:
gMyRoot.current_screen(self.tutorialLayout.screen)
elif mode == szFileBrowserOpen:
self.fileBrowser.showOpenLayout()
elif mode == szFileBrowserSaveAs:
self.fileBrowser.showSaveAsLayout()
def setExitOnTouchPrev(self, bValue):
self.bExitOnTouchPrev = bValue
def start(self):
self.show()
def update(self, dt):
pass
def clearOutput(self):
self.outputLayout.clear_widgets()
self.outputLayout.size = (W, 0)
def outputLayout_add_widget(self, widget):
self.outputLayout.add_widget(widget)
self.outputLayout.height += widget.height + widget.padding[1] + widget.padding[3]
def displayText(self, text, scroll_y, background_color=(1,1,1,0)):
if type(text) != str:
text = str(text)
output = TextInput(markup = True, text="", halign='left', valign='top', readonly=True, font_size="12dp", font_name = defaultFont,
multiline=True, background_color=background_color, foreground_color=(1,1,1,1), size_hint=(None,None), size = (self.outputWidth, 0))
output.text = text
output.size = (self.outputWidth, output.minimum_height)
self.outputLayout.add_widget(output)
self.outputLayout.size = (self.outputWidth, self.outputLayout.size[1] + output.size[1])
self.outputSV.scroll_x = 0
self.outputSV.scroll_y = scroll_y
def onConsoleInput(self, inst, bForceRun = False):
self.reFocusInputText = True
if inst.text.strip():
bRunCode = len(inst.text) == self.consoleInput.cursor_index()
lastLine_nonStrip = inst.text.split("\n")[-1]
lastLine = lastLine_nonStrip.strip()
# indent mode - continue input but not run
if not bForceRun and lastLine and (lastLine[-1] in ("\\", ":") or self.bIndentMode or not bRunCode):
self.bIndentMode = True
# get indent space
self.lastIndentSpace = ""
if self.bIndentMode:
for i in lastLine_nonStrip:
if i in [" ", "\t"]:
self.lastIndentSpace += i
else:
break
inst.insert_text("\n" + self.lastIndentSpace)
self.consoleInput.size = (W, self.consoleInput.minimum_height)
return
# check indent mode - run code
self.bIndentMode = False
# pop input text from history
if len(self.history) > 0 and self.historyIndex > -1 \
and self.historyIndex < len(self.history) and self.history[self.historyIndex] == inst.text:
self.history.pop(self.historyIndex)
# append item to history
if self.currentMode == szConsole:
self.history.append(inst.text.strip())
# check history count
if len(self.history) > self.historyCount:
self.history = self.history[:self.historyCount]
self.historyIndex = len(self.history)
# display input text to output widget
if self.currentMode == szConsole:
lines = inst.text.split("\n")
result = []
for i, line in enumerate(lines):
line = (">>> " if i == 0 else "... ") + line
result.append(line)
result = "\n".join(result)
self.displayText(result, 0)
elif self.currentMode == szEditor:
currentDocument = ">>> Run Untitled.py"
self.displayText("\n".join(["-" * len(currentDocument), currentDocument + " - " + time.ctime()]), 0)
# run code
self.repl.onConsoleInput(inst.text)
# end message
if self.currentMode == szEditor:
self.displayText("Done.", 0)
# clear text input
if self.currentMode == szConsole:
self.setInputText("")
def refreshLayout(self):
if self.currentMode == szConsole:
keyboardHeight = gMyRoot.getKeyboardHeight() if self.consoleInput.focus else 0
limitHeight = (H - topMargin - keyboardHeight) * 0.5
if self.consoleInput.minimum_height > limitHeight:
height = limitHeight
else:
height = self.consoleInput.minimum_height
self.screenMenuLayout.pos = (0, keyboardHeight)
self.textInputSV.pos = (0, self.screenMenuLayout.top)
self.textInputSV.size = (self.textInputWidth, height)
self.consoleInput.size = (self.textInputWidth, self.consoleInput.minimum_height)
self.btn_run.pos = (self.textInputSV.size[0], self.textInputSV.pos[1])
self.btn_run.size = (W - self.textInputSV.size[0], self.textInputSV.size[1])
self.consoleMenuLayout.pos = (0, self.textInputSV.top)
self.outputSV.pos = (0, self.consoleMenuLayout.top)
self.outputSV.size = (W, H - self.outputSV.pos[1] - topMargin)
def setInputText(self, text):
self.consoleInput.text = text
if self.consoleInput.size[1] != self.consoleInput.minimum_height:
self.refreshLayout()
def insertInputText(self, text):
self.consoleInput.insert_text(text)
if self.consoleInput.size[1] != self.consoleInput.minimum_height:
self.refreshLayout()
def inputBoxForceFocus(self, bFocus):
if bFocus != self.consoleInput.focus:
self.consoleInput.focus = bFocus
def inputBoxFocus(self, inst, bFocus):
bAlwaysPreserveFocus = True
if not bFocus:
if self.reFocusInputText:
self.reFocusInputText = bAlwaysPreserveFocus
inst.focus = True
self.reFocusInputText = bAlwaysPreserveFocus
self.refreshLayout()
def update(self, dt):
pass
def touchPrev(self):
if self.currentMode == szEditor:
self.editorLayout.touchPrev()
elif self.currentMode in (szFileBrowserOpen, szFileBrowserSaveAs):
self.fileBrowser.touchPrev()
elif self.currentMode == szTutorial:
self.tutorialLayout.touchPrev()
elif self.consoleInput.focus:
self.reFocusInputText = False
self.inputBoxForceFocus(False)
else:
self.exit()
def toggle(self):
if not self.isInit:
self.init()
if self.screen.name == gMyRoot.get_current_screen():
self.close()
else:
self.show()
def show(self):
if not self.isInit:
self.init()
gMyRoot.add_screen(self.screen)
gMyRoot.add_screen(self.editorLayout.screen)
gMyRoot.add_screen(self.tutorialLayout.screen)
gMyRoot.current_screen(self.screen)
self.oldTouchPrev = gMyRoot.getTouchPrev()
gMyRoot.setTouchPrev(self.touchPrev)
self.setMode(szConsole)
def close(self):
gMyRoot.setTouchPrev(self.oldTouchPrev)
gMyRoot.remove(self)
gMyRoot.remove_screen(self.screen)
gMyRoot.remove_screen(self.editorLayout.screen)
gMyRoot.remove_screen(self.tutorialLayout.screen)
if self.bExitOnTouchPrev:
self.editorLayout.exit()
gMyRoot.exit()
def exit(self, *args):
self.reFocusInputText = False
self.inputBoxForceFocus(False)
gMyRoot.popup("Exit Python?", "", self.close, None)