Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 35 additions & 2 deletions JsonEditor.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,13 @@

"""

"""
Original author and repo
Abdullah Ahmad Zarir
https://github.com/zargit/tkinter-json-editor


"""
class ValueTypes:
DICT = 1
LIST = 2
Expand Down Expand Up @@ -56,6 +62,9 @@ def __init__(self, master, **options):

self.popup_menu_actions['add_child_filepath'] = {'text': 'Add Filepath',
'action': lambda: self.add_item_from_input(ValueTypes.FILEPATH)}
#add Duplicate here
self.popup_menu_actions['add_duplicate'] = {'text': 'Duplicate',
'action': lambda: self.duplicate_item_from_input(self.get_selected_index())}

self.popup_menu_actions['edit_child'] = {'text': 'Edit',
'action': lambda: self.edit_item_from_input()}
Expand Down Expand Up @@ -324,6 +333,30 @@ def edit_item_from_input(self):
if self.verify_value(value):
self.edit_item(selection, value=value)

def duplicate_item_from_input(self, index):
"""
:param index: Duplicate the item at index and its children from the list

"""
#print("duplicate item from input")
json_root = self.get_json_root(index)
if index == json_root:
for item in self.tree.get_children(index):
key_item = self.get_key(item)
value_item = self.get_value(item)
self.add_item(item, key_item, value_item)
else:
key_index = self.get_key(index)
value_index = self.get_value(index)
parent_index=self.tree.parent(index)
self.add_item(key_index, value_index, parent_index)


if self.tree.tag_has(Tags.FILE, json_root):
self.save_json_file(self.get_json_filepath(json_root), self.get_value(json_root))



def remove_item(self, index):
"""
:param index: Removes the item at index and its children from the list.
Expand All @@ -337,7 +370,7 @@ def remove_item_from_input(self, index):
"""

json_root = self.get_json_root(index)

print(json_root)
if index == json_root:
for item in self.tree.get_children(index):
self.remove_item(item)
Expand Down Expand Up @@ -392,7 +425,7 @@ def save_json_file(self, filepath, data):
:param data: A <dict> object.
"""
with open(filepath, 'w') as f:
json.dump(data, f)
json.dump(data, f, sort_keys=True, indent=4)

def get_selected_index(self):
"""
Expand Down