Skip to content

Commit

Permalink
Add python black
Browse files Browse the repository at this point in the history
  • Loading branch information
damontecres committed Feb 10, 2024
1 parent e2a4bc5 commit ccc5e3c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
22 changes: 15 additions & 7 deletions app/convert_strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand All @@ -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}")
Expand All @@ -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("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n")
f.write('<?xml version="1.0" encoding="utf-8"?>\n')
f.write("<resources>\n")
for key, value in parse_dict(data, ["stashapp"]):
key = "_".join(key)
Expand All @@ -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" <string name=\"{key}\" formatted=\"{str(formatted).lower()}\">{value}</string>\n")
f.write(
f' <string name="{key}" formatted="{str(formatted).lower()}">{value}</string>\n'
)
f.write("</resources>\n")
return set(collected_keys)


main_file = source_dir / "en-GB.json"
main_dest = Path(dest_prefix) / "stash_strings.xml"

Expand All @@ -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)
Expand Down

0 comments on commit ccc5e3c

Please sign in to comment.