digitales-heizhaus/mapcleanup.py
2021-12-26 16:31:15 +01:00

59 lines
1.8 KiB
Python
Executable file

#!/usr/bin/env python3
import argparse
import sys
import xml.etree.ElementTree as ET
def main():
parser = argparse.ArgumentParser(description="Cleanup rc3.world maps")
parser.add_argument("map", help="map in .tmx format")
parser.add_argument("-o", "--output", help="where to write modified map")
parser.add_argument(
"-i",
"--in-place",
help="replace map with modified version",
action="store_true",
)
parser.add_argument(
"--fix-copyright",
help="add mapCopyright/tilesetCopyright properties",
action="store_true",
)
args = parser.parse_args()
if not args.output and not args.in_place:
print("Output unspecified.", file=sys.stderr)
sys.exit(1)
tree = ET.parse(args.map)
map = tree.getroot()
if args.fix_copyright:
fix_copyright(map)
tree.write(args.output or args.map, xml_declaration=False)
def fix_copyright(map):
if map.find("properties/property[@name='mapCopyright']") is None:
license = input("Map license? [CC0] ") or "CC0"
properties = map.find("properties")
if properties is None:
properties = ET.SubElement(map, "properties")
ET.SubElement(properties, "property", name="mapCopyright", value=license)
for tileset in map.findall("tileset"):
if tileset.find("properties/property[@name='tilesetCopyright']") is None:
license = (
input("Tileset '%s' license? [CC0] " % tileset.attrib["name"]) or "CC0"
)
properties = tileset.find("properties")
if properties is None:
properties = ET.SubElement(tileset, "properties")
ET.SubElement(
properties, "property", name="tilesetCopyright", value=license
)
if __name__ == "__main__":
main()