From 92530d497990ac1be7ecaa39211f2d79c15e2ef8 Mon Sep 17 00:00:00 2001 From: OMGeeky Date: Thu, 12 Sep 2024 19:37:25 +0200 Subject: [PATCH] comment out testing code --- .../remove_duplicate_tileset_usages.py | 35 +- .../test_remove_duplicate_tileset_usages.py | 490 ++++++++++++++++++ 2 files changed, 524 insertions(+), 1 deletion(-) create mode 100644 at_remove_duplicate_tilesets/test_remove_duplicate_tileset_usages.py diff --git a/at_remove_duplicate_tilesets/remove_duplicate_tileset_usages.py b/at_remove_duplicate_tilesets/remove_duplicate_tileset_usages.py index b77ed79..fb3c7b8 100644 --- a/at_remove_duplicate_tilesets/remove_duplicate_tileset_usages.py +++ b/at_remove_duplicate_tilesets/remove_duplicate_tileset_usages.py @@ -5,7 +5,9 @@ import xml.etree.ElementTree import zlib import gzip import struct +from re import RegexFlag from typing import Any +import re def get_min_gid_after(after: int, tilesets: list[tuple[Any, int]]): @@ -14,6 +16,33 @@ def get_min_gid_after(after: int, tilesets: list[tuple[Any, int]]): ) +def remove_tilesets( + filepath: str, duplicates: list[tuple[int, int, int, str, int, int]] +): + print(f"writing file: {filepath}") + with open(filepath, "r", encoding="utf-8") as f: + content = f.read() + for duplicate in duplicates: + content = remove_tileset_with_regex(content, duplicate[3], duplicate[0]) + + with open(filepath, "w", encoding="utf-8") as f: + f.write(content) + pass + + +def remove_tileset_with_regex(content, tileset_name, tileset_first_gid): + first_gid = tileset_first_gid + name = tileset_name + modified = re.sub( + f' <[^<]*firstgid="{first_gid}"[^<]*({name})[\\S\\s]*?\\n', + "", + content, + 1, + RegexFlag.MULTILINE, + ) + return modified + + def remove_duplicate_usages(xml_file_path: str): print(f"checking {xml_file_path}") tree = xml.etree.ElementTree.parse(xml_file_path) @@ -27,6 +56,7 @@ def remove_duplicate_usages(xml_file_path: str): found_duplicate = len(duplicates) > 0 if not found_duplicate: + print("no duplicates found") return # basically a map where the first two numbers are the range, that needs to be @@ -78,8 +108,11 @@ def remove_duplicate_usages(xml_file_path: str): modified = original.replace(data, modified_data_str) f.write(modified) + remove_tilesets(xml_file_path, duplicate_usages_maps) + print( - f"XML file has unused duplicate tilesets please remove them manually: '{xml_file_path}' " + "XML file had duplicate tilesets. They were removed automatically" + f", so please check if this worked: '{xml_file_path}' " ) diff --git a/at_remove_duplicate_tilesets/test_remove_duplicate_tileset_usages.py b/at_remove_duplicate_tilesets/test_remove_duplicate_tileset_usages.py new file mode 100644 index 0000000..21ede32 --- /dev/null +++ b/at_remove_duplicate_tilesets/test_remove_duplicate_tileset_usages.py @@ -0,0 +1,490 @@ +from unittest import TestCase + +from remove_duplicate_tileset_usages import remove_tileset_with_regex + +sample_input = """ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + eJzbyMnAsJFGeD8TA8MBIF6HhNdCaVrZCcIXgeZfQrILhPfTwV4QfsKE6u+LUHdQYuZPbgaGX0D8m5s0f+/HYu9PJHNwsWGYk4eBgQuIuXmI8/daJD+jy0sCzZDmwc9GViuFRZyYdEbr+MUV3vS0Fzm86W0vCI/aO2ovLTAoHwMAiyotWg== + + + + eJylVE1LA0EMDd4WVJpZWLAXpf1l6l78OPYn9KRe9acInhcKgvS4tuJNaD3U/+CE3bBv0pltxQfDTF6bSV4ym80J0Y9fG7MLLJfaBUeO6NiFfoglE9Xc2fvEqeAs/n3xYzEVFfB3w3jM84zowq/LLIyPPPo9nhE9+VW2vHBfB/H4Nr97n8NDm8c71GTFoc/M2ILDotknWViTudGOuaAexZh3n2OYtnoHuY8/IDrNu1jYX6zRf1FzWL+XIt5vyUN78mF0rFv71fD1Dr1lth0r9da0NraPCJuX5eQ9SJ8mWcctjH7s7XPRrGUR3jnyPs41/cxdaDPYI5PPtEcvvltBCTlW0Ju/7Aq8+9vn9MZhbJujAHunWlQrG+0MvwsW4Ls2NYjNF3lXqXmzr9YVhzNN8RmZbTaXOXxPfTNLYGeW5VIx+uapnSGp/+r5yvfyerh9943nbiO84hcEGsAg + + + eJy7I8nAcBeIQeAOlI1Ob0Zi41OHLIcNbEYSr5LCbhYyQLYfHcDkShUYGMoUCNuNrg8EqoFuqJHCr54Y8AbJ/fvw2I/PP7QC6GFEKUAPY1xhTi37yLGbWgBfuqMHwOVffOmW0rSFy2x6+hvdTmS7QemK2PIGH0Av0/DZiSy+Dyk/4dNPiv/w2Y1LzWYs8tjYkcC4jJLCNDsaKBaDRRwZ4NKLrQxFLmNgbFxlMz7zAPGWgcM= + + + + eJzbKMbAsHEUDwgmFmDTQw934RJDZxNjHjFsYvxLrP+pBYgNe+TwoJaduNxBjD/R3USIxmYPPrvR9VLqd1z+ISWcSfUrIbcQm55JAbjijp55mdS8RE8MAAqL1Dg= + + + + + + + + + + + + + + + +""" +expected_output = """ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + eJzbyMnAsJFGeD8TA8MBIF6HhNdCaVrZCcIXgeZfQrILhPfTwV4QfsKE6u+LUHdQYuZPbgaGX0D8m5s0f+/HYu9PJHNwsWGYk4eBgQuIuXmI8/daJD+jy0sCzZDmwc9GViuFRZyYdEbr+MUV3vS0Fzm86W0vCI/aO2ovLTAoHwMAiyotWg== + + + + eJylVE1LA0EMDd4WVJpZWLAXpf1l6l78OPYn9KRe9acInhcKgvS4tuJNaD3U/+CE3bBv0pltxQfDTF6bSV4ym80J0Y9fG7MLLJfaBUeO6NiFfoglE9Xc2fvEqeAs/n3xYzEVFfB3w3jM84zowq/LLIyPPPo9nhE9+VW2vHBfB/H4Nr97n8NDm8c71GTFoc/M2ILDotknWViTudGOuaAexZh3n2OYtnoHuY8/IDrNu1jYX6zRf1FzWL+XIt5vyUN78mF0rFv71fD1Dr1lth0r9da0NraPCJuX5eQ9SJ8mWcctjH7s7XPRrGUR3jnyPs41/cxdaDPYI5PPtEcvvltBCTlW0Ju/7Aq8+9vn9MZhbJujAHunWlQrG+0MvwsW4Ls2NYjNF3lXqXmzr9YVhzNN8RmZbTaXOXxPfTNLYGeW5VIx+uapnSGp/+r5yvfyerh9943nbiO84hcEGsAg + + + eJy7I8nAcBeIQeAOlI1Ob0Zi41OHLIcNbEYSr5LCbhYyQLYfHcDkShUYGMoUCNuNrg8EqoFuqJHCr54Y8AbJ/fvw2I/PP7QC6GFEKUAPY1xhTi37yLGbWgBfuqMHwOVffOmW0rSFy2x6+hvdTmS7QemK2PIGH0Av0/DZiSy+Dyk/4dNPiv/w2Y1LzWYs8tjYkcC4jJLCNDsaKBaDRRwZ4NKLrQxFLmNgbFxlMz7zAPGWgcM= + + + + eJzbKMbAsHEUDwgmFmDTQw934RJDZxNjHjFsYvxLrP+pBYgNe+TwoJaduNxBjD/R3USIxmYPPrvR9VLqd1z+ISWcSfUrIbcQm55JAbjijp55mdS8RE8MAAqL1Dg= + + + + + + + + + + + + + + + +""" + + +class Test(TestCase): + + def test_remove_tileset_with_regex(self): + """ + should remove this tileset: + + + + """ + response = remove_tileset_with_regex(sample_input, "map_tree_1", 8181) + self.assertEqual( + response, expected_output, "removed stuff was not what we expected" + )