diff --git a/lib/generate_xml.py b/lib/generate_xml.py index 8493c97..8b21135 100644 --- a/lib/generate_xml.py +++ b/lib/generate_xml.py @@ -28,3 +28,36 @@ def create_config_xml(folder_path): output_path = os.path.join(folder_path, "config.xml") tree.write(output_path, encoding="utf-8", xml_declaration=True) print(f"Config XML created at: {output_path}") + + +def append_to_config_xml(file_path, uid_list): + # Check if the file exists + file_path = f"{file_path}/config.xml" + if not os.path.exists(file_path): + raise FileNotFoundError(f"The file {file_path} does not exist.") + + # Parse the existing XML file + tree = ET.parse(file_path) + root = tree.getroot() + + # Find the element + maps = root.find(".//list[@id='maps']") + if maps is None: + raise ValueError("The XML file does not contain a element.") + + # Add new UIDs + for uid in uid_list: + # Check if the UID already exists (escaping 'from' in XPath) + existing = None + for record in maps.findall("record"): + if record.attrib.get('from') == str(uid): + existing = record + break + + if existing is None: + # Add the new record if the UID doesn't exist + ET.SubElement(maps, "record", attrib={"from": str(uid), "to": f"graphics/pictures/person/r-{uid}/portrait"}) + + # Write the updated XML back to the file + tree.write(file_path, encoding="utf-8", xml_declaration=True) + print(f"Appended new UIDs to {file_path}") \ No newline at end of file diff --git a/lib/xml_reader.py b/lib/xml_reader.py new file mode 100644 index 0000000..9e130cc --- /dev/null +++ b/lib/xml_reader.py @@ -0,0 +1,25 @@ +import xml.etree.ElementTree as ET + +def extract_from_values(xml_file): + """ + Extract all 'from' values from the record elements in the XML file. + + Args: + xml_file (str): Path to the XML file. + + Returns: + list: List of all 'from' attribute values. + """ + from_values = [] + + # Parse the XML file + tree = ET.parse(xml_file) + root = tree.getroot() + + # Find all 'record' elements and extract 'from' attributes + for record in root.findall(".//record"): + from_value = record.get('from') + if from_value: + from_values.append(from_value) + + return from_values \ No newline at end of file