-
Notifications
You must be signed in to change notification settings - Fork 0
/
shell.py
86 lines (72 loc) · 2.33 KB
/
shell.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
# -*- coding: utf-8 -*-
from code import InteractiveConsole
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.uix.textinput import TextInput
from kivy.uix.scrollview import ScrollView
from sympy import *
from sympy.abc import *
from sympy.utilities.lambdify import lambdify
import sys
from keyboard import *
FONT_NAME = "res/ubuntu-font-family-0.80/UbuntuMono-R.ttf"
FONT_SIZE = 18
DEBUG = False
class PyShell( BoxLayout ) :
def __init__( self, plotFoo ) :
BoxLayout.__init__( self, orientation="vertical" )
self._lastOutput = []
self.console = InteractiveConsole()
frm = BoxLayout( orientation="vertical" )
self.listed = TextInput()
self.listed.font_size = FONT_SIZE
self.listed.font_name = FONT_NAME
self.listed.readonly = True
self.listed.size_hint = 1, 0.3
frm.add_widget( self.listed )
self.kb = KiPyKeyboard( self.onBtnExecPress, plotFoo )
self.kb.size_hint = 1, 0.7
frm.add_widget( self.kb )
frm.size_hint = 1,1
self.add_widget( frm )
def start( self ) :
if not DEBUG :
sys.stdout = self
sys.stderr = self
self.shellInit()
def shellInit( self ) :
initCode = open( "res/SHELL_INIT" ).read()
for line in initCode.split("\n") :
self.console.push( line )
def write( self, sometext ) :
self._lastOutput.append( sometext )
self.listed.text += sometext
def correctInput( self, stat ) :
if stat == "\n" : stat = "ans\n"
print( "in : " + stat[:-1] )
if stat.upper() in ( "ANS\n", "ANS" ) :
return "print( ans )", False
keys = [ "DEF", "FOR", "WHILE", "IMPORT", "EXEC" ]
for key in keys :
if key in stat.upper() :
return stat, False
return "ans = " + stat, True
def onBtnExecPress( self, instance ) :
stat = self.kb.current.text + "\n"
stat, printANS = self.correctInput( stat )
#Save current ANS.
if printANS : self.console.push( "last_ANS = ans\n" )
#Execute statement.
moreInputNeeded = self.console.push( stat )
#Check for multiline instructions.
if moreInputNeeded :
print( "#Multiline instructions must be entered in one fell swoop!" )
self.console.push( "\n" )
#The input was ok.
else :
#If ANS is None, restore last_ANS.
self.console.push( "if ans is None : ans = last_ANS\n" )
#Print ANS, flush input buffer.
if printANS : self.console.push( "print( ans )\n" )
self.kb.flush()