Warning: Can not open [/home/conf/public_html/cgi-bin/show_python.log]. Ignore No title

Download script from XMLView.py
Related files:


import os
import sys
import toml
import xml.etree.ElementTree as ET
import tkinter as tk
from tkinter import ttk


if len(sys.argv) <= 1:
    print("Usage: python xml_viewer.py infile window_size")
    sys.exit(1)

infile = '../XML/vasprun.xml'
geometry = '800x600'

inifile = os.path.splitext(sys.argv[0])[0] + ".ini"
if os.path.isfile(inifile):
    geometry = toml.load(inifile).get("geometry", geometry)

nargs = len(sys.argv)
if nargs >= 2:
    infile = sys.argv[1]
if nargs >= 3:
    geometry = sys.argv[2]


class XMLViewer:
    def __init__(self, root, infile, geometry = '800x600', tree_open = True):
        self.tree_open = tree_open

        self.root = root
        self.root.title(f"XML Viewer - {infile}")
        self.root.geometry(geometry)

        self.treeview = ttk.Treeview(self.root)
        self.treeview.heading('#0', text = infile)
        self.treeview.pack(fill = 'both', expand = True)

        self.read_xml(infile)

    def read_xml(self, infile):
        if not os.path.isfile(infile):
            print(f"\nError: Can not find [{infile}]")
            input("Press ENTER to terminate>>\n")
            exit()

        try:
            self.etree = ET.parse(infile)
            self.root_element = self.etree.getroot()
            self.display_element(self.root_element, '')
        except ET.ParseError as e:
            self.treeview.insert("", "end", text="Error", values = (str(e)))

    def display_element(self, element, item_id = '', level = 0):
        if level <= 1:
            tree_open = self.tree_open
        else:
            tree_open = False

        text = None
        if element.attrib.get('name', None) is not None:
            text = element.attrib["name"]
        if element.text is not None:
            if text is None:
                text = element.text.strip()
            else:
                text += f":{element.text.strip()}"
        if text is None or text == '':
            text = element.tag.strip()
        if text is None or text == '':
            text = 'root'

        child_item = self.treeview.insert(item_id, "end", text = text, open = tree_open)
        self.treeview.insert(child_item, "end", text = f"tag: {element.tag.strip()}", open = tree_open)
        if element.text is not None:
            self.treeview.insert(child_item, "end", text = f"text: {element.text.strip()}")
        self.treeview.insert(child_item, "end", text = f"attrib: {element.attrib}", open = tree_open)
        for key, value in element.attrib.items():
            self.treeview.insert(child_item, "end", text = f"{key}: {value}", open = tree_open)

        for child in element.findall('*'):
            self.display_element(child, child_item, level = level + 1)

def window_close(root):
    with open(inifile, "w") as file:
        toml.dump({"geometry": root.geometry()}, file)
    root.quit()

def main():
    root = tk.Tk()
    app = XMLViewer(root, infile, geometry, tree_open = True)
    root.protocol("WM_DELETE_WINDOW", lambda: window_close(root))

    root.mainloop()


if __name__ == "__main__":
    main()