anki/qt/aqt/forms/build_ui_qt5.py

39 lines
903 B
Python
Raw Normal View History

import re
import sys
import io
from PyQt5.uic import compileUi
ui_file = sys.argv[1]
py_file = sys.argv[2]
buf = io.StringIO()
compileUi(open(ui_file), buf, from_imports=True)
outdata = buf.getvalue()
2020-11-17 10:55:18 +01:00
outdata = outdata.replace(
"# -*- coding: utf-8 -*-", "# -*- coding: utf-8 -*-\nfrom aqt.utils import tr\n"
2020-11-17 10:55:18 +01:00
)
outdata = re.sub(
r'(?:QtGui\.QApplication\.)?_?translate\(".*?", "(.*?)"', "tr.\\1(", outdata
2020-11-17 10:55:18 +01:00
)
outlines = []
2021-04-14 10:22:02 +02:00
qt_bad_types = [
".connect(",
"setStandardButtons",
"setTextInteractionFlags",
"setAlignment",
]
for line in outdata.splitlines():
for substr in qt_bad_types:
if substr in line:
line = line + " # type: ignore"
break
if line == "from . import icons_rc":
continue
line = line.replace(":/icons/", "icons:")
outlines.append(line)
with open(py_file, "w") as file:
file.write("\n".join(outlines))