-
Notifications
You must be signed in to change notification settings - Fork 0
/
easgui.py
179 lines (143 loc) · 8.22 KB
/
easgui.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
# -*- coding: utf-8 -*-
"""
Created on Thu May 14 01:06:33 2020
@author: Jose Amoroso
"""
from textx import metamodel_from_file
from errorMessages import TextXSemanticError, TextXRuntimeError
from tkinter import Tk,Button,Label,Entry,LEFT,HORIZONTAL,BOTH,TOP,X,Frame,RAISED,Checkbutton,IntVar
from tkinter import ttk
from functionCaller import wrap, procMessage,bcolors,underlineStr
from functools import partial
import inspect
import numpy as np
import sys
############ Semantics ##############################
def checkSemantics(model, metamodel):
modelCommands = model.commands
inputIDList=[]
importedFunctions = []
for command in modelCommands:
line,col = model._tx_parser.pos_to_linecol(command._tx_position)
if command.__class__.__name__ == "ImportFunction":
importedFunctions.append(command.newFunctionName)
if command.__class__.__name__ == "CreateFunction":
#CHECK REPEATED IDS -> INPUTS
for inputs in command.parameter:
inputValue = inputs.inputName
inputType = inputs.inputType
if inputValue in inputIDList:
lineInput,colInput = model._tx_parser.pos_to_linecol(inputs._tx_position)
messageError ="Repeated inputs IDs, check it is not used before.\n\t{} {}".format(
underlineStr(inputValue),underlineStr(inputType))
message = procMessage(messageError,bcolors.BLUE,lineInput,colInput)
raise TextXSemanticError(message,line=line,col=col,filename=model._tx_filename)
inputIDList.append((inputValue))
#Check if function is not declared before
if command.functionName not in importedFunctions:
lineInput,colInput = model._tx_parser.pos_to_linecol(command._tx_position)
messageError ="Used function was not imported before.\n\t{}".format(
underlineStr(command.functionName ))
message = procMessage(messageError,bcolors.BLUE,lineInput,colInput)
raise TextXSemanticError(message,line=line,col=col,filename=model._tx_filename)
################################################################
########## Processors ################################
def createWindowProcessor(createWindowCmd):
# If steps is not given, set it do default 1 value.
if len(createWindowCmd.coor)==1:
createWindowCmd.coor.append(createWindowCmd.coor[0])
if len(createWindowCmd.coor)==0:
createWindowCmd.coor = [500,500]
################################################################
class GUI:
def __init__(self,master):
self.functions = {}
self.master = master
self.functionFlag = False
self.allButton ={}
def interpret(self, model):
for c in model.commands:
if c.__class__.__name__ == "ImportFunction":
package = c.fileName
name = c.functionName
try:
imported = getattr(__import__(package, fromlist=[name]), name)
self.functions[c.newFunctionName]=imported
except ModuleNotFoundError:
lineInput,colInput = model._tx_parser.pos_to_linecol(c._tx_position)
messageError ="FILE {} NOT FOUND".format(underlineStr(package))
message = procMessage(messageError,bcolors.BLUE,lineInput,colInput)
raise TextXRuntimeError(message,line=lineInput,col=colInput,filename=model._tx_filename)
sys.exit()
except AttributeError:
lineInput,colInput = model._tx_parser.pos_to_linecol(c._tx_position)
messageError ="FUNCTION {} NOT FOUND IN {}".format(underlineStr(name),underlineStr(package))
message = procMessage(messageError,bcolors.BLUE,lineInput,colInput)
raise TextXRuntimeError(message,line=lineInput,col=colInput,filename=model._tx_filename)
sys.exit()
if c.__class__.__name__ == "CreateWindow":
self.master.title(c.windowID)
newSize = "{}x{}".format(c.coor[0],c.coor[1])
self.master.geometry(newSize)
if c.__class__.__name__ == "CreateFunction":
self.inputs = {}
self.listValues = None
self.button=None
#Create frame for each function
self.frame = Frame(self.master, relief=RAISED, borderwidth=1)
self.frame.pack(fill=BOTH, expand=True)
self.tempListValues = []
#Insert line horizontal
if (self.functionFlag):
self.separ = ttk.Separator(self.master, orient=HORIZONTAL)
self.separ.pack(side=TOP,fill=BOTH, expand=True)
self.functionFlag = True
######################################################################
########################## Create inputs #############################
######################################################################
for inputs in c.parameter:
self.frameInner = Frame(self.frame, relief=RAISED)
self.frameInner.pack(fill=BOTH, expand=True)
line,col= model._tx_parser.pos_to_linecol(inputs._tx_position)
self.inputPosition = (line,col)
self.inputType = inputs.inputType
if self.inputType == 'OUTPUT':
self.label = Label(self.frameInner, text=inputs.inputName+':', anchor='w',font=("Calibri", 10, "bold underline"))
self.entry = Entry(self.frameInner,state='disabled')
self.label.pack(side=LEFT,padx=5, pady=5)
self.entry.pack(side=LEFT,fill=X,expand=True,padx=10)
self.tempListValues.append((self.entry,self.inputType,self.inputPosition,inputs.inputName))
elif self.inputType == 'BOOLINPUT':
self.label = Label(self.frameInner, text=inputs.inputName+':', anchor='w')
self.label.pack(side=LEFT,padx=5, pady=5)
self.checkvar = IntVar()
self.Checkbutton = ttk.Checkbutton(self.frameInner)
self.Checkbutton.state(['!alternate'])
self.Checkbutton.pack(side=LEFT,fill=X,padx=10)
self.tempListValues.append((self.Checkbutton,self.inputType,self.inputPosition,inputs.inputName))
else:
self.label = Label(self.frameInner, text=inputs.inputName+':', anchor='w')
self.entry = Entry(self.frameInner)
self.label.pack(side=LEFT,padx=5, pady=5)
self.entry.pack(side=LEFT,fill=X,expand=True,padx=10)
self.tempListValues.append((self.entry,self.inputType,self.inputPosition,inputs.inputName))
######################################################################
################### Create button with function ######################
######################################################################
self.listValues = tuple(self.tempListValues)
self.button = Button(self.frame, text=c.buttonName)
self.button.configure(command=partial(wrap,self.functions[c.functionName],self.listValues))
self.button.pack(pady=5)
######################################################################
def main(debug=False):
gui_mm = metamodel_from_file('tx_easgui/easgui.tx')
gui_mm.register_model_processor(checkSemantics)
gui_mm.register_obj_processors({'CreateWindow': createWindowProcessor})
gui_model = gui_mm.model_from_file('program.eui')
# gui_model = gui_mm.model_from_file(str(sys.argv[1]))
root = Tk()
my_gui = GUI(root)
my_gui.interpret(gui_model)
root.mainloop()
if __name__ == "__main__":
main()