-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
310 lines (256 loc) · 7.97 KB
/
main.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
import kivy
kivy.require('1.2.0')
import copy
import pickle
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
from kivy.uix.popup import Popup
from kivy.uix.floatlayout import FloatLayout
from kivy.core.window import Window
#Window.size = (640, 1163)
import os
import pkgutil
import sys
import rpncalc
import settings
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)
##########
interp = rpncalc.Interpreter(rpncalc.ops, {})
def import_file(filename):
f = open(filename)
commands = f.read()
f.close()
for command in commands.split('\n'):
if len(command) > 0:
if command[0] == "#":
continue
parse(command)
if settings.auto_import_functions:
for dirpath, dirnames, filenames in os.walk(settings.auto_functions_directory):
for filename in filenames:
import_file(os.path.join(settings.auto_functions_directory, filename))
##########
class LoadDialog(FloatLayout):
load = ObjectProperty(None)
cancel = ObjectProperty(None)
class ImportDialog(FloatLayout):
import_file = ObjectProperty(None)
cancel = ObjectProperty(None)
class SaveDialog(FloatLayout):
save = ObjectProperty(None)
text_input = ObjectProperty(None)
cancel = ObjectProperty(None)
class Calculator(Widget):
entryline = ObjectProperty(None)
lastOp = ""
operation = False
stackdisplay = []
loadfile = ObjectProperty(None)
savefile = ObjectProperty(None)
text_input = ObjectProperty(None)
def dismiss_popup(self):
self._popup.dismiss()
def show_load(self):
content = LoadDialog(load=self.load, cancel=self.dismiss_popup)
self._popup = Popup(title="Load File", content=content,
size_hint=(0.9, 0.9))
self._popup.open()
def show_import(self):
content = ImportDialog(import_file=self.import_file, cancel=self.dismiss_popup)
self._popup = Popup(title="Import File", content=content,
size_hint=(0.9, 0.9))
self._popup.open()
def show_save(self):
content = SaveDialog(save=self.save, cancel=self.dismiss_popup)
self._popup = Popup(title="Save file", content=content,
size_hint=(0.9, 0.9))
self._popup.open()
def import_file(self, path, filename):
filenamefull = os.path.join(path, filename[0])
f = open(filenamefull)
commands = f.read()
f.close()
for command in commands.split('\n'):
if len(command) > 0:
if command[0] == "#":
continue
parse(command)
interp.message('Imported Instructions from ' + filenamefull)
self.dismiss_popup()
self.update_screen()
def save(self, path, filename):
filenamefull = os.path.join(path, filename)
#options['defaultextension'] = '.rpn'
#options['filetypes'] = [('all files', '.*'), ('rpn files', '.rpn')]
#options['title'] = 'Save Session'
#filename = tkFileDialog.asksaveasfilename(filetypes=[('RPN files', '.rpn'), ('all files', '*')], title='Load Session')
if filenamefull:
interp2 = copy.deepcopy(interp)
interp2.builtin_functions = {}
interp2.operatorlist = []
f = open(filenamefull, 'wb')
pickle.dump(interp2, f, 2)
f.close()
interp.message('Saved as ' + filenamefull)
print 'saved: ', filenamefull
else:
interp.message('Invalid Filename ' + filenamefull)
self.dismiss_popup()
self.update_screen()
def load(self, path, filename):
filenamefull = os.path.join(path, filename[0])
global interp
if filenamefull:
f = open(filenamefull, 'rb')
tmp = pickle.load(f)
f.close()
tmp.builtin_functions = interp.builtin_functions
tmp.operatorlist = interp.operatorlist
interp = tmp
interp.message('Loaded ' + filenamefull)
else:
interp.message('Invalid Filename ' + filenamefull)
self.dismiss_popup()
self.update_screen()
def _execute(self, op):
if op in ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '.', '00', '[', 'space']:
if op in ['[',]:
op = ' ' + op + ' '
if op in ['space', ]:
op = ' '
self.entryline.text = self.entryline.text + op
if interp.messages:
self.messageline.value.text = interp.messages[-1]
else:
self.messageline.value.text = ''
return
else:
if self.entryline.text:
parse(self.entryline.text)
self.entryline.text = ''
if op not in ['enter', 'step', 'resume']:
parse(op)
else:
if op == 'step':
interp.step()
elif op == 'resume':
interp.run()
self.update_screen()
def update_screen(self):
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 = ''
if interp.messages:
self.messageline.value.text = interp.messages[-1]
else:
self.messageline.value.text = ''
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)
self.Calc.stackdisplay.append(self.Calc.stackline4)
self.Calc.stackdisplay.append(self.Calc.stackline5)
self.Calc.stackdisplay.append(self.Calc.stackline6)
self.Calc.stackdisplay.append(self.Calc.stackline7)
self.Calc.stackdisplay.append(self.Calc.stackline8)
self.Calc.messageline.number.text = "Msg: "
self.Calc.messageline.number.width = '40sp'
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":
print "fuck"
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()