unused_strings/purge.py

55 lines
1.4 KiB
Python
Executable file

#!/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 fqkey not in to_discard:
if isinstance(v, str):
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/layers")
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)