anki/pylib/tools/rewrite_tr.py

43 lines
1.2 KiB
Python
Raw Normal View History

2021-03-26 03:37:18 +01:00
# Copyright: Ankitects Pty Ltd and contributors
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
from __future__ import annotations
import os
import re
from re import Match
import stringcase
2021-03-27 01:39:53 +01:00
TR_REF = re.compile(r'\.trn\(\s*TR::([^,) ]+),\s+tr_(?:strs|args)!\[((?:"[A-z0-9_]+"\s*=>[^=]+?)+)]\s*,?\s*\)')
TR_ARG_INNER = re.compile(r'"([A-z0-9_]+)"\s*=>(?!=>)+,?\s*')
2021-03-26 03:37:18 +01:00
2021-03-27 01:39:53 +01:00
# eg "count"=>output.trash_count, "megs"=>megs
def rewrite_tr_args(args: str) -> str:
return TR_ARG_INNER.sub("", args)
2021-03-26 03:37:18 +01:00
def repl(m: Match) -> str:
2021-03-26 14:16:08 +01:00
name = stringcase.snakecase(m.group(1))
2021-03-27 01:39:53 +01:00
args = rewrite_tr_args(m.group(2))
#print(m.group(0))
#print(f".{name}({args})")
#print(args)
return f".{name}({args})"
2021-03-26 03:37:18 +01:00
def update_py(path: str) -> None:
buf = open(path).read()
buf2 = TR_REF.sub(repl, buf)
if buf != buf2:
open(path, "w").writelines(buf2)
2021-03-26 03:37:18 +01:00
print("updated", path)
2021-03-27 01:39:53 +01:00
# print(buf2)
2021-03-26 03:37:18 +01:00
for dirpath, dirnames, fnames in os.walk(os.environ["BUILD_WORKSPACE_DIRECTORY"]):
if "bazel-" in dirpath:
continue
for fname in fnames:
2021-03-26 14:16:08 +01:00
if fname.endswith(".rs"):
2021-03-26 03:37:18 +01:00
update_py(os.path.join(dirpath, fname))