Utilities/uic.py

changeset 2969
0e1af1313b8b
parent 2302
f29e9405c851
child 3057
10516539f238
child 3160
209a07d7e401
equal deleted inserted replaced
2968:b109ff4678bc 2969:0e1af1313b8b
11 import os 11 import os
12 12
13 13
14 def compileUiFiles(dir, recurse=False): 14 def compileUiFiles(dir, recurse=False):
15 """ 15 """
16 Module function to compile the .ui files of a directory tree to Python sources. 16 Module function to compile the .ui files of a directory tree to Python
17 sources.
17 18
18 @param dir name of a directory to scan for .ui files (string) 19 @param dir name of a directory to scan for .ui files (string)
19 @param recurse flag indicating to recurse into subdirectories (boolean) 20 @param recurse flag indicating to recurse into subdirectories (boolean)
20 """ 21 """ # __IGNORE_WARNING__
21 try: 22 try:
22 from PyQt4.uic import compileUiDir 23 from PyQt4.uic import compileUiDir
23 except ImportError: 24 except ImportError:
24 from PyQt4.uic import compileUi 25 from PyQt4.uic import compileUi
25 26
26 def compileUiDir(dir, recurse = False, map = None, # __IGNORE_WARNING__ 27 def compileUiDir(dir, recurse=False, # __IGNORE_WARNING__
27 ** compileUi_args): 28 map=None, **compileUi_args):
28 """ 29 """
29 Creates Python modules from Qt Designer .ui files in a directory or 30 Creates Python modules from Qt Designer .ui files in a directory or
30 directory tree. 31 directory tree.
31 32
32 Note: This function is a modified version of the one found in PyQt4. 33 Note: This function is a modified version of the one found in
34 PyQt4.
33 35
34 @param dir Name of the directory to scan for files whose name ends with 36 @param dir Name of the directory to scan for files whose name ends
35 '.ui'. By default the generated Python module is created in the same 37 with '.ui'. By default the generated Python module is created
36 directory ending with '.py'. 38 in the same directory ending with '.py'.
37 @param recurse flag indicating that any sub-directories should be scanned. 39 @param recurse flag indicating that any sub-directories should be
38 @param map an optional callable that is passed the name of the directory 40 scanned.
39 containing the '.ui' file and the name of the Python module that will be 41 @param map an optional callable that is passed the name of the
40 created. The callable should return a tuple of the name of the directory 42 directory containing the '.ui' file and the name of the Python
41 in which the Python module will be created and the (possibly modified) 43 module that will be created. The callable should return a tuple
42 name of the module. 44 of the name of the directory in which the Python module will be
43 @param compileUi_args any additional keyword arguments that are passed to 45 created and the (possibly modified) name of the module.
44 the compileUi() function that is called to create each Python module. 46 @param compileUi_args any additional keyword arguments that are
47 passed to the compileUi() function that is called to create
48 each Python module.
45 """ 49 """
46 def compile_ui(ui_dir, ui_file): 50 def compile_ui(ui_dir, ui_file):
47 """ 51 """
48 Local function to compile a single .ui file. 52 Local function to compile a single .ui file.
49 53
53 # Ignore if it doesn't seem to be a .ui file. 57 # Ignore if it doesn't seem to be a .ui file.
54 if ui_file.endswith('.ui'): 58 if ui_file.endswith('.ui'):
55 py_dir = ui_dir 59 py_dir = ui_dir
56 py_file = ui_file[:-3] + '.py' 60 py_file = ui_file[:-3] + '.py'
57 61
58 # Allow the caller to change the name of the .py file or generate 62 # Allow the caller to change the name of the .py file or
59 # it in a different directory. 63 # generate it in a different directory.
60 if map is not None: 64 if map is not None:
61 py_dir, py_file = list(map(py_dir, py_file)) 65 py_dir, py_file = list(map(py_dir, py_file))
62 66
63 # Make sure the destination directory exists. 67 # Make sure the destination directory exists.
64 try: 68 try:
87 if os.path.isfile(os.path.join(dir, ui)): 91 if os.path.isfile(os.path.join(dir, ui)):
88 compile_ui(dir, ui) 92 compile_ui(dir, ui)
89 93
90 def pyName(py_dir, py_file): 94 def pyName(py_dir, py_file):
91 """ 95 """
92 Local function to create the Python source file name for the compiled .ui file. 96 Local function to create the Python source file name for the compiled
97 .ui file.
93 98
94 @param py_dir suggested name of the directory (string) 99 @param py_dir suggested name of the directory (string)
95 @param py_file suggested name for the compile source file (string) 100 @param py_file suggested name for the compile source file (string)
96 @return tuple of directory name (string) and source file name (string) 101 @return tuple of directory name (string) and source file name (string)
97 """ 102 """

eric ide

mercurial