-
Notifications
You must be signed in to change notification settings - Fork 0
/
gui.py
333 lines (281 loc) · 14.8 KB
/
gui.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
#for UI
import customtkinter as ct
#file dialog
import tkinter as tk
from tkinter import filedialog
#for getting the file name from path
import os
#for matplotlib
import matplotlib.backends.backend_tkagg
#for program features
from formatting import minify, prettify
from xml2json import xml2json
from correct_xml import correct_xml
from xml_error_detector import XML_error_detector
# Social Network
from build_graph_network_from_xml import *
# Compression
from BPE import BPE
bpe = BPE()
ct.set_appearance_mode("system") # Modes: system (default), light, dark
ct.set_default_color_theme("dark-blue") # Themes: blue (default), dark-blue, green
class Ui :
def __init__(self):
self.root = ct.CTk()
#init members
self.inputPath = ""
#init window dimensions + title
self.root.geometry("950x470")
self.root.title("XML Parsiewer")
#make the first column -> editor textbox and import file button
self.editor_text_box = ct.CTkTextbox(self.root, height=400, width=270, wrap='none', undo=True, autoseparators=True)
self.editor_text_box.grid(row=0, column=0, padx=10, pady=10,sticky='n')
self.importFileButton = ct.CTkButton(self.root, width=270, text="Import File", command=self.chooseInputFile)
self.importFileButton.grid(row=1, column=0, padx=10, pady=10)
#make feature buttons -> second column
self.frame_of_frames = ct.CTkFrame(self.root)
self.frame_of_frames.grid(row=0, column=1, padx=10, pady=15, sticky='n')
self.out_to_in_button = ct.CTkButton(self.frame_of_frames, text="Copy Output to Input for Modifications", command=self.copy_inout)
self.out_to_in_button.grid(row=2, column=0, padx=10, pady=5, sticky='n', columnspan = 2)
self.in_to_out_button = ct.CTkButton(self.frame_of_frames, text="Copy Input to Output for Compression", command=self.copy_outin)
self.in_to_out_button.grid(row=1, column=0, padx=10, pady=5, sticky='n', columnspan = 2)
# Create a frame for the second column buttons
self.frame_buttons1 = ct.CTkFrame(self.frame_of_frames)
self.frame_buttons1.grid(row=0, column=0, padx=10, pady=5, sticky='n')
self.minifyButton = ct.CTkButton(self.frame_buttons1, text="Minify", command=self.minify)
self.minifyButton.grid(row=0, column=0, padx=10, pady=5, columnspan=2)
self.prettifyButton = ct.CTkButton(self.frame_buttons1, text="Prettify", command=self.prettify)
self.prettifyButton.grid(row=1, column=0, padx=10, pady=5, columnspan=2)
self.convertToJsonButton = ct.CTkButton(self.frame_buttons1, text="Convert to JSON", command=self.xml2json)
self.convertToJsonButton.grid(row=2, column=0, padx=10, pady=5, columnspan=2)
self.compressButton = ct.CTkButton(self.frame_buttons1, text="Compress Output", command=self.compress)
self.compressButton.grid(row=3, column=0, padx=10, pady=5, columnspan=2)
# Add entry widgets for user IDs
self.iterations = ct.CTkEntry(self.frame_buttons1, width=50)
self.iterations.grid(row=4, column=1, padx=10, pady=5)
self.iterationsText = ct.CTkLabel(self.frame_buttons1, text ="Max iters.", padx = 5, pady = 5)
self.iterationsText.grid(row=4, column=0, padx=10, pady=5)
self.decompressButton = ct.CTkButton(self.frame_buttons1, text="Decompress", command=self.decompress)
self.decompressButton.grid(row=5, column=0, padx=10, pady=5, columnspan=2)
self.correctButton = ct.CTkButton(self.frame_buttons1, text="Correct XML", command=self.correct_xml)
self.correctButton.grid(row=6, column=0, padx=10, pady=5, columnspan=2)
self.showErrorsButton = ct.CTkButton(self.frame_buttons1, text="Show XML Errors",command=self.show_xml_errors)
self.showErrorsButton.grid(row=7, column=0, padx=10, pady=5, columnspan=2)
self.frame_buttons2 = ct.CTkFrame(self.frame_of_frames)
self.frame_buttons2.grid(row=0, column=1, padx=10, pady=5, sticky='n')
# Add a button to parse XML and build the social graph
self.buildGraphButton = ct.CTkButton(self.frame_buttons2, text="Build Social Graph", command=self.build_social_graph)
self.buildGraphButton.grid(row=0, column=0, padx=10, pady=5, columnspan=2)
# Add a button to visualize the social graph
self.visualizeGraphButton = ct.CTkButton(self.frame_buttons2, text="Visualize Social Graph", command=self.visualize_social_graph)
self.visualizeGraphButton.grid(row=1, column=0, padx=10, pady=5, columnspan=2)
# Add a button to search for posts by topic
self.searchPostsButton = ct.CTkButton(self.frame_buttons2, text="Search Posts by Topic", command=self.search_posts_by_topic)
self.searchPostsButton.grid(row=2, column=0, padx=10, pady=5, columnspan=2)
# Add an entry widget for the topic
self.topicEntry = ct.CTkEntry(self.frame_buttons2, width=100)
self.topicEntry.grid(row=3, column=0, padx=10, pady=5, columnspan=2)
# Add a button to print the network analysis report
self.networkAnalysisButton = ct.CTkButton(self.frame_buttons2, text="Network Analysis", command=self.show_network_analysis)
self.networkAnalysisButton.grid(row=4, column=0, padx=10, pady=5, columnspan=2)
# Add entry widgets for user IDs
self.user1Entry = ct.CTkEntry(self.frame_buttons2, width=50)
self.user1Entry.grid(row=5, column=1, padx=10, pady=5)
self.user1ID = ct.CTkLabel(self.frame_buttons2, text ="User1 ID:", padx = 5, pady = 5)
self.user1ID.grid(row=5, column=0, padx=10, pady=5)
self.user2Entry = ct.CTkEntry(self.frame_buttons2, width=50)
self.user2Entry.grid(row=6, column=1, padx=10, pady=5)
self.user2ID = ct.CTkLabel(self.frame_buttons2, text ="User2 ID:", padx = 5, pady = 5)
self.user2ID.grid(row=6, column=0, padx=10, pady=5)
# Attributes used to build and maintain the social graph
self.content_that_built_the_graph = None
self.social_graph = None
self.undo_button = ct.CTkButton(self.frame_buttons2, width=50, text="Undo", command=self.undo)
self.undo_button.grid(row=7, column=0, padx=5, pady=5)
self.redo_button = ct.CTkButton(self.frame_buttons2, width=50, text="Redo", command=self.redo)
self.redo_button.grid(row=7, column=1, padx=5, pady=5)
#make third column -> output textbox and save file
self.output_text_box = ct.CTkTextbox(self.root, height=400, width=270, state='disabled', wrap='none')
self.output_text_box.grid(row=0, column=3, padx=10, pady=10, sticky='n')
self.saveFileButton = ct.CTkButton(self.root, width=270, text="Save File", command=self.choose_output_file)
self.saveFileButton.grid(row=1, column=3, padx=10, pady=10)
self.last_function_performed_output_extension = ".txt"
self.makeResponsive()
#main event loop
self.root.mainloop()
def makeResponsive (self):
# Configure row and column weights for responsiveness
self.root.grid_rowconfigure(0, weight=1)
self.root.grid_rowconfigure(1, weight=1)
self.root.grid_columnconfigure(0, weight=1)
self.root.grid_columnconfigure(1, weight=1)
self.root.grid_columnconfigure(2, weight=1)
# These still need buttons
def undo(self):
try:
self.editor_text_box.edit_undo()
except:
pass
def redo(self):
try:
self.editor_text_box.edit_redo()
except:
pass
def copy_inout(self):
content = self.output_text_box.get(1.0, tk.END)
self.editor_text_box.delete(1.0, tk.END)
self.editor_text_box.insert(tk.END, content)
def copy_outin(self):
content = self.editor_text_box.get(1.0, tk.END)
self.show_output(content)
self.last_function_performed_output_extension = ".xml"
def chooseInputFile (self):
file_path = filedialog.askopenfilename(
title="Open File",
filetypes=(("xml files", "*.xml"), ("text files", "*.txt"), ("All Files", "*.*")))
if file_path:
# Do something with the selected file path (e.g., display it in a label)
# self.inputFileNameLabel.configure(text=f"{os.path.basename(file_path)}")
self.inputPath = file_path
else:
self.inputPath = ""
#display in text box
if file_path:
with open(file_path, 'r') as file:
content = file.read()
self.editor_text_box.delete(1.0, tk.END) # Clear existing content
self.editor_text_box.insert(tk.END, content)
def choose_output_file(self):
file_path = filedialog.asksaveasfilename(
title="Save File",
defaultextension=self.last_function_performed_output_extension,
filetypes=[("All files", "*.*")])
if file_path:
with open(file_path, 'w') as file:
text_content = self.output_text_box.get(1.0, tk.END)
file.write(text_content)
def compress(self):
text_content = self.output_text_box.get(1.0, tk.END)
self.show_output("Compressing file, this might\ntake a moment..")
file_path = filedialog.asksaveasfilename(
title="Compress File",
defaultextension=".xip",
filetypes=[("All files", "*.xip")])
if file_path:
bpe.compress(text_content, file_path[:len(file_path)-4], self.iterations.get())
self.show_output("Compression done!")
def decompress(self):
self.show_output("Decompressing file, this might\ntake a moment..")
file_path = filedialog.askopenfilename(
title="Decompress File",
filetypes=[("All files", "*.xip")])
if file_path:
content = bpe.decompress(file_path)
self.show_output(content)
self.last_function_performed_output_extension = ".xml"
def minify(self):
content = self.editor_text_box.get(1.0, tk.END)
content = minify(content)
self.show_output(content)
self.last_function_performed_output_extension = ".xml"
def xml2json(self):
content = self.editor_text_box.get(1.0, tk.END)
content = xml2json(content)
self.show_output(content)
self.last_function_performed_output_extension = ".json"
def prettify(self):
content = self.editor_text_box.get(1.0, tk.END)
content = prettify(content)
self.show_output(content)
self.last_function_performed_output_extension = ".xml"
def correct_xml(self):
content = self.editor_text_box.get(1.0, tk.END)
try:
content = correct_xml(content)
self.show_output(content)
self.last_function_performed_output_extension = ".xml"
except:
self.show_error("Input is not a syntax-error-free XML file.")
def show_xml_errors(self):
content=self.editor_text_box.get(1.0,tk.END)
content = XML_error_detector(content)
self.show_output(content)
self.last_function_performed_output_extension = ".txt"
def build_social_graph(self):
xml_content = self.editor_text_box.get(1.0, tk.END)
try:
self.social_graph = build_graph_netowrk_from_xml(xml_content)
self.show_output("Social graph built successfully.")
self.content_that_built_the_graph = xml_content
except:
self.show_error("The entered file is not a valid\nsocial graph representation. Try again\nwith a different file.")
def visualize_social_graph(self):
xml_content = self.editor_text_box.get(1.0, tk.END)
if (xml_content == self.content_that_built_the_graph):
try:
self.social_graph.visualize_graph()
except:
self.show_error("The file must be correct XML.")
else:
self.show_error("Please build the social graph first.")
def search_posts_by_topic(self):
# Get the topic from the entry widget
topic = self.topicEntry.get()
xml_content = self.editor_text_box.get(1.0, tk.END)
if topic:
if (xml_content == self.content_that_built_the_graph):
posts_by_topic = self.social_graph.search_posts_by_topic(topic)
self.output_text_box.configure(state='normal')
self.output_text_box.delete(1.0, tk.END)
for post in posts_by_topic:
self.output_text_box.insert(tk.END, post + '\n')
if (len(posts_by_topic) == 0):
self.output_text_box.insert(tk.END, "No posts with this topic were found.")
self.output_text_box.configure(state='disabled')
self.last_function_performed_output_extension = ".txt"
else:
self.show_error("Please build the social graph first.")
else:
self.show_error("Please enter a topic to search for.")
def show_network_analysis(self):
xml_content = self.editor_text_box.get(1.0, tk.END)
if (xml_content != self.content_that_built_the_graph):
self.show_error("Please build the social graph first.")
else:
try:
# Get user-specified parameters
user1_id = self.user1Entry.get()
user2_id = self.user2Entry.get()
topic = self.topicEntry.get()
# Perform network analysis with user-specified parameters
result = self.social_graph.print_network_analysis(user1_id, user2_id, topic)
# Display the result in the output text box
self.show_result("Network Analysis", result)
self.last_function_performed_output_extension = ".txt"
except:
self.show_error("The file must be correct XML.")
def show_result(self, title, result):
self.output_text_box.configure(state='normal')
self.output_text_box.delete(1.0, tk.END)
self.output_text_box.insert(tk.END, f"{title}:\n")
if result:
for item in result:
self.output_text_box.insert(tk.END, f"{item}\n")
else:
self.output_text_box.insert(tk.END, "No result found.")
self.output_text_box.configure(state='disabled')
# The two functions below are almost identical, but the different names are for clarity
def show_error(self, message):
# Show error messages in the output text box
self.output_text_box.configure(state='normal')
self.output_text_box.delete(1.0, tk.END)
self.output_text_box.insert(tk.END, message)
self.output_text_box.configure(state='disabled')
self.last_function_performed_output_extension = ".txt"
def show_output(self, message):
self.output_text_box.configure(state='normal')
self.output_text_box.delete(1.0, tk.END)
self.output_text_box.insert(tk.END, message)
self.output_text_box.configure(state='disabled')
#start app
Ui()