duckstation

duckstation, but archived from the revision just before upstream changed it to a proprietary software project, this version is the libre one
git clone https://git.neptards.moe/u3shit/duckstation.git
Log | Files | Refs | README | LICENSE

merge_compatibility_list.py (2132B)


      1 from copy import deepcopy
      2 import sys
      3 import argparse
      4 import xml.etree.ElementTree as ET
      5 from xml.dom import minidom
      6 
      7 # https://pymotw.com/2/xml/etree/ElementTree/create.html
      8 def prettify(elem):
      9     """Return a pretty-printed XML string for the Element.
     10     """
     11     rough_string = ET.tostring(elem, 'utf-8')
     12     reparsed = minidom.parseString(rough_string)
     13     dom_string = reparsed.toprettyxml(encoding="utf-8",indent="  ")
     14     return b'\n'.join([s for s in dom_string.splitlines() if s.strip()])
     15 
     16 
     17 # https://stackoverflow.com/questions/25338817/sorting-xml-in-python-etree/25339725#25339725
     18 def sortchildrenby(parent, attr):
     19     parent[:] = sorted(parent, key=lambda child: child.get(attr))
     20 
     21 
     22 def add_entries_from_file(filename, new_tree, overwrite_existing = False):
     23     tree = ET.parse(filename)
     24     for child in tree.getroot():
     25         if (child.tag != "entry"):
     26             print("!!! Skipping invalid tag '%s'" % child.tag)
     27             continue
     28 
     29         game_code = child.get("code")
     30         existing_node = new_tree.getroot().find(".//*[@code='%s']" % game_code)
     31         if existing_node is not None:
     32             if overwrite_existing:
     33                 print("*** Replacing %s from new list" % game_code)
     34                 new_tree.getroot().remove(existing_node)
     35             else:
     36                 print("*** Skipping %s from new list" % game_code)
     37                 continue
     38 
     39         new_tree.getroot().append(deepcopy(child))
     40 
     41 
     42 if __name__ == "__main__":
     43     parser = argparse.ArgumentParser()
     44     parser.add_argument("--overwrite", action="store_true")
     45     parser.add_argument("existing_list", action="store")
     46     parser.add_argument("list_to_merge", action="store")
     47     parser.add_argument("output_list", action="store")
     48     args = parser.parse_args()
     49 
     50     new_tree = ET.ElementTree(ET.Element("compatibility-list"))
     51     add_entries_from_file(args.existing_list, new_tree, False)
     52     add_entries_from_file(args.list_to_merge, new_tree, args.overwrite)
     53 
     54     sortchildrenby(new_tree.getroot(), "title")
     55 
     56     output_file = open(args.output_list, "wb")
     57     output_file.write(prettify(new_tree.getroot()))
     58     output_file.close()
     59