-
Notifications
You must be signed in to change notification settings - Fork 2
/
BuildUiFiles.py
113 lines (90 loc) · 3.49 KB
/
BuildUiFiles.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/usr/bin/python
# Original Author: Christopher Nyland
# Original Creation Date: 2012/08/13
"""
This script file creates the pyc ui files and also creates the __init__.py file
that makes this folder a module. These are regenerated each time the script is
run. It automatically deletes the py files that it creates with uic after
compiling them. If you add ui files or make changes just run this script and
it will make the correct files and add additional ui modules to the __init__
file.
If you don't want the script to automatically delete the py files then run it
from the command line with --dontremove argument.
"""
import os
import sys
import ast
import py_compile
from PyQt4 import uic
file_names = []
class ClassParser(ast.NodeVisitor):
""" This class is used with the AST model to collect all the class names
in a file
"""
def __init__(self):
self.class_names = []
def parse(self, code):
""" parser function
This function is the acutal parser of the code. Its input is the code
to be parsed and is the one that calls the visitors for each part.
It returns the class names
"""
self.class_names = []
tree = ast.parse(code)
self.visit(tree)
return self.class_names
def visit_ClassDef(self, stmt):
""" Visitor for class definitions """
self.class_names.append(stmt.name)
super(ClassParser, self).generic_visit(stmt)
def modname(ui_dir, ui_file):
""" This is a rename mapping function for compileUiDir """
out_dir = ui_dir
out_file = 'Ui_{0}'.format(ui_file)
out_path = os.path.join(out_dir, out_file)
file_names.append(out_path)
return (out_dir, out_file)
def main(argv, dir='.'):
""" Main function that parses and compiles the files """
parser = ClassParser()
if isinstance(dir, str):
dir = [dir]
for dir in dir[:]:
# Create py files from ui files
# file_names is passed around here as it is outside of each functions scope
del file_names[:]
uic.compileUiDir(dir, map=modname)
# Parse those py_ui files for the class names
file_classes = {}
for file in file_names:
with open(file,'rb') as fid:
code = fid.read()
file_classes[file] = parser.parse(code)
# Create an init file filling it with import statements using the class
# names that were parse above
init_file = os.path.join(dir, "__init__.py")
with open(init_file, "wb") as fid:
fid.write("#This file is generated by BuildUiFiles.py")
fid.write(os.linesep)
for k, v in file_classes.items():
file = os.path.split(k)[1]
i_from = os.path.splitext(file)[0]
print(i_from)
for mod in v:
stmt = "from {} import {}".format(i_from, mod)
fid.write(stmt)
fid.write(os.linesep)
py_compile.compile(init_file)
# Compile the py_ui file and then remove the uncompiled versions unless
# the dontremove flag was set.
py_compile.main(file_names)
if not("--dontremove" in argv):
for k in file_names:
os.remove(k)
if __name__ == "__main__":
if '-v' in sys.argv:
print(__version__)
sys.exit()
else:
main(sys.argv)
raw_input("Done")