Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CleanJSON.py: add missing keys from the source file, and reformat with black #407

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
109 changes: 75 additions & 34 deletions CleanJSON.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,47 @@

import json


class LocaleCleaner:
def __init__(self, en, lang, out):
en_file = open(en, 'r', encoding="utf8")
lang_file = open(lang, 'r', encoding="utf8")
self.out = out
self.en = en_file.readlines()
self.lang = json.load(lang_file)
self.output = []

def __init__(self, en: str, lang: str, out: str, add_missing_keys: bool = False):
with open(en, "r", encoding="utf8") as en_file:
self.en: list = en_file.readlines()
with open(lang, "r", encoding="utf8") as lang_file:
self.lang: dict = json.load(lang_file)
self.out: str = out
self.output: list = []
self.add_missing_keys: bool = add_missing_keys

def run(self):
self.make_header()
self.parse()
self.make_footer()
self.save()

def make_header(self):
self.output.append('{')
"""
{
"localeCode": "en",
"authors": ["author1", "author2", ],
"messages": {
"""
self.output.append("{")
self.output.append(' "localeCode": "{}",'.format(self.lang["localeCode"]))
self.output.append(' "authors": ["{}"],'.format('", "'.join(self.lang["authors"])))
self.output.append(
' "authors": ["{}"],'.format('", "'.join(self.lang["authors"]))
)
self.output.append(' "messages": {')

def make_footer(self):
"""
"Dummy": "Dummy"
}
}
"""
self.output.append(' "Dummy": "Dummy"')
self.output.append(' }')
self.output.append('}')
self.output.append('')
self.output.append(" }")
self.output.append("}")
self.output.append("")

def find_start(self):
counter = 0
Expand All @@ -36,40 +51,66 @@ def find_start(self):
if "message" in line:
break
return counter

def parse(self):
start_pos = self.find_start()
blank = False
for line in self.en[start_pos:]:
if '"Dummy": "Dummy"' in line:
break
key = line.strip().split(':')[0].strip().replace('"','')
kv = line.strip().split(":", 1)
key = kv[0].strip().replace('"', "")
if key in self.lang["messages"]:
blank = False
translation = self.lang["messages"][key].replace('\n','\\n').replace('"','\\"')
translation = (
self.lang["messages"][key].replace("\n", "\\n").replace('"', '\\"')
)
self.output.append(' "{}": "{}",'.format(key, translation))
elif self.add_missing_keys and key != "":
blank = False
value = kv[1].strip().rstrip(",")[1:-1]
self.output.append(' "#{}": "{}",'.format(key, value))
elif blank == False:
self.output.append('')
self.output.append("")
blank = True

def save(self):
out = open(self.out, 'w', encoding="utf8")
out.write('\n'.join(self.output))
out.close()
with open(self.out, "w", encoding="utf8") as out:
out.write("\n".join(self.output))


if __name__ == "__main__":
import argparse

parser = argparse.ArgumentParser(description='This script will reformat a Babel style JSON for locales to match the en.json baseline formatting for git changes purposes.')
parser.add_argument('--en', metavar='en_path', type=str,
help='The path to the en.json locale.')
parser.add_argument('--lang', metavar='lang_path', type=str,
help='The path to the LANG.json locale to clean.')
parser.add_argument('--out', metavar='out_path', type=str,
help='The path to save the formatted file.')

parser = argparse.ArgumentParser(
description="This script will reformat a Babel style JSON for locales to match the en.json baseline formatting for git changes purposes."
)
parser.add_argument(
"--en", metavar="en_path", type=str, help="The path to the en.json locale."
)
parser.add_argument(
"--lang",
metavar="lang_path",
type=str,
help="The path to the LANG.json locale to clean.",
)
parser.add_argument(
"--out",
metavar="out_path",
type=str,
help="The path to save the formatted file.",
)
parser.add_argument(
"-a",
"--add-missing-keys",
action="store_true",
help="If a key is missing in the translation, copy original text from en.json to the output file. The key will be prefixed with #.",
)

args = parser.parse_args()
N = LocaleCleaner(args.en, args.lang, args.out)
if args.lang is None or args.en is None or args.out is None:
parser.print_help()
exit(1)
N = LocaleCleaner(args.en, args.lang, args.out, args.add_missing_keys)
N.run()
print("Cleaned!")