302 lines
11 KiB
Python
302 lines
11 KiB
Python
from tkinter import messagebox
|
|
import tkinter as tk
|
|
import ast
|
|
import easygui
|
|
from tkinter import filedialog
|
|
class block():
|
|
def __init__(self, combinations, executor, type=-1, args=[]):
|
|
self.type = type
|
|
self.combinations = combinations
|
|
self.executor = executor
|
|
self.args = args
|
|
|
|
def gettypeintext(self):
|
|
if self.type in self.combinations:
|
|
return self.combinations[self.type]["name"]
|
|
else:
|
|
return "ERROR 1"
|
|
|
|
def execute(self):
|
|
#preparing arguments
|
|
preparedargs = []
|
|
for i in self.args:
|
|
if "<variable>" in i:
|
|
temp = i.replace("<variable>", "")
|
|
temp = self.executor.getvariable(temp)
|
|
preparedargs.append(temp)
|
|
elif "<function>" in i:
|
|
temp = i.replace("<function>", "")
|
|
tempargs = temp.split("<args>")
|
|
tempargs = tempargs[1]
|
|
tempargs = ast.literal_eval(tempargs)
|
|
temp = temp.split("<args>")
|
|
temp = temp[0]
|
|
temp = self.executor.texttointtype(temp)
|
|
if self.type in self.combinations:
|
|
temp2 = {"executor": self.executor, "block": self, "args": tempargs, "out": ""}
|
|
try:
|
|
exec(self.combinations[temp]["code"], temp2)
|
|
temp = temp2["out"]
|
|
except Exception as e:
|
|
messagebox.showerror("fehler", "ERROR 2: block type: {} error information: {}".format(self.type, e))
|
|
temp = "ERROR 2: block type: {} error information: {}".format(self.type, e)
|
|
else:
|
|
messagebox.showerror("fehler", "ERROR 3: block type: {}".format(self.type))
|
|
temp = "ERROR 3: block type: {}".format(self.type)
|
|
preparedargs.append(temp)
|
|
else:
|
|
preparedargs.append(i)
|
|
#code execution
|
|
if self.type in self.combinations:
|
|
temp2 = {"executor": self.executor, "block": self, "args": preparedargs}
|
|
try:
|
|
exec(self.combinations[self.type]["code"], temp2)
|
|
except Exception as e:
|
|
messagebox.showerror("fehler", "ERROR 2: block type: {} error information: {}".format(self.type, e))
|
|
else:
|
|
messagebox.showerror("fehler", "ERROR 3: block type: {}".format(self.type))
|
|
|
|
def converttotext(self):
|
|
temp = {"type": self.type, "args": self.args}
|
|
return temp
|
|
|
|
class blockstream():
|
|
def __init__(self, blocks):
|
|
self.blocks = blocks
|
|
|
|
def addblock(self, block):
|
|
self.blocks.append(block)
|
|
|
|
def execute(self):
|
|
for i in self.blocks:
|
|
i.execute()
|
|
|
|
def executespecblock(self, index):
|
|
try:
|
|
self.blocks[index].execute()
|
|
except:
|
|
pass
|
|
|
|
def getnames(self):
|
|
temp = []
|
|
for i in self.blocks:
|
|
temp.append(i.gettypeintext())
|
|
return temp
|
|
|
|
def getargs(self):
|
|
temp = []
|
|
for i in self.blocks:
|
|
temp.append(i.args)
|
|
return temp
|
|
|
|
class executor():
|
|
def __init__(self, combinations):
|
|
self.blockstream = blockstream([])
|
|
self.combinations = combinations
|
|
self.container = tk.Tk()
|
|
self.buttons = []
|
|
self.block = 0
|
|
self.runloop = False
|
|
self.variables = {}
|
|
self.update()
|
|
|
|
def startloop(self):
|
|
self.container.mainloop()
|
|
|
|
def addblock(self, type, args):
|
|
self.blockstream.addblock(block(self.combinations, self, type=type, args=args))
|
|
|
|
def delete(self, index):
|
|
self.blockstream.blocks.remove(self.blockstream.blocks[index])
|
|
self.update()
|
|
|
|
def executespecblock(self, index):
|
|
self.blockstream.executespecblock(index)
|
|
|
|
def execute(self):
|
|
self.variables = {}
|
|
self.block = 0
|
|
while self.block <= len(self.blockstream.blocks):
|
|
self.executespecblock(self.block)
|
|
self.block += 1
|
|
|
|
def texttointtype(self, type):
|
|
temp = "ERROR 4"
|
|
for i in self.combinations:
|
|
if self.combinations[i]["name"] == type:
|
|
temp = i
|
|
return temp
|
|
|
|
def getallblocknames(self):
|
|
temp = []
|
|
for i in self.combinations:
|
|
temp.append(self.combinations[i]["name"])
|
|
return temp
|
|
|
|
def addblockprepare(self, selected, container):
|
|
selected = selected.get()
|
|
args = []
|
|
for i in range(self.combinations[self.texttointtype(selected)]["args"]):
|
|
reply = easygui.enterbox("argument nummer {}".format(i+1), "argumenten abfrage")
|
|
if reply:
|
|
if reply[0] == "*" and reply[-1] == "*" and reply[1] != "*":
|
|
temp = reply
|
|
temp = temp[1:]
|
|
temp = temp[:-1]
|
|
args.append("<variable>{}".format(temp))
|
|
elif reply[0] == "*" and reply[1] == "*" and reply[-1] == "*" and reply[-2] == "*":
|
|
temp = reply
|
|
temp = temp[1:]
|
|
temp = temp[:-1]
|
|
temp = temp[1:]
|
|
temp = temp[:-1]
|
|
temp = temp.split("[")
|
|
args.append("<function>{}<args>{}".format(temp[0], "["+temp[1]))
|
|
else:
|
|
args.append(reply)
|
|
else:
|
|
args.append("ERROR 6")
|
|
self.addblock(self.texttointtype(selected), args)
|
|
self.update()
|
|
container.destroy()
|
|
|
|
def setargs(self, index, blocktype, container):
|
|
args = []
|
|
for i in range(self.combinations[self.texttointtype(blocktype)]["args"]):
|
|
reply = easygui.enterbox("argument nummer {}".format(i+1), "argumenten abfrage")
|
|
if reply:
|
|
if reply[0] == "*" and reply[-1] == "*":
|
|
temp = reply
|
|
temp = temp[1:]
|
|
temp = temp[:-1]
|
|
args.append("<variable>{}".format(temp))
|
|
else:
|
|
args.append(reply)
|
|
else:
|
|
args.append("ERROR 6")
|
|
self.blockstream.blocks[index].args = args
|
|
self.update()
|
|
container.destroy()
|
|
|
|
def delete2(self, index, container):
|
|
self.blockstream.blocks.remove(self.blockstream.blocks[index])
|
|
self.update()
|
|
container.destroy()
|
|
|
|
def blockmenu(self, index):
|
|
container = tk.Tk()
|
|
temp = self.blockstream.getnames()
|
|
tempargs = self.blockstream.getargs()
|
|
info = tk.Label(container, text="block information: {}".format("'{}' args: {}".format(temp[index], tempargs[index])))
|
|
info.grid()
|
|
deletebutton = tk.Button(container, text="block löschen", command=lambda: self.delete2(index, container))
|
|
deletebutton.grid()
|
|
argsset = tk.Button(container, text="argumente neu setzen", command=lambda: self.setargs(index, temp[index], container))
|
|
argsset.grid()
|
|
back = tk.Button(container, text="zurück", command=container.destroy)
|
|
back.grid()
|
|
container.mainloop()
|
|
|
|
def menu(self):
|
|
container = tk.Tk()
|
|
selectable = self.getallblocknames()
|
|
selected = tk.StringVar(container)
|
|
selected.set(selectable[0])
|
|
menu = tk.OptionMenu(container, selected, *selectable)
|
|
menu.grid()
|
|
addbutton = tk.Button(container, text="block hinzufügen", command=lambda: self.addblockprepare(selected, container))
|
|
addbutton.grid()
|
|
container.mainloop()
|
|
|
|
def makevariable(self, variablename, value="0"):
|
|
self.variables[variablename] = value
|
|
|
|
def deletevariable(self, variablename):
|
|
if variablename in self.variables:
|
|
del self.variables[variablename]
|
|
else:
|
|
messagebox.showerror("fehler", "ERROR 5")
|
|
|
|
def getvariable(self, variablename):
|
|
try:
|
|
return self.variables[variablename]
|
|
except:
|
|
messagebox.showerror("fehler", "ERROR 5")
|
|
return "ERROR 5"
|
|
|
|
def setvariable(self, variablename, value):
|
|
if variablename in self.variables:
|
|
self.variables[variablename] = value
|
|
else:
|
|
messagebox.showerror("fehler", "ERROR 5: set var")
|
|
|
|
def manuelsave(self, path):
|
|
temp = []
|
|
for i in self.blockstream.blocks:
|
|
temp.append(i.converttotext())
|
|
file = open(path, 'w')
|
|
file.write(str(temp))
|
|
file.close()
|
|
|
|
def manuelload(self, path):
|
|
file = open(path, 'r')
|
|
file = file.read()
|
|
file = ast.literal_eval(file)
|
|
self.blockstream.blocks = []
|
|
for i in file:
|
|
self.addblock(i["type"], i["args"])
|
|
self.update()
|
|
|
|
def save(self):
|
|
temp = []
|
|
for i in self.blockstream.blocks:
|
|
temp.append(i.converttotext())
|
|
file = open(filedialog.asksaveasfilename(defaultextension=".BPE"), 'w')
|
|
file.write(str(temp))
|
|
file.close()
|
|
messagebox.showinfo("fertig", "speichern abgeschlossen")
|
|
|
|
def load(self):
|
|
file = open(filedialog.askopenfilename(), 'r')
|
|
file = file.read()
|
|
file = ast.literal_eval(file)
|
|
self.blockstream.blocks = []
|
|
for i in file:
|
|
self.addblock(i["type"], i["args"])
|
|
self.update()
|
|
|
|
def update(self):
|
|
try:
|
|
self.executebutton.grid_forget()
|
|
self.addblockn.grid_forget()
|
|
self.savebutton.grid_forget()
|
|
self.loadbutton.grid_forget()
|
|
except:
|
|
pass
|
|
for i in self.buttons.copy():
|
|
i.grid_forget()
|
|
self.buttons.remove(i)
|
|
temp = self.blockstream.getnames()
|
|
tempargs = self.blockstream.getargs()
|
|
for i in range(len(temp)):
|
|
index = i
|
|
i = temp[i]
|
|
temp2 = tk.Button(self.container, text="'{}' args: {}".format(i, tempargs[index]), command=lambda index=index: self.blockmenu(index))
|
|
temp2.grid()
|
|
self.buttons.append(temp2)
|
|
self.executebutton = tk.Button(self.container, text="ausführen", command=self.execute)
|
|
self.executebutton.grid()
|
|
self.addblockn = tk.Button(self.container, text="block hinzufügen", command=self.menu)
|
|
self.addblockn.grid()
|
|
self.savebutton = tk.Button(self.container, text="speichern", command=self.save)
|
|
self.savebutton.grid()
|
|
self.loadbutton = tk.Button(self.container, text="laden", command=self.load)
|
|
self.loadbutton.grid()
|
|
|
|
combinations = open("combinations.txt", 'r')
|
|
combinations = combinations.read()
|
|
combinations = ast.literal_eval(combinations)
|
|
temp = executor(combinations)
|
|
temp.startloop()
|
|
|