LunaTranslator/py/generate_xp_code.py

28 lines
943 B
Python
Raw Normal View History

2024-11-15 00:00:34 +08:00
import os, re
def parsecode(code: str):
# PyQt
code = code.replace("self.screen().geometry().height()", "99999")
code = code.replace("self.parent().devicePixelRatioF()", "1")
code = code.replace("self.devicePixelRatioF()", "1")
code = re.sub(
r"(Q[a-zA-Z0-9_]+)\.[a-zA-Z0-9_]+\.([a-zA-Z0-9_]+)([ \)\n,:])", r"\1.\2\3", code
)
# 移除类型注解
code = re.sub(r": [a-zA-Z0-9_]+\)", ")", code)
2024-11-17 03:03:26 +08:00
code = re.sub(r"([a-zA-Z0-9_]): [a-zA-Z0-9_]+,", r"\1,", code)
2024-11-16 23:20:48 +08:00
code = re.sub(r": [a-zA-Z0-9_]+ =", " =", code)
2024-11-15 00:00:34 +08:00
return code
for _dir, _, _fs in os.walk("./LunaTranslator"):
for _f in _fs:
if not _f.endswith(".py"):
continue
path = os.path.normpath(os.path.abspath(os.path.join(_dir, _f)))
with open(path, "r", encoding="utf8") as ff:
code = ff.read()
with open(path, "w", encoding="utf8") as ff:
ff.write(parsecode(code))