From ccc5e3c395e63c7f1e31641ece23f446076fbc99 Mon Sep 17 00:00:00 2001 From: Damontecres <154766448+damontecres@users.noreply.github.com> Date: Sat, 10 Feb 2024 18:23:36 -0500 Subject: [PATCH] Add python black --- .pre-commit-config.yaml | 5 +++++ app/convert_strings.py | 22 +++++++++++++++------- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 3e55016f..6e956a21 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -10,3 +10,8 @@ repos: hooks: - id: pretty-format-kotlin args: [ --autofix ] +- repo: https://github.com/psf/black + rev: 23.12.1 + hooks: + - id: black + files: app/.*\.py diff --git a/app/convert_strings.py b/app/convert_strings.py index a4d9adbd..2585e9ac 100644 --- a/app/convert_strings.py +++ b/app/convert_strings.py @@ -7,17 +7,19 @@ DEBUG = "--debug" in sys.argv source_dir = Path("../stash-server/ui/v2.5/src/locales") -dest_prefix="src/main/res/values" +dest_prefix = "src/main/res/values" param_pattern = re.compile(r"{\w+}") + def escape_value(value: str): value = escape(value) - count=1 + count = 1 while param_pattern.search(value): value = param_pattern.sub(f"%{count}$s", value, 1) - count+=1 - return count>1, value.replace(r"'", r"\'") + count += 1 + return count > 1, value.replace(r"'", r"\'") + def parse_dict(d, keys=[]): for key, value in d.items(): @@ -27,6 +29,7 @@ def parse_dict(d, keys=[]): else: yield keys + [key], value + def convert_file(source_file, dest_file, allowed_keys): if DEBUG: print(f"Converting {source_file} to {dest_file}") @@ -35,7 +38,7 @@ def convert_file(source_file, dest_file, allowed_keys): data = json.load(f) dest_file.parent.mkdir(parents=True, exist_ok=True) with open(dest_file, "w") as f: - f.write("\n") + f.write('\n') f.write("\n") for key, value in parse_dict(data, ["stashapp"]): key = "_".join(key) @@ -45,10 +48,13 @@ def convert_file(source_file, dest_file, allowed_keys): continue collected_keys.append(key) formatted, value = escape_value(value) - f.write(f" {value}\n") + f.write( + f' {value}\n' + ) f.write("\n") return set(collected_keys) + main_file = source_dir / "en-GB.json" main_dest = Path(dest_prefix) / "stash_strings.xml" @@ -61,7 +67,9 @@ def convert_file(source_file, dest_file, allowed_keys): continue else: lang = file.name.replace(".json", "").replace("-", "+").replace("_", "+") - convert_file(file, Path(dest_prefix+"-b+"+lang) / "stash_strings.xml", allowed_keys) + convert_file( + file, Path(dest_prefix + "-b+" + lang) / "stash_strings.xml", allowed_keys + ) if DEBUG: print(main_dest.name)