diff options
author | David Wührer <def@gmx.at> | 2022-11-03 19:23:18 +0100 |
---|---|---|
committer | David Wührer <def@gmx.at> | 2022-11-03 19:23:18 +0100 |
commit | be555f29f47f81993c12bf76065a0cb6bb0fee96 (patch) | |
tree | eb5991d8b0309609746fe612068faeadf0c52e0b /xmlbrowser | |
parent | e60829f7b9c61dc870173a8dd8f2f85c17517ec4 (diff) |
Add a few old things.
Diffstat (limited to 'xmlbrowser')
-rwxr-xr-x | xmlbrowser | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/xmlbrowser b/xmlbrowser new file mode 100755 index 0000000..731b466 --- /dev/null +++ b/xmlbrowser @@ -0,0 +1,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() |