10 |
10 |
11 from __future__ import unicode_literals |
11 from __future__ import unicode_literals |
12 from __future__ import print_function |
12 from __future__ import print_function |
13 |
13 |
14 import sys |
14 import sys |
15 import os |
15 |
|
16 # step 1: determine PyQt variant, preferring PyQt5 |
|
17 try: |
|
18 import PyQt5 # __IGNORE_WARNING__ |
|
19 pyqtVariant = "PyQt5" |
|
20 except ImportError: |
|
21 import PyQt4 # __IGNORE_WARNING__ |
|
22 pyqtVariant = "PyQt4" |
|
23 |
|
24 # step 2: compile the UI files |
|
25 if pyqtVariant == "PyQt4": |
|
26 from PyQt4.uic import compileUiDir |
|
27 else: |
|
28 from PyQt5.uic import compileUiDir |
|
29 |
|
30 |
|
31 def __pyName(py_dir, py_file): |
|
32 """ |
|
33 Local function to create the Python source file name for the compiled |
|
34 .ui file. |
|
35 |
|
36 @param py_dir suggested name of the directory (string) |
|
37 @param py_file suggested name for the compile source file (string) |
|
38 @return tuple of directory name (string) and source file name (string) |
|
39 """ |
|
40 return py_dir, "Ui_{0}".format(py_file) |
16 |
41 |
17 |
42 |
18 def compileUiFiles(): |
43 def compileUiFiles(): |
19 """ |
44 """ |
20 Compile the .ui files to Python sources. |
45 Compile the .ui files to Python sources. |
21 """ # __IGNORE_WARNING__ |
46 """ |
22 # step 1: determine PyQt variant, preferring PyQt5 |
47 compileUiDir(".", True, __pyName) |
23 try: |
|
24 import PyQt5 # __IGNORE_WARNING__ |
|
25 pyqtVariant = "PyQt5" |
|
26 except ImportError: |
|
27 import PyQt4 # __IGNORE_WARNING__ |
|
28 pyqtVariant = "PyQt4" |
|
29 |
|
30 # step 2: compile the UI files |
|
31 try: |
|
32 if pyqtVariant == "PyQt4": |
|
33 from PyQt4.uic import compileUiDir |
|
34 else: |
|
35 from PyQt5.uic import compileUiDir |
|
36 except ImportError: |
|
37 if pyqtVariant == "PyQt4": |
|
38 from PyQt4.uic import compileUi |
|
39 else: |
|
40 from PyQt5.uic import compileUi |
|
41 |
|
42 def compileUiDir(dir, recurse=False, # __IGNORE_WARNING__ |
|
43 map=None, **compileUi_args): |
|
44 """ |
|
45 Creates Python modules from Qt Designer .ui files in a directory or |
|
46 directory tree. |
|
47 |
|
48 Note: This function is a modified version of the one found in |
|
49 PyQt5. |
|
50 |
|
51 @param dir Name of the directory to scan for files whose name ends |
|
52 with '.ui'. By default the generated Python module is created |
|
53 in the same directory ending with '.py'. |
|
54 @param recurse flag indicating that any sub-directories should be |
|
55 scanned. |
|
56 @param map an optional callable that is passed the name of the |
|
57 directory containing the '.ui' file and the name of the Python |
|
58 module that will be created. The callable should return a tuple |
|
59 of the name of the directory in which the Python module will be |
|
60 created and the (possibly modified) name of the module. |
|
61 @param compileUi_args any additional keyword arguments that are |
|
62 passed to the compileUi() function that is called to create |
|
63 each Python module. |
|
64 """ |
|
65 def compile_ui(ui_dir, ui_file): |
|
66 """ |
|
67 Local function to compile a single .ui file. |
|
68 |
|
69 @param ui_dir directory containing the .ui file (string) |
|
70 @param ui_file file name of the .ui file (string) |
|
71 """ |
|
72 # Ignore if it doesn't seem to be a .ui file. |
|
73 if ui_file.endswith('.ui'): |
|
74 py_dir = ui_dir |
|
75 py_file = ui_file[:-3] + '.py' |
|
76 |
|
77 # Allow the caller to change the name of the .py file or |
|
78 # generate it in a different directory. |
|
79 if map is not None: |
|
80 py_dir, py_file = list(map(py_dir, py_file)) |
|
81 |
|
82 # Make sure the destination directory exists. |
|
83 try: |
|
84 os.makedirs(py_dir) |
|
85 except: |
|
86 pass |
|
87 |
|
88 ui_path = os.path.join(ui_dir, ui_file) |
|
89 py_path = os.path.join(py_dir, py_file) |
|
90 |
|
91 ui_file = open(ui_path, 'r') |
|
92 py_file = open(py_path, 'w') |
|
93 |
|
94 try: |
|
95 compileUi(ui_file, py_file, **compileUi_args) |
|
96 finally: |
|
97 ui_file.close() |
|
98 py_file.close() |
|
99 |
|
100 if recurse: |
|
101 for root, _, files in os.walk(dir): |
|
102 for ui in files: |
|
103 compile_ui(root, ui) |
|
104 else: |
|
105 for ui in os.listdir(dir): |
|
106 if os.path.isfile(os.path.join(dir, ui)): |
|
107 compile_ui(dir, ui) |
|
108 |
|
109 def pyName(py_dir, py_file): |
|
110 """ |
|
111 Local function to create the Python source file name for the compiled |
|
112 .ui file. |
|
113 |
|
114 @param py_dir suggested name of the directory (string) |
|
115 @param py_file suggested name for the compile source file (string) |
|
116 @return tuple of directory name (string) and source file name (string) |
|
117 """ |
|
118 return py_dir, "Ui_{0}".format(py_file) |
|
119 |
|
120 compileUiDir(".", True, pyName) |
|
121 |
48 |
122 |
49 |
123 def main(argv): |
50 def main(argv): |
124 """ |
51 """ |
125 The main function of the script. |
52 The main function of the script. |