-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxmltools.py
35 lines (28 loc) · 1.12 KB
/
xmltools.py
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
from lxml import etree
def parse_xml(file_path):
"""Parse XML definition for type."""
tree = etree.parse(file_path)
root = tree.getroot()
return root
def populate_tree(tree_widget, xml_root):
for element in xml_root.findall('.//{http://definition.nexusformat.org/nxdl/3.1}group'):
item = QTreeWidgetItem(tree_widget)
item.setText(0, element.attrib.get("type", "Unnamed"))
doc = element.find('{http://definition.nexusformat.org/nxdl/3.1}doc')
if doc is not None:
item.setToolTip(0, doc.text.strip())
import json
def update_json(editor, selected_items):
output = {}
for item in selected_items:
output[item.text(0)] = {"details": item.toolTip(0)}
editor.setPlainText(json.dumps(output, indent=4))
self.tree.itemSelectionChanged.connect(
lambda: update_json(self.json_editor, self.tree.selectedItems())
)
from PyQt5.QtWidgets import QFileDialog
def save_json(editor):
path, _ = QFileDialog.getSaveFileName(None, "Save JSON File", "", "JSON Files (*.json)")
if path:
with open(path, "w") as file:
file.write(editor.toPlainText())