File: compile_ui_files.py

package info (click to toggle)
finalcif 137%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 43,544 kB
  • sloc: python: 49,119; cpp: 67; sh: 51; makefile: 22
file content (24 lines) | stat: -rw-r--r-- 815 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
from pathlib import Path

from qtpy import uic

def fix_comment(pyfile: Path, uifile: Path):
    txt = pyfile.read_text()
    lines = txt.splitlines(keepends=True)
    lines[2] = f"# Form implementation generated from reading ui file '{uifile.name}'\n"
    lines[4] = "# Created by: PyQt5 UI code generator"
    pyfile.write_text(data=''.join(lines))


def compile_ui():
    ui_files = Path(__file__).parent.parent.rglob('*.ui')
    for ui_file in ui_files:
        py_file = ui_file.with_suffix('.py')
        with open(py_file, 'w', encoding='utf-8') as pyf, open(ui_file, 'r', encoding='utf-8') as uif:
            uic.compileUi(uifile=uif, pyfile=pyf, execute=True)
            print(f'Compiling {ui_file.name}')
        fix_comment(pyfile=py_file, uifile=ui_file)


if __name__ == '__main__':
    compile_ui()