-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileSaveLoad.py
427 lines (361 loc) · 12.2 KB
/
FileSaveLoad.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
421
422
423
424
425
426
427
from kivy.lang import Builder
from kivy.uix.widget import Widget
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.floatlayout import FloatLayout
from kivy.properties import ObjectProperty, StringProperty, BooleanProperty
from kivy.uix.image import Image
from kivy.core.window import Window
from kivy.uix.label import Label
from CodePiece import CodePieceGenerator, CodePieceGeneratorLimited, CodePiece
from CodeSpace import CodeLinePlus
from RunCode import CodeRunner
from Tkinter import Tk
from tkFileDialog import askopenfilename, asksaveasfilename
from json import JSONEncoder, dump, load
from os.path import splitext, isfile
Tk().withdraw()
Builder.load_file("FileSaveLoad.kv")
fileoptions = {'defaultextension': '.pyp',
'filetypes' : [('PythonPieces files', '.pyp'),
('PythonPieces template file', '.pypt'),
('all files', '.*')]}
insertString = "#INSERTHERE#"
class VersionEncoder(JSONEncoder):
def default(self, obj):
if isinstance(obj, (Version,GeneratorRep)):
d = {'__class__':obj.__class__.__name__}
d.update(obj.__dict__)
return d
return JSONEncoder.default(self, obj)
def dict_to_object(d):
if '__class__' in d:
class_name = d.pop('__class__')
if class_name == 'Version':
return Version(genL = d['generatorList'], codeL = d['codeList'])
elif class_name == 'GeneratorRep':
return GeneratorRep(text = d['text'], count=d['count'], color = d['color'], bkgdColor =d['bkgdColor'])
else:
return None
else:
return d
class MenuBar(FloatLayout):
undoStack = ObjectProperty(None)
redoStack = ObjectProperty(None)
maxStackSize = 100
currentVersion = None
currentFileName = None
loadB = ObjectProperty(None)
reloadB = ObjectProperty(None)
saveB = ObjectProperty(None)
saveAsB = ObjectProperty(None)
undoB = ObjectProperty(None)
redoB = ObjectProperty(None)
runB = ObjectProperty(None)
# settingsB = ObjectProperty(None)
# settingspanel = ObjectProperty(None)
argsInput = ObjectProperty(None)
# stopB = ObjectProperty(None)
def __init__(self, **kw):
super(MenuBar, self).__init__(**kw)
self.undoStack = []
self.redoStack = []
self.currentVersion = Version(lines = 15)
self.coderunner = CodeRunner()
self._keyboard = Window.request_keyboard(self._keyboard_closed, self)
self._keyboard.bind(on_key_down=self._on_keyboard_down)
self._keyboard.bind(on_key_up=self._on_keyboard_up)
self.lctrlPress = False
self.rctrlPress = False
self.rshiftPress = False
self.shiftPress = False
# self.settingspanel = DropDown()
# self.fontslider = FontSizeSlider()
# self.fontslider.bind(on_value = self.updateFontSize)
# self.settingspanel.add_widget(self.fontslider)
# App.get_running_app().bind(on_stop = self.stopCode)
def toggleBlockMaker(self):
self.parent.workspace.toggleBlockMaker()
def saveAndRunCode(self):
codefilename = self.saveCode()
if not codefilename:
return None
self.coderunner.startCode(codefilename, self.argsInput.text)
# def stopCode(self):
# self.coderunner.stopProc()
def saveCode(self):
if not self.currentFileName:
return None
savefilename = self.currentFileName + '.py'
f = open(savefilename, 'w')
codeTemplatefilename = self.currentFileName + '.pypc'
if isfile(codeTemplatefilename):
with open(codeTemplatefilename, 'r') as fc:
codelines = self.parent.workspace.getCodeLines()
for line in fc:
if insertString in line:
#get indentation
tabs = line.split(insertString)[0]
#insert code here
for cline in codelines:
f.write(tabs + cline + "\n")
else:
f.write(line)
else: #just write code to file
codestring = self.parent.workspace.getCode()
f.write(codestring)
f.close()
print "saved code to " + savefilename
return savefilename
def loadVersion(self):
loadfilename = askopenfilename(**fileoptions)
if not loadfilename:
return None
f = open(loadfilename, 'r')
loadV = load(f,object_hook=dict_to_object)
if not isinstance(loadV, Version):
print "Error loading from " + loadfilename
return None
self.undoStack = []
self.redoStack = []
self.currentFileName, ext = splitext(loadfilename)
self.currentVersion = loadV
self.currentVersion.setAsCurrent(self.parent.workspace)
f.close()
self.undoB.pressable = False
self.redoB.pressable = False
self.saveB.pressable = True
self.runB.pressable = True
templatefilename = self.currentFileName + '.pypt'
self.reloadB.pressable = isfile(templatefilename)
print "loaded from " + loadfilename
self.get_root_window().set_title(loadfilename)
return loadfilename
def reloadFromTemplate(self):
if not self.currentFileName:
return None
loadfilename = self.currentFileName + '.pypt'
if not isfile(loadfilename):
print "No template to load from"
return None
f = open(loadfilename, 'r')
loadV = load(f,object_hook=dict_to_object)
if not isinstance(loadV, Version):
print "Error loading from " + loadfilename
return None
self.undoStack.append(self.currentVersion)
self.currentVersion = loadV
self.redoStack = []
self.currentVersion.setAsCurrent(self.parent.workspace)
f.close()
self.undoB.pressable = True
self.redoB.pressable = False
print "reloaded from " + loadfilename
self.get_root_window().set_title(loadfilename)
return loadfilename
def saveCurrentVersionAs(self):
savefilename = asksaveasfilename(**fileoptions)
if not savefilename:
return None
self.currentFileName, ext = splitext(savefilename)
f = open(savefilename, 'w')
dump(self.currentVersion,f, **{"cls":VersionEncoder})
f.close()
self.saveB.pressable = False
self.runB.pressable = True
print "saved to " + savefilename
self.get_root_window().set_title(savefilename)
return savefilename
def saveCurrentVersion(self):
if not self.currentFileName:
self.saveB.pressable = False
return None
savefilename = self.currentFileName + '.pyp'
f = open(savefilename, 'w')
dump(self.currentVersion,f, **{"cls":VersionEncoder})
f.close()
self.saveB.pressable = False
print "saved to " + savefilename
self.get_root_window().set_title(savefilename)
return savefilename
def updateVersion(self):
if self.currentVersion:
self.undoStack.append(self.currentVersion)
while len(self.undoStack) > self.maxStackSize :
self.undoStack.pop(0)
self.currentVersion = Version().fromState(self.parent.workspace)
self.redoStack = []
if self.currentFileName:
self.saveB.pressable = True
self.undoB.pressable = True
self.redoB.pressable = False
def undoLast(self):
if len(self.undoStack) == 0:
return
self.redoStack.append(self.currentVersion)
while len(self.redoStack) > self.maxStackSize :
self.redoStack.pop(0)
self.currentVersion = self.undoStack.pop()
self.currentVersion.setAsCurrent(self.parent.workspace)
if self.currentFileName:
self.saveB.pressable = True
self.redoB.pressable = True
self.undoB.pressable = True if (len(self.undoStack) > 0) else False
def redoLast(self):
if len(self.redoStack) == 0:
return
self.undoStack.append(self.currentVersion)
while len(self.undoStack) > self.maxStackSize :
self.undoStack.pop(0)
self.currentVersion = self.redoStack.pop()
self.currentVersion.setAsCurrent(self.parent.workspace)
if self.currentFileName:
self.saveB.pressable = True
self.undoB.pressable = True
self.redoB.pressable = True if len(self.redoStack) > 0 else False
#These keyboard shortcuts are based in Windows standards
def _keyboard_closed(self):
pass
# self._keyboard.unbind(on_key_down = self._on_keyboard_down)
# self._keyboard.unbind(on_key_up = self._on_keyboard_up)
# self._keyboard = None
def _on_keyboard_up(self, keyboard, keycode):
if keycode[1] == 'lctrl':
self.lctrlPress = False
elif keycode[1] == 'rctrl':
self.rctrlPress = False
elif keycode[1] == 'shift':
self.shiftPress = False
elif keycode[1] == 'rshift':
self.rshiftPress = False
return True
def _on_keyboard_down(self, keyboard, keycode, text, modifiers):
if keycode[1] == 'lctrl':
self.lctrlPress = True
elif keycode[1] == 'rctrl':
self.rctrlPress = True
elif keycode[1] == 'shift':
self.shiftPress = True
elif keycode[1] == 'rshift':
self.rshiftPress = True
else :
if not (self.lctrlPress or self.rctrlPress) :
return True
if keycode[1] == 's':
if (self.shiftPress or self.rshiftPress):
self.saveCurrentVersionAs()
else:
self.saveCurrentVersion()
elif keycode[1] == 'o':
self.loadVersion()
elif keycode[1] == 'z' :
#if blockmaker has text focus, don't override the undo
if not self.parent.workspace.blockmaker.is_text_focused() :
self.undoLast()
elif keycode[1] == 'u':
self.undoLast()
elif keycode[1] == 'y':
self.redoLast()
elif keycode[1] == 'r':
#if blockmaker has text focus, don't override the redo
if not self.parent.workspace.blockmaker.is_text_focused() :
self.redoLast()
elif keycode[1] == 'backspace':
if (self.shiftPress or self.rshiftPress):
self.reloadFromTemplate()
elif keycode[1] == 'enter':
if (self.shiftPress or self.rshiftPress):
self.stopCode()
else:
self.saveAndRunCode()
return True
class GeneratorRep:
text = None
count = None
color = None
bkgdColor = None
def __init__(self, text, count, color, bkgdColor):
self.text = text
self.count = count
self.color = color
self.bkgdColor = bkgdColor
def __repr__(self):
return str(self.count) + " " + self.text + " " + str(self.color) + " " + str(self.bkgdColor)
#representation of state of codeSpace and blockSpace, can be used to restore the state of a projec
class Version:
generatorList = [] #list of generator representations
codeList = [] #list of code lines, values are lists of IDs that refer to piece list's generators
def __init__(self, lines = 10, genL = [], codeL = None):
self.generatorList = genL
if not codeL:
self.codeList = []
for x in range(lines):
self.codeList.append([])
else :
self.codeList = codeL
#Write Version from the state of the workspace
def fromState(self, workspace):
codespace, blockspace = workspace.codespace, workspace.blockspace
pieceIdDict = {} #keys: instances of generators, values ID numbers
self.generatorList = []
self.codeList = []
i = len(blockspace.children)
for gen in blockspace.children:
i -= 1
pieceIdDict[gen] = i
rep = GeneratorRep(text = gen.codetext, count = gen.count, color=gen.color, bkgdColor = gen.bkgdColor)
self.generatorList.insert(0,rep)
for codelineplus in codespace.children:
codeline = codelineplus.codeline
linelist = []
for piece in codeline.children:
id = pieceIdDict[piece.generator]
linelist.insert(0,id)
self.codeList.insert(0,linelist)
return self
#Overwrite workspace with this version
def setAsCurrent(self, workspace):
codespace, blockspace = workspace.codespace, workspace.blockspace
blockspace.clear_widgets()
idPieceDict = [] #keys: indexes, values: generator instances
#populate the blockspace, keep records on generators
for rep in self.generatorList:
generator = None
if rep.count >= 0:
generator = CodePieceGeneratorLimited(workspace = workspace, start_text = rep.text,
color = rep.color, bkgdColor = rep.bkgdColor, max_count = rep.count)
else:
generator = CodePieceGenerator(workspace = workspace, start_text = rep.text,
color = rep.color, bkgdColor = rep.bkgdColor)
idPieceDict.append(generator)
blockspace.add_widget(generator, index = 0)
codespace.clear_widgets()
#populate codespace, each line, linking pieces to generators
for line in self.codeList :
codelineplus = CodeLinePlus(fontsize = workspace.fontsize)
codeline = codelineplus.codeline
for id in line:
piece = CodePiece(generator = idPieceDict[id])
codeline.add_widget(piece, index = 0)
codespace.add_widget(codelineplus, index = 0)
codespace.updateLineNums()
def toStringDebug(self):
repr = ""
i = 0
for rep in self.generatorList:
repr += str(i) + " : " + str(rep) + "\n"
i += 1
j = 0
for line in self.codeList:
j += 1
repr += str(j) + " :"
for id in line:
repr += " " + str(id)
repr += "\n"
return repr
class MenuButton(Button):
imageSource = StringProperty('')
hoverText = StringProperty('')
pressable = BooleanProperty(True)
class MenuSpacer(Widget):
pass