-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_gui.pyw
More file actions
240 lines (203 loc) · 9.89 KB
/
main_gui.pyw
File metadata and controls
240 lines (203 loc) · 9.89 KB
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
import tkinter as tk
from tkinter import StringVar, ttk
from tkinter import filedialog
from tkinter import messagebox
from hash_valid_lib import HashFile
from hash_valid_lib import get_avail_hash_types
HASH_TYPES = get_avail_hash_types()
class MainApplication(tk.Frame):
def __init__(self, parent: tk.Tk, *args, **kwargs):
"""MainApplication contains the Tkinter Frame that forms the main GUI.
@parent: A Tkinter Tk root parent window for the frame to bind to.
"""
tk.Frame.__init__(self, parent, *args, **kwargs)
self.parent = parent
#####################
### Main Frames setup
frame_file = tk.Frame(master=parent)
frame_hashes = tk.Frame(master=parent, relief=tk.GROOVE, borderwidth=5)
frame_action = tk.Frame(master=parent)
# NOTE: Layout arranging
frame_file.pack(ipadx=5, ipady=5, fill=tk.X)
frame_hashes.pack(padx=5, pady=5, fill=tk.BOTH)
frame_action.pack(ipadx=5, ipady=5, fill=tk.X)
##############################
### File Selection Frame setup
label_file = tk.Label(
master=frame_file,
text="File Address",
foreground="black")
self.file_addr = tk.StringVar()
entry_file = tk.Entry(
master=frame_file,
width=50,
foreground="black",
background="white",
textvariable=self.file_addr)
button_file = tk.Button(master=frame_file,
text="...",
foreground="black",
command=lambda: self.tk_find_file(
entry_file,
self.file_addr))
# NOTE: The button's command uses a lambda to pass an argument. This can
# be done instead of partial becauses the argument is not changing here
# NOTE: Layout setup
label_file.pack(side=tk.LEFT, padx=3)
entry_file.pack(side=tk.LEFT, padx=3)
button_file.pack(side=tk.LEFT, padx=3)
#####################################
### Hash Calculation/Validation Frame
label_hash_header = tk.Label(
master=frame_hashes,
font="Verdana 16 bold",
foreground="black",
text="Expected Hashes")
label_hash_header.grid(row=0, column=0, columnspan=3)
# NOTE: To keep the code more flexible with supported hash types, a loop
# over the possible hash types is used along with a procedural
# generation of corresponding UI elements (in this case, the Hash Frame)
self.hash_frame = {}
for index, hash_type in enumerate(HASH_TYPES, 1):
textvar_entry_hash = StringVar()
textvar_label_hashresult = StringVar(value="Waiting")
label_hash = tk.Label(
master=frame_hashes,
foreground="black",
text=hash_type)
entry_hash = tk.Entry(
master=frame_hashes,
width=50,
foreground="black",
background="white",
textvariable=textvar_entry_hash)
label_hashresult = tk.Label(
master=frame_hashes,
foreground="black",
textvariable=textvar_label_hashresult)
# NOTE: Layout setup
label_hash.grid(row=index, column=0, sticky='e')
entry_hash.grid(row=index, column=1, sticky='w')
label_hashresult.grid(row=index, column=2)
self.hash_frame[hash_type] = {
"ui_entry": entry_hash,
"ui_result": label_hashresult,
"textvar_entry_hash": textvar_entry_hash,
"textvar_result": textvar_label_hashresult
}
########################
### Action Buttons Frame
self.textvar_status = StringVar(value="Waiting for file")
self.label_status = tk.Label(
master=frame_action,
font="Verdana 12 bold",
foreground="black",
textvariable=self.textvar_status)
self.progressbar_status = ttk.Progressbar(
master=frame_action,
orient=tk.HORIZONTAL,
length=300,
mode='indeterminate')
self.button_validate = tk.Button(
master=frame_action,
text="Validate",
command=self.validate_all_hashes,
foreground="white",
background="green")
self.label_status.pack()
self.progressbar_status.pack(fill=tk.X, pady=10)
self.button_validate.pack()
def tk_find_file(
self,
entry: tk.Entry,
textvar_entry: tk.StringVar) -> None:
"""Opens a Tkinter file-selection dialog and inserts a selected file's
directory address into the given Entry element.
@entry: The Tkinter Entry element containing the directory address.
@textvar_entry: The StringVar used to contain the Entry element text.
"""
file_addr = filedialog.askopenfilename(
title="Select target file...",
initialdir="./")
if file_addr:
textvar_entry.set(file_addr)
entry.xview(tk.END)
self.textvar_status.set("Ready")
def validate_all_hashes(self) -> None:
"""Checks the target file's hash against all given expected hashes.
"""
# NOTE: Calculation flow:
# 1. Checks whether a file directory address exists.
# 2. Iterates over each hash type where an expected hash is
# present
# 3. Calculates the relevant hash and compares it with the
# expected hash, updating the UI accordingly
if self.file_addr.get():
hashfile = HashFile(self.file_addr.get())
self.start_progress(True)
for hash_type in HASH_TYPES:
# NOTE: This may look confusing. In order to access both the UI
# elements on the application, I save the objects into a
# dictionary so that it can be picked up by self.
expected_hash = self.hash_frame[hash_type]["textvar_entry_hash"].get()
if expected_hash:
self.hash_frame[hash_type]["textvar_result"].set("Processing")
self.textvar_status.set("Processing "+hash_type)
self.parent.update()
hashstring = hashfile.hashCalculate(hash_type=hash_type)
if hashstring == expected_hash:
self.update_frame_hash_status(
hash_type,
"white",
"green",
"Valid")
else:
self.update_frame_hash_status(
hash_type,
"black",
"red",
"Invalid")
else:
self.update_frame_hash_status(
hash_type,
"black",
"white",
"Waiting")
self.start_progress(False)
else:
messagebox.showerror(
title="No target file!",
message="Please give a file directory address first.")
def start_progress(self, starting: bool):
if starting:
self.progressbar_status.start()
else:
self.textvar_status.set("Finished")
self.progressbar_status.stop()
def update_frame_hash_status(self,
hash_type: str,
foreground: str,
background: str,
status: str):
self.hash_frame[hash_type]["ui_entry"].configure(
foreground=foreground,
background=background)
self.hash_frame[hash_type]["textvar_result"].set(status)
self.parent.update()
def main():
"""CLI-based program.
"""
#############################
### Tkinter Root Window Setup
tk_root_window = tk.Tk()
tk_root_window.resizable(width=False, height=False)
tk_root_window.title("File-Hash Validation")
tk_root_window.eval("tk::PlaceWindow . center")
tk_root_window.iconbitmap("img/icon.ico")
###########
### GUI run
app = MainApplication(tk_root_window)
app.pack(fill=tk.BOTH)
tk_root_window.mainloop()
if __name__ == '__main__':
main()