Compare commits

...
2 Commits
Author SHA1 Message Date
Esa Kataja e3e2bcb260 Rename xml parser functions and remove unused dependencies 2025-04-07 16:37:41 +03:00
Esa Kataja d602cbb897 Move sanitize_name to lib.py 2025-04-07 16:36:54 +03:00
3 changed files with 14 additions and 34 deletions
+9
View File
@@ -0,0 +1,9 @@
def sanitize_name(name: str) -> str:
"""
Sanitize a name by removing all non-letter characters except hyphens and Scandinavian letters.
"""
# Replace Scandinavian letters
name = name.replace('ä', 'a').replace('ö', 'o')
name = name.replace('Ä', 'A').replace('Ö', 'O')
# Remove all non-letter characters except hyphens
return ''.join(c for c in name if c.isalpha() or c == '-')
+1 -1
View File
@@ -1,6 +1,6 @@
from pathlib import Path from pathlib import Path
from xml_parser import sanitize_name from lib import sanitize_name
from models.savefile import SaveFile from models.savefile import SaveFile
get_all_code = """ get_all_code = """
+3 -32
View File
@@ -1,15 +1,15 @@
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
def write_xml_data(savefile: SaveFile) -> None: def write_xml_data(savefile: SaveFile) -> None:
filename = Path("output/CodeServerConnector.xml") filename = Path("output/CodeServerConnector.xml")
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)
@@ -17,16 +17,6 @@ def write_xml_data(savefile: SaveFile) -> None:
with open(filename, "w", encoding="utf-8-sig") as f: with open(filename, "w", encoding="utf-8-sig") as f:
f.write(xml_str) f.write(xml_str)
def sanitize_name(name: str) -> str:
"""
Sanitize a name by removing all non-letter characters except hyphens and Scandinavian letters.
"""
# Replace Scandinavian letters
name = name.replace('ä', 'a').replace('ö', 'o')
name = name.replace('Ä', 'A').replace('Ö', 'O')
# Remove all non-letter characters except hyphens
return ''.join(c for c in name if c.isalpha() or c == '-')
def add_connection(root: ET.Element, name: str, attr_list: list[str], description: str = "Default description") -> ET.Element: def add_connection(root: ET.Element, name: str, attr_list: list[str], description: str = "Default description") -> ET.Element:
""" """
Add a new connection parameter to the XML configuration. Add a new connection parameter to the XML configuration.
@@ -81,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.
@@ -105,22 +95,3 @@ def _generate_xml_configuration() -> ET.Element:
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}")