comfy_fm24_newgens/lib/generate_xml.py

63 lines
2.2 KiB
Python
Raw Normal View History

2024-12-13 12:06:05 +00:00
import os
import xml.etree.ElementTree as ET
def create_config_xml(folder_path):
# Define the root element
root = ET.Element("record")
# Add resource manager options
ET.SubElement(root, "boolean", id="preload", value="false")
ET.SubElement(root, "boolean", id="amap", value="false")
# Add the maps section
maps = ET.SubElement(root, "list", id="maps")
# Iterate through files in the folder
for filename in os.listdir(folder_path):
if filename.endswith(".png"):
# Extract the UID from the filename (remove the extension)
uid = os.path.splitext(filename)[0]
# Create a record for each file
2024-12-13 12:45:38 +00:00
ET.SubElement(maps, "record", attrib={"from": uid, "to": f"graphics/pictures/person/r-{uid}/portrait"})
2024-12-13 12:06:05 +00:00
# Create the XML tree
tree = ET.ElementTree(root)
# Save the XML file
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}")
2024-12-14 22:53:07 +00:00
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 <list id="maps"> element
maps = root.find(".//list[@id='maps']")
if maps is None:
raise ValueError("The XML file does not contain a <list id='maps'> 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}")