-
Notifications
You must be signed in to change notification settings - Fork 0
/
k2_interface.py
165 lines (145 loc) · 4.06 KB
/
k2_interface.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
import kivy
#kivy.require('1.2.0')
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.button import Button
from kivy.uix.textinput import TextInput
from kivy.clock import Clock
from kivy.properties import ObjectProperty, NumericProperty, ListProperty
from kivy.factory import Factory
from kivy.config import ConfigParser
from kivy.uix.settings import Settings
import rpncalc
import settings
interp = rpncalc.Interpreter(rpncalc.ops, {})
def parse(input_string):
global mode
if input_string[0] == ':': # interface commands start with a colon
input_string = input_string[1:]
text = input_string.split()
if text[0] == 'step':
interp.step()
elif text[0] == 'run':
interp.resume()
elif text[0] == 'import':
import_file(os.path.join(functions_directory, text[1]))
elif text[0] == 'export':
f = open(os.path.join(settings.functions_directory, text[1]), 'w+')
commands = interp.stack[-1].stack
for cmd in commands:
f.write(cmd)
f.write('\n')
f.close()
elif text[0] == 'quit':
raise ShutErDownBoys()
elif text[0] == '!':
try:
command = ''
for character in input_string[1:]:
if character == '?':
command += str(interp.pop()[0].val)
else:
command += character
res = eval(command)
interp.parse(str(res))
except Exception as e:
raise BadPythonCommand('Bad Python Command (' + command + ') ' + e.message)
elif text[0] == 'graph':
if len(text) > 1:
if text[1] == 'x':
mode = GRAPH_X
interp.message("X Graph Mode")
elif text[1] == 'xy':
mode = GRAPH_XY
interp.message("XY Graph Mode")
elif mode == GRAPH_XY:
mode = GRAPH_X
interp.message("X Graph Mode")
else:
mode = GRAPH_XY
interp.message("XY Graph Mode")
elif text[0] == 'stack':
interp.message("Stack Mode")
mode = STACK
else:
interp.parse(input_string, True)
class Calculator(Widget):
entryline = ObjectProperty(None)
lastOp = ""
memorynum = ''
operation = False
stackdisplay = []
def _execute(self, op):
if op in '1234567890.':
self.memorynum += op
self.entryline.text = self.memorynum
return
else:
if self.memorynum:
parse(self.memorynum)
self.memorynum = ''
if op not in ['enter', 'step', 'resume']:
parse(op)
else:
if op == 'step':
interp.step()
elif op == 'resume':
interp.run()
self.entryline.text = self.memorynum
stack_size = len(interp.stack)
for x in xrange(len(self.stackdisplay)):
if x < stack_size:
self.stackdisplay[x].value.text = str(interp.stack[-(x + 1)])
else:
self.stackdisplay[x].value.text = ''
def _setText(self, op):
if op == "." and "." in self.entryline.text:
return
if self.operation:
self.entryline.text = op
self.operation = False
else:
self.entryline.text += op
def setup_stack_display(self, count):
pass
class CalcApp(App):
use_kivy_settings = False
def build(self):
self.Calc = Calculator()
self.Calc.stackdisplay.append(self.Calc.stackline0)
self.Calc.stackdisplay.append(self.Calc.stackline1)
self.Calc.stackdisplay.append(self.Calc.stackline2)
self.Calc.stackdisplay.append(self.Calc.stackline3)
for x in range(len(self.Calc.stackdisplay)):
self.Calc.stackdisplay[x].number.text = str(x)
return self.Calc
def build_config(self, config):
config.setdefaults('Calculator Colors', \
{
'CC' : 'Off',
'NB' : 'default',
'NBT': 'default',
'OB' : 'default',
'OBT' : 'default',
'SB' : 'default',
'SBT' : 'default',
'TIB' : 'default',
'TIF' : 'default'
})
def on_config_change(self, config, section, key, value):
if config is self.config:
if key == "CC" and value == "On":
pass
else:
print section, key, value
#def build_settings(self, settings):
#settings.add_json_panel('Customization',
#self.config,
#"settings_calc.json")
def on_pause(self):
return True
def on_resume(self):
pass
if __name__=='__main__':
CA = CalcApp()
CA.run()