Skip to content

Commit 4864512

Browse files
committed
Added source and destination buttons. "Rename selected file" doesn't work yet.
1 parent cfdf634 commit 4864512

File tree

4 files changed

+95
-6
lines changed

4 files changed

+95
-6
lines changed

flatc_deserializer_frontend.py

Lines changed: 89 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"""
44
# pylint: disable=import-error, too-many-instance-attributes, too-many-statements
55
import os
6+
import tkinter.filedialog
67
from collections.abc import Callable
78
from importlib import import_module
89
from tkinter import ttk
@@ -60,10 +61,27 @@ def __init__(self):
6061
self.src_binaries_table.heading("file", text=t("frontend.source_file_loc"))
6162
self.src_binaries_table.heading("file_size", text=t("frontend.file_size"))
6263
self.src_binaries_frame.grid_rowconfigure(0, weight=1)
64+
self.src_binaries_frame.grid_rowconfigure(1, weight=1)
6365
self.src_binaries_frame.grid_columnconfigure(0, weight=1)
66+
self.src_binaries_frame.grid_columnconfigure(1, weight=1)
67+
self.src_binaries_frame.grid_columnconfigure(2, weight=1)
6468
self.src_binaries_frame.grid_propagate(False)
65-
self.src_binaries_table.grid(row=0, column=0, padx=10, pady=10, sticky=CTk.NSEW)
69+
self.src_binaries_table.grid(row=0, column=0, columnspan=3, padx=10, pady=10,
70+
sticky=CTk.NSEW)
6671
self.attempt_apply_dnd(self.src_binaries_table.winfo_id(), self.on_binary_dropped)
72+
self.src_binaries_add_btn = CTk.CTkButton(self.src_binaries_frame,
73+
text=t("frontend.button_add"))
74+
self.src_binaries_add_btn.configure(command=self.on_binary_add_click)
75+
self.src_binaries_add_btn.grid(row=1, column=0, padx=10, pady=10, sticky=CTk.EW)
76+
self.src_binaries_remove_selected_btn = CTk.CTkButton(self.src_binaries_frame, text=t(
77+
"frontend.button_remove_selected"))
78+
self.src_binaries_remove_selected_btn.grid(row=1, column=1, padx=10, pady=10, sticky=CTk.EW)
79+
self.src_binaries_remove_selected_btn.configure(
80+
command=self.on_binary_remove_selected_click)
81+
self.src_binaries_remove_all = CTk.CTkButton(self.src_binaries_frame,
82+
text=t("frontend.button_remove_all"))
83+
self.src_binaries_remove_all.grid(row=1, column=2, padx=10, pady=10, sticky=CTk.EW)
84+
self.src_binaries_remove_all.configure(command=self.on_binary_remove_all_click)
6785
self.src_schemas_frame = ttk.LabelFrame(self.src_files_frame,
6886
text=t("frontend.source_schemas"))
6987
self.src_schemas_frame.grid(row=1, column=0, padx=10, pady=10, sticky=CTk.NSEW)
@@ -84,14 +102,25 @@ def __init__(self):
84102
self.dest_binaries_frame.grid(row=0, column=0, padx=10, pady=10, sticky=CTk.NSEW)
85103
self.dest_binaries_table = ttk.Treeview(self.dest_binaries_frame,
86104
columns=("file", "result", "file_size"),
87-
show="headings")
105+
show="headings", selectmode="browse")
88106
self.dest_binaries_table.heading("file", text=t("frontend.destination_file_loc"))
89107
self.dest_binaries_table.heading("result", text=t("frontend.result"))
90108
self.dest_binaries_table.heading("file_size", text=t("frontend.file_size"))
91109
self.dest_binaries_frame.grid_rowconfigure(0, weight=1)
110+
self.dest_binaries_frame.grid_rowconfigure(1, weight=1)
92111
self.dest_binaries_frame.grid_columnconfigure(0, weight=1)
112+
self.dest_binaries_frame.grid_columnconfigure(1, weight=1)
93113
self.dest_binaries_frame.grid_propagate(False)
94-
self.dest_binaries_table.grid(row=0, column=0, padx=10, pady=10, sticky=CTk.NSEW)
114+
self.dest_binaries_table.grid(row=0, column=0, columnspan=2, padx=10, pady=10,
115+
sticky=CTk.NSEW)
116+
self.attempt_apply_dnd(self.dest_binaries_table.winfo_id(), self.on_binary_dropped)
117+
self.dest_binaries_change_dest_btn = CTk.CTkButton(self.dest_binaries_frame,
118+
text=t("frontend.button_change_dest"))
119+
self.dest_binaries_change_dest_btn.grid(row=1, column=0, padx=10, pady=10, sticky=CTk.EW)
120+
self.dest_binaries_change_dest_btn.configure(command=self.on_change_dest_click)
121+
self.dest_binaries_rename_btn = CTk.CTkButton(self.dest_binaries_frame,
122+
text=t("frontend.button_rename_file"))
123+
self.dest_binaries_rename_btn.grid(row=1, column=1, padx=10, pady=10, sticky=CTk.EW)
95124
self.dest_options_frame = ttk.LabelFrame(self.dest_files_frame,
96125
text=t("frontend.destination_options"))
97126
self.dest_options_frame.grid_propagate(False)
@@ -127,6 +156,60 @@ def flatc_button_pressed():
127156
"""
128157
execute_download(os.getcwd())
129158

159+
def on_binary_add_click(self):
160+
"""
161+
Triggered when "Add..." button is clicked.
162+
"""
163+
binary_paths = tkinter.filedialog.askopenfilenames(title=t("main.tkinter_binaries_select"))
164+
if binary_paths is not None:
165+
self.on_binary_dropped(binary_paths)
166+
167+
def on_binary_remove_selected_click(self):
168+
"""
169+
Triggered when "Remove selected" button is clicked.
170+
"""
171+
selected_items = self.src_binaries_table.selection()
172+
for selected_item in selected_items:
173+
self.src_binaries_table.delete(selected_item)
174+
self.src_binaries_table.update()
175+
self.dest_binaries_table.delete(selected_item)
176+
self.dest_binaries_table.update()
177+
178+
def on_binary_remove_all_click(self):
179+
"""
180+
Triggered when "Remove all" button is clicked.
181+
:return:
182+
"""
183+
for item in self.dest_binaries_table.get_children():
184+
self.src_binaries_table.delete(item)
185+
self.src_binaries_table.update()
186+
self.dest_binaries_table.delete(item)
187+
self.dest_binaries_table.update()
188+
189+
def on_change_dest_click(self):
190+
"""
191+
Triggered when "Change destination directory..." button is clicked.
192+
:return:
193+
"""
194+
selected_items = self.dest_binaries_table.selection()
195+
for selected_item in selected_items:
196+
file_path = self.dest_binaries_table.set(selected_item, 0)
197+
file_dir, file_name = os.path.split(file_path)
198+
dest_dir = tkinter.filedialog.askdirectory(title=t("main.tkinter_output_select"),
199+
initialdir=file_dir)
200+
if os.path.isdir(dest_dir):
201+
new_file_path = os.path.abspath(os.path.join(dest_dir, file_name))
202+
self.dest_binaries_table.set(selected_item, 0, new_file_path)
203+
if os.path.isfile(new_file_path):
204+
self.dest_binaries_table.set(selected_item, 1,
205+
t("frontend.file_already_exists"))
206+
self.dest_binaries_table.set(selected_item, 2,
207+
t("frontend.size_kb") % (
208+
os.path.getsize(new_file_path) / 1024))
209+
else:
210+
self.dest_binaries_table.set(selected_item, 1, "")
211+
self.dest_binaries_table.update()
212+
130213
def on_binary_dropped(self, paths: list[str]):
131214
"""
132215
Triggered when binary files or directories are added to table.
@@ -233,6 +316,9 @@ def deserialize_and_update_table(self, flatc_path: str, schema_path: str, binary
233316
self.dest_binaries_table.set(i, 1, t("frontend.result_done"))
234317
self.dest_binaries_table.set(i, 2, t("frontend.size_kb") % (
235318
os.path.getsize(json_path) / 1024))
319+
elif not os.path.isfile(binary_path):
320+
self.dest_binaries_table.set(i, 1, t("frontend.binary_not_found"))
321+
self.dest_binaries_table.set(i, 2, "")
236322
elif not os.path.isfile(schema_path):
237323
self.dest_binaries_table.set(i, 1, t("frontend.schema_not_found"))
238324
self.dest_binaries_table.set(i, 2, "")

flatc_funcs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def deserialize(flatc_path: str, schema_path: str, binary_path: str, output_path
2828
binary_path = os.path.abspath(binary_path)
2929
binary_name = os.path.splitext(os.path.basename(binary_path))[0]
3030
output_path = os.path.abspath(output_path)
31-
json_path = os.path.abspath(output_path + os.sep + binary_name + ".json")
31+
json_path = os.path.abspath(os.path.join(output_path, binary_name + ".json"))
3232
previous_json_contents = None
3333
try:
3434
with open(json_path, "rb") as json_file:

localization/en_US/frontend.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,7 @@ flatc_not_found: Flatbuffers Schema Compiler (flatc) not found.
1919
error: Error
2020
result_done: Done
2121
result_error: Error
22-
schema_not_found: Schema not found
22+
result_binary_not_found: Binary not found
23+
schema_not_found: Schema not found
24+
button_change_dest: Change destination directory
25+
button_rename_file: Rename selected file

localization/en_US/main.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ tkinter_fbs_select: Select schema file
44
fbs_filetype: Flatbuffers schema
55
tkinter_binaries_select: Select binary files
66
flatc_binary_filetype: Binary Flatbuffers file
7-
tkinter_output_select: Select output directory
7+
tkinter_output_select: Select destination directory
88
files: Files
99
unsupported_platform: %s platform is not supported.
1010
executable_not_found: Executable %s not found.

0 commit comments

Comments
 (0)