WebUI: Implement double-click behavior controls
[qBittorrent.git] / src / webui / www / split_merge_json.py
blob17cd12539e1b77da9f8c36ebcd87250a4bc772d8
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
4 # split_merge_json.py - script for splitting the JSON files from
5 # Transifex into their public/private parts and to merging the
6 # private/public en.json files to form the source file for Transifex
8 # Copyright (C) 2024 sledgehammer999 <hammered999@gmail.com>
10 # License: Public domain.
12 import argparse
13 import glob
14 import json
15 import os
16 from pathlib import PurePath
17 import sys
20 def updateJson(json_file: str, source: dict[str, str]) -> None:
21 trimmed_path: str = json_file
22 path_parts = PurePath(json_file).parts
23 if len(path_parts) >= 3:
24 trimmed_path = f"{path_parts[-3]}/{path_parts[-2]}/{path_parts[-1]}"
26 if not os.path.exists(json_file):
27 print(f"\tWARNIG: {trimmed_path} doesn't exist")
28 return
30 print(f"\tUpdating {trimmed_path}")
31 with open(json_file, mode="r+", encoding='utf-8') as file:
32 target: dict[str, str] = json.load(file)
33 if not isinstance(target, dict):
34 print(f"\tWARNIG: {trimmed_path} is malormed")
35 return
37 for key in target.keys():
38 value = source.get(key)
39 if value:
40 target[key] = value
41 file.seek(0)
42 json.dump(target, file, ensure_ascii=False, indent=2, sort_keys=True)
43 file.write("\n")
44 file.truncate()
47 def splitJson(transifex_dir: str, json_public_dir: str, json_private_dir: str) -> None:
48 locales: list[str] = glob.glob("*.json", root_dir=transifex_dir)
49 locales = [x for x in locales if x != "en.json"]
50 for locale in locales:
51 print(f"Processing lang {locale}")
52 transifex_file: str = f"{transifex_dir}/{locale}"
53 public_file: str = f"{json_public_dir}/{locale}"
54 private_file: str = f"{json_private_dir}/{locale}"
56 transifex_json: dict[str, str] = {}
57 with open(transifex_file, mode="r", encoding='utf-8') as file:
58 transifex_json = json.load(file)
59 if not isinstance(transifex_json, dict):
60 trimmed_path: str = transifex_file
61 path_parts = PurePath(transifex_file).parts
62 if len(path_parts) >= 2:
63 trimmed_path = f"{path_parts[-2]}/{path_parts[-1]}"
64 print(f"\tERROR: {trimmed_path} is malformed.")
65 continue
67 updateJson(public_file, transifex_json)
68 updateJson(private_file, transifex_json)
71 def mergeJson(transifex_dir: str, json_public_dir: str, json_private_dir: str) -> None:
72 transifex_en_file: str = f"{transifex_dir}/en.json"
73 public_en_file: str = f"{json_public_dir}/en.json"
74 private_en_file: str = f"{json_private_dir}/en.json"
76 transifex_en_json: dict[str, str] = {}
77 public_en_json: dict[str, str] = {}
78 private_en_json: dict[str, str] = {}
80 print("Merging source en.json file")
81 if os.path.exists(public_en_file):
82 with open(public_en_file, mode="r", encoding='utf-8') as file:
83 public_en_json = json.load(file)
84 if not isinstance(public_en_json, dict):
85 public_en_json = {}
87 if os.path.exists(private_en_file):
88 with open(private_en_file, mode="r", encoding='utf-8') as file:
89 private_en_json = json.load(file)
90 if not isinstance(private_en_json, dict):
91 private_en_json = {}
93 transifex_en_json = public_en_json | private_en_json
94 with open(transifex_en_file, mode="w", encoding='utf-8') as file:
95 json.dump(transifex_en_json, file, ensure_ascii=False, indent=2, sort_keys=True)
96 file.write("\n")
99 def main() -> int:
100 script_path: str = os.path.dirname(os.path.realpath(__file__))
101 transifex_dir: str = f"{script_path}/transifex"
102 json_public_dir: str = f"{script_path}/public/lang"
103 json_private_dir: str = f"{script_path}/private/lang"
105 parser = argparse.ArgumentParser()
106 parser.add_argument("--mode", required=True, choices=["split", "merge"], help="SPLIT the translations files from Transifex into their public/private JSON counterpart. MERGE to merge the public and private en.json files into the 'transifex/en.json' file")
107 parser.add_argument("--transifex-dir", default=transifex_dir, nargs=1, help=f"directory of WebUI transifex file (default: '{transifex_dir}')")
108 parser.add_argument("--json-public-dir", default=json_public_dir, nargs=1, help=f"directory of WebUI public JSON translation files(default: '{json_public_dir}')")
109 parser.add_argument("--json-private-dir", default=json_private_dir, nargs=1, help=f"directory of WebUI private JSON translation files(default: '{json_private_dir}')")
111 args = parser.parse_args()
113 if not os.path.isdir(args.transifex_dir):
114 raise RuntimeError(f"'{args.transifex_dir}' isn't a directory")
116 if not os.path.isdir(args.json_public_dir):
117 raise RuntimeError(f"'{args.json_public_dir}' isn't a directory")
119 if not os.path.isdir(args.json_private_dir):
120 raise RuntimeError(f"'{args.json_private_dir}' isn't a directory")
122 if args.mode == "merge":
123 mergeJson(transifex_dir, json_public_dir, json_private_dir)
124 else:
125 splitJson(transifex_dir, json_public_dir, json_private_dir)
127 print("Done.")
129 return 0
132 if __name__ == '__main__':
133 sys.exit(main())