Rename xml parser functions and remove unused dependencies

This commit is contained in:
Esa Kataja
2025-04-07 16:37:41 +03:00
parent d602cbb897
commit e3e2bcb260
+3 -23
View File
@@ -1,6 +1,5 @@
import lxml.etree as ET import lxml.etree as ET
from pathlib import Path from pathlib import Path
import click
from models.savefile import SaveFile from models.savefile import SaveFile
from lib import sanitize_name from lib import sanitize_name
@@ -10,7 +9,7 @@ def write_xml_data(savefile: SaveFile) -> None:
if not filename.parent.exists(): if not filename.parent.exists():
filename.parent.mkdir(parents=True, exist_ok=True) filename.parent.mkdir(parents=True, exist_ok=True)
root = _generate_xml_configuration() root = _generate_xml_root()
for code in savefile.conceptCodes: for code in savefile.conceptCodes:
root = add_connection(root, code.name, code.attributes) root = add_connection(root, code.name, code.attributes)
@@ -72,7 +71,7 @@ def add_connection(root: ET.Element, name: str, attr_list: list[str], descriptio
ET.SubElement(item, "SetParameter", Param="RootProperty", Source="FixedValue", Value="conceptCodes") ET.SubElement(item, "SetParameter", Param="RootProperty", Source="FixedValue", Value="conceptCodes")
return root return root
def _generate_xml_configuration() -> ET.Element: def _generate_xml_root() -> ET.Element:
""" """
Generate a default XML configuration for a PowerShell connector. Generate a default XML configuration for a PowerShell connector.
@@ -95,23 +94,4 @@ def _generate_xml_configuration() -> ET.Element:
ET.SubElement(command_sequence, "Item", Command="Initialize-CodeServer", Order="1") ET.SubElement(command_sequence, "Item", Command="Initialize-CodeServer", Order="1")
ET.SubElement(environment_initialization, "Disconnect") ET.SubElement(environment_initialization, "Disconnect")
ET.SubElement(root, "Schema") ET.SubElement(root, "Schema")
return root return root
def get_xml_data(xml_file: Path) -> ET.Element:
"""
Get XML data from a file. If the file does not exist, generate a default configuration.
Args:
xml_file (Path): The path to the XML file.
Returns:
ET.Element: The root element of the XML configuration.
"""
if not xml_file.exists():
return _generate_xml_configuration()
try:
parser = ET.XMLParser(strip_cdata=False)
tree = ET.parse(xml_file, parser=parser)
return tree
except ET.XMLSyntaxError as e:
raise click.FileError(xml_file, f"Not a valid XML file. {e}")