scripts/compileUiFiles.py

branch
eric7
changeset 9986
2c571c1c7b0d
parent 9653
e67609152c5e
child 10065
de4ae767b0e3
equal deleted inserted replaced
9985:4098449deed5 9986:2c571c1c7b0d
9 """ 9 """
10 10
11 import os 11 import os
12 import sys 12 import sys
13 13
14 from PyQt6.uic import compileUiDir 14 from functools import partial
15
16 from PyQt6.uic import compileUi
15 17
16 18
17 def __pyName(py_dir, py_file): 19 def __pyName(py_dir, py_file):
18 """ 20 """
19 Local function to create the Python source file name for the compiled 21 Local function to create the Python source file name for the compiled
24 @return tuple of directory name (string) and source file name (string) 26 @return tuple of directory name (string) and source file name (string)
25 """ 27 """
26 return py_dir, "Ui_{0}".format(py_file) 28 return py_dir, "Ui_{0}".format(py_file)
27 29
28 30
31 def __compileOneUi(ui_path, mapFunc=None, execute=False, indent=4):
32 """
33 Function to compile a single form file to Python code.
34
35 @param ui_path path of the Qt form file
36 @type str
37 @param mapFunc function to change directory and/or name of the resulting Python file
38 (defaults to None)
39 @type func (optional)
40 @param execute flag indicating to generate code to execute the form in standalone
41 mode (defaults to False)
42 @type bool (optional)
43 @param indent indentation width using spaces (defaults to 4)
44 @type int (optional)
45 """
46 py_dir, py_file = os.path.split(ui_path[:-3] + ".py")
47
48 # Allow the caller to change the name of the .py file or generate
49 # it in a different directory.
50 if mapFunc is not None:
51 py_dir, py_file = mapFunc(py_dir, py_file)
52
53 # Make sure the destination directory exists.
54 os.makedirs(py_dir, exist_ok=True)
55
56 py_path = os.path.join(py_dir, py_file)
57
58 with open(py_path, "w", encoding="utf-8") as py_file:
59 compileUi(ui_path, py_file, execute=execute, indent=indent)
60
61
62 def compileUiDir(root, recurse=False, mapFunc=None, workers=1, execute=False, indent=4):
63 """
64 Function to compile all Qt form files of a directory or directory tree
65 to Python code.
66
67 @param root directory to scan for Qt form files (i.e. files ending with '.ui'
68 @type str
69 @param recurse flag indicating to recurse into sub-directories (defaults to False)
70 @type bool (optional)
71 @param mapFunc function to change directory and/or name of the resulting Python file
72 (defaults to None)
73 @type func (optional)
74 @param workers number of worker processes to be used to compile (defaults to 1)
75 @type int (optional)
76 @param execute flag indicating to generate code to execute the form in standalone
77 mode (defaults to False)
78 @type bool (optional)
79 @param indent indentation width using spaces (defaults to 4)
80 @type int (optional)
81 """
82 if recurse:
83 ui_files = []
84 for rootDir, _, files in os.walk(root):
85 ui_files.extend(
86 os.path.join(rootDir, ui) for ui in files if ui.endswith(".ui")
87 )
88 else:
89 ui_files = [
90 os.path.join(root, ui)
91 for ui in os.listdir(root)
92 if os.path.isfile(os.path.join(root, ui) and ui.endswith(".ui"))
93 ]
94
95 ProcessPoolExecutor = None
96 if workers != 1:
97 try:
98 from concurrent.futures import ProcessPoolExecutor # __IGNORE_WARNING__
99 except NotImplementedError:
100 workers = 1
101
102 if workers != 1 and ProcessPoolExecutor is not None:
103 # If workers == 0, let ProcessPoolExecutor determine worker count.
104 workers = workers or None
105 with ProcessPoolExecutor(max_workers=workers) as executor:
106 executor.map(
107 partial(
108 __compileOneUi, mapFunc=mapFunc, execute=execute, indent=indent
109 ),
110 ui_files,
111 )
112 else:
113 for ui_file in ui_files:
114 __compileOneUi(ui_file, mapFunc=mapFunc, execute=execute, indent=indent)
115
116
29 def compileUiFiles(): 117 def compileUiFiles():
30 """ 118 """
31 Compile the .ui files to Python sources. 119 Compile the .ui files to Python sources.
32 """ 120 """
33 if os.path.exists("src"): 121 if os.path.exists("src"):
34 # eric7 with 'src' layout 122 # eric7 with 'src' layout
35 compileUiDir(os.path.join("src", "eric7"), recurse=True, map=__pyName) 123 compileUiDir(
124 os.path.join("src", "eric7"), recurse=True, mapFunc=__pyName, workers=0
125 )
36 elif os.path.exists("eric7"): 126 elif os.path.exists("eric7"):
37 # old layout or invoked from within 'src' 127 # old layout or invoked from within 'src'
38 compileUiDir("eric7", recurse=True, map=__pyName) 128 compileUiDir("eric7", recurse=True, mapFunc=__pyName, workers=0)
39 else: 129 else:
40 print("No valid 'eric7' source layout could be found. Aborting...") 130 print("No valid 'eric7' source layout could be found. Aborting...")
41 131
42 132
43 def main(argv): 133 def main(argv):

eric ide

mercurial