feat(v2.3.0): add JSON flattening, fix file handle leaks, update README

This commit is contained in:
robert 2026-01-07 22:30:21 +01:00
parent 5d838bc27e
commit c34e2d663c
3 changed files with 106 additions and 19 deletions

View file

@ -94,10 +94,10 @@ class PNGMetadataEditor:
top_frame = ttk.Frame(self.root, padding="10")
top_frame.pack(fill=tk.X)
ttk.Button(top_frame, text="📄 Open PNG File", command=self.open_file).pack(side=tk.LEFT, padx=5)
ttk.Button(top_frame, text="📥 Browse Directory", command=self.browse_directory).pack(side=tk.LEFT, padx=5)
ttk.Button(top_frame, text=" Save Changes", command=self.save_changes).pack(side=tk.LEFT, padx=5)
ttk.Button(top_frame, text=" About", command=self.show_about).pack(side=tk.RIGHT, padx=5)
ttk.Button(top_frame, text="🌌 Open PNG File", command=self.open_file).pack(side=tk.LEFT, padx=5)
ttk.Button(top_frame, text="🗂️ Browse Directory", command=self.browse_directory).pack(side=tk.LEFT, padx=5)
ttk.Button(top_frame, text="💾 Save Changes", command=self.save_changes).pack(side=tk.LEFT, padx=5)
ttk.Button(top_frame, text="🍺 About", command=self.show_about).pack(side=tk.RIGHT, padx=5)
self.file_label = ttk.Label(top_frame, text="No file loaded", foreground="gray")
self.file_label.pack(side=tk.LEFT, padx=20)
@ -191,10 +191,10 @@ class PNGMetadataEditor:
button_frame = ttk.Frame(editor_frame, padding="10")
button_frame.pack(fill=tk.X)
ttk.Button(button_frame, text="🤌 Add Field", command=self.add_field).pack(side=tk.LEFT, padx=5)
ttk.Button(button_frame, text="✍️ Add Field", command=self.add_field).pack(side=tk.LEFT, padx=5)
ttk.Button(button_frame, text="✏️ Edit Field", command=self.edit_entry).pack(side=tk.LEFT, padx=5)
ttk.Button(button_frame, text="❌ Delete Field", command=self.delete_field).pack(side=tk.LEFT, padx=5)
ttk.Button(button_frame, text="✌️ Copy Value", command=self.copy_value).pack(side=tk.LEFT, padx=5)
ttk.Button(button_frame, text="📋 Copy Value", command=self.copy_value).pack(side=tk.LEFT, padx=5)
# Status bar at the bottom
status_frame = ttk.Frame(self.root, relief=tk.SUNKEN)
@ -421,6 +421,8 @@ class PNGMetadataEditor:
if hasattr(img, 'text'):
self.metadata_dict = dict(img.text)
img.close() # Release file handle
self.file_label.config(text=Path(filepath).name, foreground="")
self.mark_as_saved()
self.refresh_tree()
@ -494,6 +496,15 @@ class PNGMetadataEditor:
except (json.JSONDecodeError, TypeError):
return value
def flatten_json_if_valid(self, value):
"""If value is valid JSON, return it flattened (no indent), otherwise return as-is"""
try:
parsed = json.loads(value)
return json.dumps(parsed, ensure_ascii=False, separators=(',', ':'))
except (json.JSONDecodeError, TypeError):
return value
def truncate_value(self, value, max_length=80):
"""Truncate long values for tree display"""
value_str = str(value)
@ -583,7 +594,9 @@ class PNGMetadataEditor:
messagebox.showwarning("Invalid Input", "Key cannot be empty")
return
self.metadata_dict[key] = value
# Flatten JSON if valid to keep original format
flattened_value = self.flatten_json_if_valid(value)
self.metadata_dict[key] = flattened_value
self.mark_as_modified()
self.refresh_tree()
self.set_status(f"Added field '{key}'", color="green")
@ -637,7 +650,9 @@ class PNGMetadataEditor:
def save_edit():
new_value = value_text.get(1.0, tk.END).strip()
self.metadata_dict[key] = new_value
# Flatten JSON if valid to keep original format
flattened_value = self.flatten_json_if_valid(new_value)
self.metadata_dict[key] = flattened_value
self.mark_as_modified()
self.refresh_tree()
self.set_status(f"Updated field '{key}'", color="green")