|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2009 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing a function to compile all user interface files of a |
|
8 directory or directory tree. |
|
9 """ |
|
10 |
|
11 import os |
|
12 |
|
13 def compileUiFiles(dir, recurse = False): |
|
14 """ |
|
15 Module function to compile the .ui files of a directory tree to Python sources. |
|
16 |
|
17 @param dir name of a directory to scan for .ui files (string or QString) |
|
18 @param recurse flag indicating to recurse into subdirectories (boolean) |
|
19 """ |
|
20 try: |
|
21 from PyQt4.uic import compileUiDir |
|
22 except ImportError: |
|
23 from PyQt4.uic import compileUi |
|
24 |
|
25 def compileUiDir(dir, recurse = False, map = None, **compileUi_args): |
|
26 """ |
|
27 Creates Python modules from Qt Designer .ui files in a directory or |
|
28 directory tree. |
|
29 |
|
30 Note: This function is a modified version of the one found in PyQt4. |
|
31 |
|
32 @param dir Name of the directory to scan for files whose name ends with |
|
33 '.ui'. By default the generated Python module is created in the same |
|
34 directory ending with '.py'. |
|
35 @param recurse flag indicating that any sub-directories should be scanned. |
|
36 @param map an optional callable that is passed the name of the directory |
|
37 containing the '.ui' file and the name of the Python module that will be |
|
38 created. The callable should return a tuple of the name of the directory |
|
39 in which the Python module will be created and the (possibly modified) |
|
40 name of the module. |
|
41 @param compileUi_args any additional keyword arguments that are passed to |
|
42 the compileUi() function that is called to create each Python module. |
|
43 """ |
|
44 def compile_ui(ui_dir, ui_file): |
|
45 """ |
|
46 Local function to compile a single .ui file. |
|
47 |
|
48 @param ui_dir directory containing the .ui file (string) |
|
49 @param ui_file file name of the .ui file (string) |
|
50 """ |
|
51 # Ignore if it doesn't seem to be a .ui file. |
|
52 if ui_file.endswith('.ui'): |
|
53 py_dir = ui_dir |
|
54 py_file = ui_file[:-3] + '.py' |
|
55 |
|
56 # Allow the caller to change the name of the .py file or generate |
|
57 # it in a different directory. |
|
58 if map is not None: |
|
59 py_dir, py_file = map(py_dir, py_file) |
|
60 |
|
61 # Make sure the destination directory exists. |
|
62 try: |
|
63 os.makedirs(py_dir) |
|
64 except: |
|
65 pass |
|
66 |
|
67 ui_path = os.path.join(ui_dir, ui_file) |
|
68 py_path = os.path.join(py_dir, py_file) |
|
69 |
|
70 ui_file = open(ui_path, 'r') |
|
71 py_file = open(py_path, 'w') |
|
72 |
|
73 try: |
|
74 compileUi(ui_file, py_file, **compileUi_args) |
|
75 finally: |
|
76 ui_file.close() |
|
77 py_file.close() |
|
78 |
|
79 if recurse: |
|
80 for root, _, files in os.walk(dir): |
|
81 for ui in files: |
|
82 compile_ui(root, ui) |
|
83 else: |
|
84 for ui in os.listdir(dir): |
|
85 if os.path.isfile(os.path.join(dir, ui)): |
|
86 compile_ui(dir, ui) |
|
87 |
|
88 def pyName(py_dir, py_file): |
|
89 """ |
|
90 Local function to create the Python source file name for the compiled .ui file. |
|
91 |
|
92 @param py_dir suggested name of the directory (string) |
|
93 @param py_file suggested name for the compile source file (string) |
|
94 @return tuple of directory name (string) and source file name (string) |
|
95 """ |
|
96 return py_dir, "Ui_%s" % py_file |
|
97 |
|
98 compileUiDir(dir, recurse, pyName) |