summaryrefslogtreecommitdiff
path: root/xmlbrowser
blob: 731b466abb10bd2cddbe3d854066aecfdace6fe0 (plain)
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
36
37
38
39
#!/usr/bin/python3

import sys
import xml.etree.ElementTree as ET

from qtpy import QtGui, QtWidgets, QtCore
from qtpy.QtWidgets import QApplication, QTreeWidget, QTreeWidgetItem, QLabel

def _make_item(element) -> QTreeWidgetItem:
	r = QTreeWidgetItem([element.tag, None, element.text])
	r.addChildren([QTreeWidgetItem([None, name, value]) for name, value in element.attrib.items()])
	r.addChildren([_make_item(child) for child in element])
	return r

def main():
	app = QApplication(sys.argv)
	# TODO: Check for suitable arguments first.
	# Parameter may be a Qt option.
	if len(sys.argv) == 1:
		print("Usage:", sys.argv[0], "[file …]", file=sys.stderr)
		return

	items = []
	for arg in sys.argv[1:]:
		with open(arg) as xmlfile:
			xmltree = ET.parse(xmlfile)
		fileItem = QTreeWidgetItem([arg])
		items.append(fileItem)
		fileItem.addChildren([_make_item(xmltree.getroot())])

	tree = QTreeWidget()
	tree.setColumnCount(2)
	tree.setHeaderLabels(["Element", "attribute", "text"])
	tree.insertTopLevelItems(0, items)
	tree.show()
	sys.exit(app.exec_())	# Main loop.

if __name__ == '__main__':
	main()