Skip to content

Commit ea6daa2

Browse files
committed
Avoid to load wrong files
1 parent 6d26b57 commit ea6daa2

1 file changed

Lines changed: 23 additions & 24 deletions

File tree

main.py

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -79,25 +79,30 @@ def populate_tree(tree: ttk.Treeview, data: dict|list):
7979
elif type(data) == list:
8080
count = addListToTree(tree, "", "root", data, count)
8181

82-
def loadTreeviewFromBjson(root, tree: ttk.Treeview, fp: str|Path):
82+
def loadFileDataFromBjson(root, tree: ttk.Treeview, fp: str|Path):
8383
loading_label = tkinter.Label(root, text="Loading file...")
8484
loading_label.grid(row=0, column=0)
85-
json_str = getBjsonContent(fp)
86-
bjson_dict = json.loads(json_str)
87-
88-
populate_tree(tree, bjson_dict)
89-
if not hasattr(tree, 'icons'):
90-
tree.icons = {}
91-
tree.icons["objectLogo"] = tkinter.PhotoImage(file=os.path.join(root.app_path, "assets/object.png"))
92-
tree.icons["arrayLogo"] = tkinter.PhotoImage(file=os.path.join(root.app_path, "assets/array.png"))
93-
tree.icons["textLogo"] = tkinter.PhotoImage(file=os.path.join(root.app_path, "assets/text.png"))
94-
tree.icons["numberLogo"] = tkinter.PhotoImage(file=os.path.join(root.app_path, "assets/number.png"))
95-
tree.icons["booleanLogo"] = tkinter.PhotoImage(file=os.path.join(root.app_path, "assets/boolean.png"))
96-
tree.icons["nullLogo"] = tkinter.PhotoImage(file=os.path.join(root.app_path, "assets/null.png"))
97-
setIcons(tree, tree.icons)
85+
try:
86+
json_str = getBjsonContent(fp)
87+
bjson_dict = json.loads(json_str)
88+
89+
populate_tree(tree, bjson_dict)
90+
if not hasattr(tree, 'icons'):
91+
tree.icons = {}
92+
tree.icons["objectLogo"] = tkinter.PhotoImage(file=os.path.join(root.app_path, "assets/object.png"))
93+
tree.icons["arrayLogo"] = tkinter.PhotoImage(file=os.path.join(root.app_path, "assets/array.png"))
94+
tree.icons["textLogo"] = tkinter.PhotoImage(file=os.path.join(root.app_path, "assets/text.png"))
95+
tree.icons["numberLogo"] = tkinter.PhotoImage(file=os.path.join(root.app_path, "assets/number.png"))
96+
tree.icons["booleanLogo"] = tkinter.PhotoImage(file=os.path.join(root.app_path, "assets/boolean.png"))
97+
tree.icons["nullLogo"] = tkinter.PhotoImage(file=os.path.join(root.app_path, "assets/null.png"))
98+
setIcons(tree, tree.icons)
99+
tree.grid(row=0, column=0, sticky="wesn")
100+
inputPath = Path(fp)
101+
root.title(f"MC3DS BJSON Editor - {inputPath.name}")
102+
except:
103+
pass
98104

99105
loading_label.grid_remove()
100-
tree.grid(row=0, column=0, sticky="wesn")
101106

102107
def setIcons(tree: ttk.Treeview, icons: dict, parent=""):
103108
children = tree.get_children(parent)
@@ -195,9 +200,7 @@ def __init__(self, fp = None):
195200
self.actualIndex = None
196201

197202
if fp != None:
198-
threading.Thread(target=partial(loadTreeviewFromBjson, self, self.tree, fp)).start()
199-
inputPath = Path(fp)
200-
self.title(f"MC3DS BJSON Editor - {inputPath.name}")
203+
threading.Thread(target=partial(loadFileDataFromBjson, self, self.tree, fp)).start()
201204

202205
self.saved = True
203206

@@ -260,12 +263,10 @@ def saveChanges(self):
260263
f.write(bytearray(fileContent))
261264
self.clearTreeview()
262265
self.tree.grid_remove()
263-
outPath = Path(outputPath)
264-
threading.Thread(target=partial(loadTreeviewFromBjson, self, self.tree, outputPath)).start()
266+
threading.Thread(target=partial(loadFileDataFromBjson, self, self.tree, outputPath)).start()
265267
self.valueEntry.configure(state="readonly")
266268
self.valueStringVar.set("")
267269
self.dataTypeStringVar.set("")
268-
self.title(f"MC3DS BJSON Editor - {outPath.name}")
269270
self.lastValue = None
270271
self.changes = []
271272
self.actualIndex = None
@@ -275,16 +276,14 @@ def saveChanges(self):
275276
def openFile(self):
276277
if self.askForChanges():
277278
inputFp = tkinter.filedialog.askopenfilename(filetypes=[("BJSON Files", ".bjson")])
278-
inputPath = Path(inputFp)
279279
if inputFp != "":
280280
self.clearTreeview()
281281
self.tree.grid_remove()
282-
threading.Thread(target=partial(loadTreeviewFromBjson, self, self.tree, inputFp)).start()
282+
threading.Thread(target=partial(loadFileDataFromBjson, self, self.tree, inputFp)).start()
283283
self.valueEntry.configure(state="readonly")
284284
self.valueStringVar.set("")
285285
self.dataTypeStringVar.set("")
286286
self.saveButton.configure(state="disabled")
287-
self.title(f"MC3DS BJSON Editor - {inputPath.name}")
288287
self.lastValue = None
289288
self.changes = []
290289
self.actualIndex = None

0 commit comments

Comments
 (0)