From b165722c58d265a1d1ebd20b2c2097f80ac7f1a6 Mon Sep 17 00:00:00 2001 From: Midgard Date: Mon, 9 Dec 2024 03:34:17 +0100 Subject: [PATCH] Add purging script --- README | 12 +++++++++++- purge.py | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 1 deletion(-) create mode 100755 purge.py diff --git a/README b/README index a2db790..4314c21 100644 --- a/README +++ b/README @@ -1,2 +1,12 @@ -Do something like +Find and purge unused strings +============================= + +Dependencies +------------ +- python +- ripgrep (only for find.py) + +Usage +----- ./find.py ~/dev/mapcomplete > found.txt +./purge.py ~/dev/mapcomplete found.txt diff --git a/purge.py b/purge.py new file mode 100755 index 0000000..b04bf94 --- /dev/null +++ b/purge.py @@ -0,0 +1,55 @@ +#!/usr/bin/env python3 + +import os +import sys +import json + + + +if len(sys.argv) != 3 or sys.argv[1].startswith("-"): + print(f"Usage: {sys.argv[0]} ", file=sys.stderr) + sys.exit(1) + +path_mc_repo = sys.argv[1] +path_found_txt = sys.argv[2] + +if not os.path.isdir(path_mc_repo): + print("First argument (mapcomplete repo) is not an existing directory", file=sys.stderr) + sys.exit(1) + +if not os.path.isfile(path_found_txt): + print("Second argument (found.txt) is not an existing file", file=sys.stderr) + sys.exit(1) + + +with open(path_found_txt, "r", encoding="utf-8") as fh: + to_discard = set(map(str.strip, fh)) + + +def walk_and_discard(obj, prefix): + result = {} + for k, v in obj.items(): + fqkey = f"{prefix}{k}" + if isinstance(v, str): + if fqkey not in to_discard: + result[k] = v + elif hasattr(v, "items"): + result[k] = walk_and_discard(v, f"{prefix}{k}.") + return result + + +path_langs = os.path.join(path_mc_repo, "langs") +json_files = [ + f for f in os.listdir(path_langs) + if os.path.isfile(os.path.join(path_langs, f)) and f.endswith(".json") +] +for file in json_files: + path = os.path.join(path_langs, file) + print(f"Processing {path}", file=sys.stderr) + with open(path, "r", encoding="utf-8") as fh: + obj = json.load(fh) + + new_obj = walk_and_discard(obj, "") + + with open(path, "w", encoding="utf-8") as fh: + json.dump(new_obj, fh, indent=4, ensure_ascii=False)