-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
135 lines (100 loc) · 4.64 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
# transform variable to 2 json FB's
import tc_dom_reader
import tc_sax_writter
import tc_extract_from_gvl as extractor
import os
import json
import tkinter as tk
from tkinter.filedialog import askopenfilename
from tkinter.filedialog import askdirectory
CONFIG_FILE_NAME = 'tc_extractor_config.json'
def search_TcGVL_file():
filename = askopenfilename(filetypes=(("Beckhoff var files", "*.TcGVL"), ("All files", "*.*")))
ent1.delete(0, tk.END)
ent1.insert(tk.END, filename) # add this
prj_config['file_to_extract'] = filename
def search_project_path():
dir_name = askdirectory()
ent2.delete(0, tk.END)
ent2.insert(tk.END, dir_name) # add this
prj_config['project_path'] = dir_name
def load_config():
config = {'project_path': os.path.dirname(os.path.realpath(__file__)), 'file_to_extract': ''}
config_file_path = os.path.dirname(os.path.realpath(__file__)) + '\\' + CONFIG_FILE_NAME
try:
config_file = open(config_file_path, 'r')
config = json.loads(config_file.read())
config_file.close()
except IOError:
config_file = open(config_file_path, 'w')
config_file.write(json.dumps(config))
print('a new configuration file has been created')
config_file.close()
return config
def save_config(config):
config_file_path = os.path.dirname(os.path.realpath(__file__)) + '\\' + CONFIG_FILE_NAME
try:
config_file = open(config_file_path, 'w')
config_file.write(json.dumps(config))
config_file.close()
except IOError:
print('unable to save the file properly')
def generate():
variable_to_parse, namespace = extractor.extract_token_from_file(prj_config['file_to_extract'],
prj_config['project_path'] + '/')
writer_st, writer_var = tc_sax_writter.parse_writer(variable_to_parse, namespace + '.')
reader_st, reader_var = tc_dom_reader.parse_reader(variable_to_parse, namespace + '.')
var_reader.delete('1.0', tk.END)
var_reader.insert(tk.END, writer_var)
st_reader.delete('1.0', tk.END)
st_reader.insert(tk.END, writer_st)
var_writer.delete('1.0', tk.END)
var_writer.insert(tk.END, reader_var)
st_writer.delete('1.0', tk.END)
st_writer.insert(tk.END, reader_st)
# Press the green button in the gutter to run the script.
if __name__ == '__main__':
prj_config = load_config()
root = tk.Tk()
root.title('tc json st code generator')
root.geometry("800x600")
root.columnconfigure(3, weight=1)
root.columnconfigure(4, weight=0)
root.columnconfigure(5, weight=1)
root.columnconfigure(6, weight=0)
root.rowconfigure(8, weight=3)
root.rowconfigure(10, weight=1)
label1 = tk.Label(root, font=40, text='TcGVL file:')
label1.grid(row=2, column=1)
ent1 = tk.Entry(root, font=40)
ent1.insert(0, prj_config['file_to_extract'])
ent1.grid(row=2, column=3, sticky='EW', columnspan=4)
b1 = tk.Button(root, text="Search", font=40, width=10, bg='light blue', command=search_TcGVL_file)
b1.grid(row=2, column=7, sticky='EW', columnspan=2)
label2 = tk.Label(root, font=40, text='Project path:')
label2.grid(row=4, column=1)
ent2 = tk.Entry(root, font=40)
ent2.insert(0, prj_config['project_path'])
ent2.grid(row=4, column=3, sticky='EW', columnspan=4)
b2 = tk.Button(root, text="Search", font=40, width=10, bg='light blue', command=search_project_path)
b2.grid(row=4, column=7, sticky='EW', columnspan=2)
b3 = tk.Button(root, text="Generate", font=40, width=10, bg='light green', command=generate)
b3.grid(row=5, column=7, sticky='EW', columnspan=2)
var_reader_sb = tk.Scrollbar(root)
var_reader_sb.grid(row=8, column=4, sticky='NS')
var_reader = tk.Text(root, font=18, state=tk.NORMAL, yscrollcommand=var_reader_sb.set)
var_reader.grid(row=8, column=1, sticky='EW', columnspan=3)
st_reader_sb = tk.Scrollbar(root)
st_reader_sb.grid(row=10, column=4, sticky='NS')
st_reader = tk.Text(root, font=18, state=tk.NORMAL, yscrollcommand=st_reader_sb.set)
st_reader.grid(row=10, column=1, sticky='EW', columnspan=3)
var_writer_sb = tk.Scrollbar(root)
var_writer_sb.grid(row=8, column=8, sticky='NS')
var_writer = tk.Text(root, font=18, state=tk.NORMAL, yscrollcommand=var_writer_sb.set)
var_writer.grid(row=8, column=5, sticky='EW', columnspan=3)
st_writer_sb = tk.Scrollbar(root)
st_writer_sb.grid(row=10, column=8, sticky='NS')
st_writer = tk.Text(root, font=18, state=tk.NORMAL, yscrollcommand=st_writer_sb.set)
st_writer.grid(row=10, column=5, sticky='EW', columnspan=3)
root.mainloop()
save_config(prj_config)