compileUiFiles.py

changeset 0
de9c2efb9d02
child 12
1d8dd9706f46
equal deleted inserted replaced
-1:000000000000 0:de9c2efb9d02
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 # Copyright (c) 2009 Detlev Offenbach <detlev@die-offenbachs.de>
5 #
6
7 """
8 Script for eric4 to compile all .ui files to Python source.
9 """
10
11 import sys
12 import os
13
14 def compileUiFiles():
15 """
16 Compile the .ui files to Python sources.
17 """
18 try:
19 from PyQt4.uic import compileUiDir
20 except ImportError:
21 from PyQt4.uic import compileUi
22
23 def compileUiDir(dir, recurse = False, map = None, **compileUi_args):
24 """
25 Creates Python modules from Qt Designer .ui files in a directory or
26 directory tree.
27
28 Note: This function is a modified version of the one found in PyQt4.
29
30 @param dir Name of the directory to scan for files whose name ends with
31 '.ui'. By default the generated Python module is created in the same
32 directory ending with '.py'.
33 @param recurse flag indicating that any sub-directories should be scanned.
34 @param map an optional callable that is passed the name of the directory
35 containing the '.ui' file and the name of the Python module that will be
36 created. The callable should return a tuple of the name of the directory
37 in which the Python module will be created and the (possibly modified)
38 name of the module.
39 @param compileUi_args any additional keyword arguments that are passed to
40 the compileUi() function that is called to create each Python module.
41 """
42 def compile_ui(ui_dir, ui_file):
43 """
44 Local function to compile a single .ui file.
45
46 @param ui_dir directory containing the .ui file (string)
47 @param ui_file file name of the .ui file (string)
48 """
49 # Ignore if it doesn't seem to be a .ui file.
50 if ui_file.endswith('.ui'):
51 py_dir = ui_dir
52 py_file = ui_file[:-3] + '.py'
53
54 # Allow the caller to change the name of the .py file or generate
55 # it in a different directory.
56 if map is not None:
57 py_dir, py_file = map(py_dir, py_file)
58
59 # Make sure the destination directory exists.
60 try:
61 os.makedirs(py_dir)
62 except:
63 pass
64
65 ui_path = os.path.join(ui_dir, ui_file)
66 py_path = os.path.join(py_dir, py_file)
67
68 ui_file = open(ui_path, 'r')
69 py_file = open(py_path, 'w')
70
71 try:
72 compileUi(ui_file, py_file, **compileUi_args)
73 finally:
74 ui_file.close()
75 py_file.close()
76
77 if recurse:
78 for root, _, files in os.walk(dir):
79 for ui in files:
80 compile_ui(root, ui)
81 else:
82 for ui in os.listdir(dir):
83 if os.path.isfile(os.path.join(dir, ui)):
84 compile_ui(dir, ui)
85
86 def pyName(py_dir, py_file):
87 """
88 Local function to create the Python source file name for the compiled .ui file.
89
90 @param py_dir suggested name of the directory (string)
91 @param py_file suggested name for the compile source file (string)
92 @return tuple of directory name (string) and source file name (string)
93 """
94 return py_dir, "Ui_%s" % py_file
95
96 compileUiDir(".", True, pyName)
97
98 def main(argv):
99 """
100 The main function of the script.
101
102 @param argv the list of command line arguments.
103 """
104 # Compile .ui files
105 print "Compiling user interface files..."
106 compileUiFiles()
107
108
109 if __name__ == "__main__":
110 try:
111 main(sys.argv)
112 except SystemExit:
113 raise
114 except:
115 print \
116 """An internal error occured. Please report all the output of the program,
117 including the following traceback, to eric5-bugs@eric-ide.python-projects.org.
118 """
119 raise

eric ide

mercurial