Compare commits
2
Commits
6baf0f12ab
...
e3e2bcb260
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e3e2bcb260 | ||
|
|
d602cbb897 |
@@ -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
@@ -1,6 +1,6 @@
|
||||
from pathlib import Path
|
||||
|
||||
from xml_parser import sanitize_name
|
||||
from lib import sanitize_name
|
||||
from models.savefile import SaveFile
|
||||
|
||||
get_all_code = """
|
||||
|
||||
+3
-32
@@ -1,15 +1,15 @@
|
||||
import lxml.etree as ET
|
||||
from pathlib import Path
|
||||
import click
|
||||
|
||||
from models.savefile import SaveFile
|
||||
from lib import sanitize_name
|
||||
|
||||
def write_xml_data(savefile: SaveFile) -> None:
|
||||
filename = Path("output/CodeServerConnector.xml")
|
||||
if not filename.parent.exists():
|
||||
filename.parent.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
root = _generate_xml_configuration()
|
||||
root = _generate_xml_root()
|
||||
for code in savefile.conceptCodes:
|
||||
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:
|
||||
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:
|
||||
"""
|
||||
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")
|
||||
return root
|
||||
|
||||
def _generate_xml_configuration() -> ET.Element:
|
||||
def _generate_xml_root() -> ET.Element:
|
||||
"""
|
||||
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(root, "Schema")
|
||||
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}")
|
||||
Reference in New Issue
Block a user