2021-10-16 00:59:09 +02:00
|
|
|
import re
|
|
|
|
import sys
|
|
|
|
import io
|
2021-10-16 01:20:20 +02:00
|
|
|
import os
|
2021-10-23 06:57:48 +02:00
|
|
|
try:
|
|
|
|
from PyQt6.uic import compileUi
|
|
|
|
except ImportError:
|
|
|
|
# ARM64 Linux builds may not have access to PyQt6, and may have aliased
|
|
|
|
# it to PyQt5. We allow fallback, but the _qt6.py files will not be valid.
|
|
|
|
from PyQt5.uic import compileUi
|
|
|
|
|
2021-10-16 01:20:20 +02:00
|
|
|
from dataclasses import dataclass
|
|
|
|
|
2021-10-16 00:59:09 +02:00
|
|
|
|
|
|
|
def compile(ui_file: str) -> str:
|
|
|
|
buf = io.StringIO()
|
|
|
|
compileUi(open(ui_file), buf)
|
|
|
|
return buf.getvalue()
|
|
|
|
|
2021-10-16 01:20:20 +02:00
|
|
|
|
2021-10-16 00:59:09 +02:00
|
|
|
def with_fixes_for_qt6(code: str) -> str:
|
|
|
|
code = code.replace(
|
|
|
|
"from PyQt6 import QtCore, QtGui, QtWidgets",
|
2021-10-16 01:20:20 +02:00
|
|
|
"from PyQt6 import QtCore, QtGui, QtWidgets\nfrom aqt.utils import tr\n",
|
2021-10-16 00:59:09 +02:00
|
|
|
)
|
|
|
|
code = re.sub(
|
|
|
|
r'(?:QtGui\.QApplication\.)?_?translate\(".*?", "(.*?)"', "tr.\\1(", code
|
|
|
|
)
|
|
|
|
outlines = []
|
|
|
|
qt_bad_types = [
|
|
|
|
".connect(",
|
|
|
|
]
|
|
|
|
for line in code.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:")
|
2021-10-16 01:20:20 +02:00
|
|
|
line = line.replace(
|
|
|
|
"QAction.PreferencesRole", "QAction.MenuRole.PreferencesRole"
|
|
|
|
)
|
2021-10-16 00:59:09 +02:00
|
|
|
line = line.replace("QAction.AboutRole", "QAction.MenuRole.AboutRole")
|
|
|
|
outlines.append(line)
|
|
|
|
return "\n".join(outlines)
|
|
|
|
|
2021-10-16 01:20:20 +02:00
|
|
|
|
2021-10-16 00:59:09 +02:00
|
|
|
def with_fixes_for_qt5(code: str) -> str:
|
2021-10-23 06:57:48 +02:00
|
|
|
code = code.replace(
|
|
|
|
"from PyQt5 import QtCore, QtGui, QtWidgets",
|
|
|
|
"from PyQt5 import QtCore, QtGui, QtWidgets\nfrom aqt.utils import tr\n",
|
|
|
|
)
|
2021-10-16 00:59:09 +02:00
|
|
|
code = code.replace("Qt6", "Qt5")
|
|
|
|
code = code.replace("QtGui.QAction", "QtWidgets.QAction")
|
2021-10-23 06:57:48 +02:00
|
|
|
code = code.replace("import icons_rc", "")
|
2021-10-16 00:59:09 +02:00
|
|
|
return code
|
|
|
|
|
|
|
|
|
2021-10-16 01:20:20 +02:00
|
|
|
@dataclass
|
|
|
|
class UiFileAndOutputs:
|
|
|
|
ui_file: str
|
|
|
|
qt5_file: str
|
|
|
|
qt6_file: str
|
2021-10-16 00:59:09 +02:00
|
|
|
|
|
|
|
|
2021-10-16 01:20:20 +02:00
|
|
|
def get_files() -> list[UiFileAndOutputs]:
|
|
|
|
ui_folder = os.path.dirname(sys.argv[1])
|
|
|
|
py_folder = os.path.dirname(sys.argv[2])
|
|
|
|
out = []
|
|
|
|
for file in os.listdir(ui_folder):
|
|
|
|
if file.endswith(".ui"):
|
|
|
|
base = os.path.splitext(os.path.basename(file))[0]
|
|
|
|
out.append(
|
|
|
|
UiFileAndOutputs(
|
|
|
|
ui_file=os.path.join(ui_folder, file),
|
|
|
|
qt5_file=os.path.join(py_folder, base + "_qt5.py"),
|
|
|
|
qt6_file=os.path.join(py_folder, base + "_qt6.py"),
|
|
|
|
)
|
|
|
|
)
|
|
|
|
return out
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
for entry in get_files():
|
|
|
|
stock = compile(entry.ui_file)
|
|
|
|
for_qt6 = with_fixes_for_qt6(stock)
|
|
|
|
for_qt5 = with_fixes_for_qt5(for_qt6)
|
|
|
|
with open(entry.qt5_file, "w") as file:
|
|
|
|
file.write(for_qt5)
|
|
|
|
with open(entry.qt6_file, "w") as file:
|
|
|
|
file.write(for_qt6)
|