compileUiFiles.py

changeset 2969
0e1af1313b8b
parent 2302
f29e9405c851
child 3021
801289962f4e
child 3057
10516539f238
equal deleted inserted replaced
2968:b109ff4678bc 2969:0e1af1313b8b
1 #!/usr/bin/env python 1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*- 2 # -*- coding: utf-8 -*-
3 3
4 # Copyright (c) 2009 - 2013 Detlev Offenbach <detlev@die-offenbachs.de> 4 # Copyright (c) 2009 - 2013 Detlev Offenbach <detlev@die-offenbachs.de>
5 # 5 #
6 6
13 13
14 14
15 def compileUiFiles(): 15 def compileUiFiles():
16 """ 16 """
17 Compile the .ui files to Python sources. 17 Compile the .ui files to Python sources.
18 """ 18 """ # __IGNORE_WARNING__
19 try: 19 try:
20 from PyQt4.uic import compileUiDir 20 from PyQt4.uic import compileUiDir
21 except ImportError: 21 except ImportError:
22 from PyQt4.uic import compileUi 22 from PyQt4.uic import compileUi
23 23
24 def compileUiDir(dir, recurse = False, map = None, # __IGNORE_WARNING__ 24 def compileUiDir(dir, recurse=False, # __IGNORE_WARNING__
25 ** compileUi_args): 25 map=None, **compileUi_args):
26 """ 26 """
27 Creates Python modules from Qt Designer .ui files in a directory or 27 Creates Python modules from Qt Designer .ui files in a directory or
28 directory tree. 28 directory tree.
29 29
30 Note: This function is a modified version of the one found in PyQt4. 30 Note: This function is a modified version of the one found in
31 PyQt4.
31 32
32 @param dir Name of the directory to scan for files whose name ends with 33 @param dir Name of the directory to scan for files whose name ends
33 '.ui'. By default the generated Python module is created in the same 34 with '.ui'. By default the generated Python module is created
34 directory ending with '.py'. 35 in the same directory ending with '.py'.
35 @param recurse flag indicating that any sub-directories should be scanned. 36 @param recurse flag indicating that any sub-directories should be
36 @param map an optional callable that is passed the name of the directory 37 scanned.
37 containing the '.ui' file and the name of the Python module that will be 38 @param map an optional callable that is passed the name of the
38 created. The callable should return a tuple of the name of the directory 39 directory containing the '.ui' file and the name of the Python
39 in which the Python module will be created and the (possibly modified) 40 module that will be created. The callable should return a tuple
40 name of the module. 41 of the name of the directory in which the Python module will be
41 @param compileUi_args any additional keyword arguments that are passed to 42 created and the (possibly modified) name of the module.
42 the compileUi() function that is called to create each Python module. 43 @param compileUi_args any additional keyword arguments that are
44 passed to the compileUi() function that is called to create
45 each Python module.
43 """ 46 """
44 def compile_ui(ui_dir, ui_file): 47 def compile_ui(ui_dir, ui_file):
45 """ 48 """
46 Local function to compile a single .ui file. 49 Local function to compile a single .ui file.
47 50
51 # Ignore if it doesn't seem to be a .ui file. 54 # Ignore if it doesn't seem to be a .ui file.
52 if ui_file.endswith('.ui'): 55 if ui_file.endswith('.ui'):
53 py_dir = ui_dir 56 py_dir = ui_dir
54 py_file = ui_file[:-3] + '.py' 57 py_file = ui_file[:-3] + '.py'
55 58
56 # Allow the caller to change the name of the .py file or generate 59 # Allow the caller to change the name of the .py file or
57 # it in a different directory. 60 # generate it in a different directory.
58 if map is not None: 61 if map is not None:
59 py_dir, py_file = list(map(py_dir, py_file)) 62 py_dir, py_file = list(map(py_dir, py_file))
60 63
61 # Make sure the destination directory exists. 64 # Make sure the destination directory exists.
62 try: 65 try:
85 if os.path.isfile(os.path.join(dir, ui)): 88 if os.path.isfile(os.path.join(dir, ui)):
86 compile_ui(dir, ui) 89 compile_ui(dir, ui)
87 90
88 def pyName(py_dir, py_file): 91 def pyName(py_dir, py_file):
89 """ 92 """
90 Local function to create the Python source file name for the compiled .ui file. 93 Local function to create the Python source file name for the compiled
94 .ui file.
91 95
92 @param py_dir suggested name of the directory (string) 96 @param py_dir suggested name of the directory (string)
93 @param py_file suggested name for the compile source file (string) 97 @param py_file suggested name for the compile source file (string)
94 @return tuple of directory name (string) and source file name (string) 98 @return tuple of directory name (string) and source file name (string)
95 """ 99 """
113 try: 117 try:
114 main(sys.argv) 118 main(sys.argv)
115 except SystemExit: 119 except SystemExit:
116 raise 120 raise
117 except: 121 except:
118 print("""An internal error occured. Please report all the output of the program, 122 print("""
123 An internal error occured. Please report all the output of the program,
119 including the following traceback, to eric5-bugs@eric-ide.python-projects.org. 124 including the following traceback, to eric5-bugs@eric-ide.python-projects.org.
120 """) 125 """)
121 raise 126 raise

eric ide

mercurial