Add purging script

This commit is contained in:
Midgard 2024-12-09 03:34:17 +01:00
parent b834d3ea2c
commit b165722c58
Signed by: midgard
GPG key ID: 511C112F1331BBB4
2 changed files with 66 additions and 1 deletions

12
README
View file

@ -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 ./find.py ~/dev/mapcomplete > found.txt
./purge.py ~/dev/mapcomplete found.txt

55
purge.py Executable file
View file

@ -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]} <path to mapcomplete repo> <path to found.txt>", 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)