Skip to content

Commit

Permalink
CleanJSON.py: reformat with black
Browse files Browse the repository at this point in the history
  • Loading branch information
medicalwei committed Jun 25, 2024
1 parent 1c362cd commit b038bec
Showing 1 changed file with 51 additions and 30 deletions.
81 changes: 51 additions & 30 deletions CleanJSON.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,36 @@

import json


class LocaleCleaner:
def __init__(self, en, lang, out, add_missing_keys=False):
en_file = open(en, 'r', encoding="utf8")
lang_file = open(lang, 'r', encoding="utf8")
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 = []
self.add_missing_keys=add_missing_keys
self.add_missing_keys = add_missing_keys

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

def make_header(self):
self.output.append('{')
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):
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 @@ -37,44 +40,62 @@ 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
kv = line.strip().split(':', 1)
key = kv[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 = open(self.out, "w", encoding="utf8")
out.write("\n".join(self.output))
out.close()



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.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.')

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.",
)

args = parser.parse_args()
N = LocaleCleaner(args.en, args.lang, args.out, args.add_missing_keys)
Expand Down

0 comments on commit b038bec

Please sign in to comment.