From 8076d48fcfa2d1434d0175005e470f687f5e58ac Mon Sep 17 00:00:00 2001 From: Damien Savatier Date: Tue, 13 Jun 2023 22:28:06 +0200 Subject: [PATCH] Initial commit --- .gitignore | 2 ++ merge.py | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ unzip.sh | 6 +++++ zip.sh | 5 ++++ 4 files changed, 91 insertions(+) create mode 100644 .gitignore create mode 100755 merge.py create mode 100755 unzip.sh create mode 100755 zip.sh diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9c5d19d --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*.json +*.bin diff --git a/merge.py b/merge.py new file mode 100755 index 0000000..7bdab33 --- /dev/null +++ b/merge.py @@ -0,0 +1,78 @@ +#!/usr/bin/env python3 + +import json +from argparse import ArgumentParser +from pathlib import Path + +parser = ArgumentParser() +parser.add_argument("source", type=Path, nargs="?", default="wordlist.json") +parser.add_argument( + "mix_with", type=Path, nargs="?", default="wordlist.en.json" +) +parser.add_argument( + "target", type=Path, nargs="?", default="wordlist.merged.json" +) +parser.add_argument("--overrides", type=Path, default="overrides.json") +parser.add_argument( + "-p", + "--patch", + type=str, + nargs="+", + action="append", + choices=["all", "songs", "modes", "folders", "default"], + default=[["default"]], +) +args = parser.parse_args() + +PREFIXES = { + "songs": ["song_"], + "folders": ["genre_", "folder_"], + "modes": ["mode_select_"], +} +PREFIXES["all"] = [""] +PREFIXES["default"] = PREFIXES["songs"] + PREFIXES["folders"] + + +def read_file(path: Path): + with path.open("r") as f: + return json.load(f) + + +def save_file(path: Path, json_data): + with path.open("w") as f: + json.dump(json_data, f, indent=2, ensure_ascii=False) + + +def matcher(key: str): + for patch in args.patch: + patch = patch[0] + for prefix in PREFIXES[patch]: + if key.startswith(prefix): + return True + return False + + +def read_as_dict(path: Path): + data = read_file(path) + if "items" not in data: + raise ValueError(f"Invalid file: {path}, missing 'items' key") + return {item["key"]: item for item in data["items"]} + + +mix_data = read_as_dict(args.mix_with) + +data = read_file(args.source) +if args.overrides.exists(): + overrides = {x["key"]: x for x in read_file(args.overrides)} +else: + overrides = {} +if "items" not in data: + raise ValueError(f"Invalid file: {data}, missing 'items' key") +for index, item in enumerate(data["items"]): + if item["key"] in overrides: + data["items"][index] = overrides[item["key"]] + continue + if item["key"] in mix_data and matcher(item["key"]): + data["items"][index] = mix_data[item["key"]] + +save_file(args.target, data) diff --git a/unzip.sh b/unzip.sh new file mode 100755 index 0000000..8227f71 --- /dev/null +++ b/unzip.sh @@ -0,0 +1,6 @@ +#!/usr/bin/env bash + +7za x wordlist.bin +mv wordlist wordlist.json +7za x wordlist.en.bin +mv wordlist wordlist.en.json \ No newline at end of file diff --git a/zip.sh b/zip.sh new file mode 100755 index 0000000..a7619fb --- /dev/null +++ b/zip.sh @@ -0,0 +1,5 @@ +#!/usr/bin/env bash + +mv wordlist.merged.json wordlist +gzip wordlist +mv wordlist.gz wordlist.merged.bin \ No newline at end of file