Skip to content
Snippets Groups Projects
Commit 762694f6 authored by Kristina Mazur's avatar Kristina Mazur
Browse files

Improves the output

parent 2dee08bc
No related branches found
No related tags found
No related merge requests found
......@@ -51,7 +51,7 @@ from pathlib import Path
# === Configuration ===
# Define the format of the output
FORMAT = {
"Header": "# {:s}\n> **Description**: {:s}\n",
"Header": "\n## {:s}\n> **Description**: {:s}\n",
"Unit": "> **Unit**: {:s}\n",
"TableHeader": "| Relative XML Path | Unit | Description |\n|:---|:---:|:---|",
"Table": "| <nobr>`{:s}` | *{:s}* | {:s} |",
......@@ -92,43 +92,28 @@ class Page:
self.sections = [[]]
def create(self, node: ET.Element):
"""Creates the page from the given node.
"""Creates the page from the given node, summarizing nested elements properly.
Args:
node (ET.Element): Node to start from.
"""
# Only add the header if it was not already added at this level
if node.tag in self.sections[self.current_level]:
return
# Check whether the current entry can be a header
# Add header if appropriate
if not self.max_level_reached:
self.sections[self.current_level].append(node.tag)
self.make_header_entry(node)
self.current_path = Path(node.tag)
else:
self.current_path /= node.tag
# Loop through the children and group single entries in a table
for child in node:
if len(child) == 0: # No further nesting
self.make_table_entry(child)
else:
self.create(child) # Recursively process child nodes
# Add a new level of sections
self.current_level += 1
self.sections.append([])
# Loop again and add the subnodes
for child in node:
if len(child) > 0:
# Treat node attributes and values as part of the parent row
if len(node) == 0: # Leaf node
self.make_table_entry(node)
else: # Parent node
# Add a summary row for the parent node
self.make_table_entry(node)
# Process child elements recursively, skipping common attributes like 'value', 'unit', etc.
for child in node:
if child.tag in ["value", "unit", "lower_boundary", "upper_boundary"]:
continue
self.create(child)
# Decrease the level again when finished
self.current_level -= 1
self.sections.pop()
self.current_path = self.current_path.parent
def make_header_entry(self, node: ET.Element):
"""Creates a header entry.
......@@ -155,7 +140,7 @@ class Page:
print(FORMAT["Unit"].format(unit))
def make_table_entry(self, node: ET.Element):
"""Creates a table entry.
"""Creates a table entry for the current node, embedding child attributes into the parent node's row.
Args:
node (ET.Element): The current node element.
......@@ -165,17 +150,25 @@ class Page:
self.table_started = True
print(FORMAT["TableHeader"])
# Since we use pathlib for convenience, we need to convert it to a string
# and replace the backslashes with forward slashes
# Generate a relative XML path for the current node
path_name = str(self.current_path / node.tag).replace("\\", "/")
# Fetch description and unit
# Fetch parent description and unit
description = node.attrib.get(KEYS["Description"], f"No description for {node.tag}")
unit = node.attrib.get(KEYS["Unit"], "No unit specified")
# Collect all child attributes
child_details = []
for child in node:
child_desc = child.attrib.get(KEYS["Description"], f"No description for {child.tag}")
child_unit = child.attrib.get(KEYS["Unit"], "No unit specified")
child_details.append(f"{child.tag}: {child_desc} (unit: {child_unit})")
# Combine parent description with child attributes
combined_description = description + " | " + " | ".join(child_details)
# Create the table row
print(FORMAT["Table"].format(path_name, unit, description))
# Print the table row for the parent node
print(FORMAT["Table"].format(path_name, unit, combined_description))
# === Main ===
def main():
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment