|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2011 - 2021 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module to get the object name, class name or signatures of a Qt form (*.ui). |
|
8 """ |
|
9 |
|
10 import os |
|
11 import sys |
|
12 import json |
|
13 import xml.etree.ElementTree # secok |
|
14 import contextlib |
|
15 |
|
16 try: |
|
17 from PyQt5.QtCore import QMetaMethod, QByteArray |
|
18 from PyQt5.QtWidgets import QAction, QWidget, QApplication |
|
19 from PyQt5 import uic |
|
20 except ImportError: |
|
21 print("PyQt5 could not be found.") |
|
22 sys.exit(1) |
|
23 |
|
24 with contextlib.suppress(ImportError): |
|
25 from PyQt5 import QtWebEngineWidgets # __IGNORE_WARNING__ |
|
26 |
|
27 sys.path.append(os.path.dirname(os.path.dirname(__file__))) |
|
28 # add the eric package directory |
|
29 |
|
30 |
|
31 def objectName(formFile, projectPath): |
|
32 """ |
|
33 Function to get the object name of a form. |
|
34 |
|
35 @param formFile file name of the form |
|
36 @type str |
|
37 @param projectPath directory name of the project |
|
38 @type str |
|
39 """ |
|
40 app = QApplication([]) # __IGNORE_WARNING__ |
|
41 try: |
|
42 dlg = uic.loadUi(formFile, package=projectPath) |
|
43 print(dlg.objectName()) |
|
44 sys.exit(0) |
|
45 except (AttributeError, ImportError, |
|
46 xml.etree.ElementTree.ParseError) as err: |
|
47 print(str(err)) |
|
48 sys.exit(1) |
|
49 |
|
50 |
|
51 def className(formFile, projectPath): |
|
52 """ |
|
53 Function to get the class name of a form. |
|
54 |
|
55 @param formFile file name of the form |
|
56 @type str |
|
57 @param projectPath directory name of the project |
|
58 @type str |
|
59 """ |
|
60 app = QApplication([]) # __IGNORE_WARNING__ |
|
61 try: |
|
62 dlg = uic.loadUi(formFile, package=projectPath) |
|
63 print(dlg.metaObject().className()) |
|
64 sys.exit(0) |
|
65 except (AttributeError, ImportError, |
|
66 xml.etree.ElementTree.ParseError) as err: |
|
67 print(str(err)) |
|
68 sys.exit(1) |
|
69 |
|
70 |
|
71 def __mapType(type_): |
|
72 """ |
|
73 Private function to map a type as reported by Qt's meta object to the |
|
74 correct Python type. |
|
75 |
|
76 @param type_ type as reported by Qt |
|
77 @type QByteArray or bytes |
|
78 @return mapped Python type |
|
79 @rtype str |
|
80 """ |
|
81 mapped = bytes(type_).decode() |
|
82 |
|
83 # I. always check for * |
|
84 mapped = mapped.replace("*", "") |
|
85 |
|
86 # 1. check for const |
|
87 mapped = mapped.replace("const ", "") |
|
88 |
|
89 # 2. replace QString and QStringList |
|
90 mapped = ( |
|
91 mapped |
|
92 .replace("QStringList", "list") |
|
93 .replace("QString", "str") |
|
94 ) |
|
95 |
|
96 # 3. replace double by float |
|
97 mapped = mapped.replace("double", "float") |
|
98 |
|
99 return mapped |
|
100 |
|
101 |
|
102 def signatures(formFile, projectPath): |
|
103 """ |
|
104 Function to get the signatures of form elements. |
|
105 |
|
106 @param formFile file name of the form |
|
107 @type str |
|
108 @param projectPath directory name of the project |
|
109 @type str |
|
110 """ |
|
111 objectsList = [] |
|
112 |
|
113 app = QApplication([]) # __IGNORE_WARNING__ |
|
114 try: |
|
115 dlg = uic.loadUi(formFile, package=projectPath) |
|
116 objects = dlg.findChildren(QWidget) + dlg.findChildren(QAction) |
|
117 for obj in objects: |
|
118 name = obj.objectName() |
|
119 if not name or name.startswith("qt_"): |
|
120 # ignore un-named or internal objects |
|
121 continue |
|
122 |
|
123 metaObject = obj.metaObject() |
|
124 objectDict = { |
|
125 "name": name, |
|
126 "class_name": metaObject.className(), |
|
127 "methods": [], |
|
128 } |
|
129 |
|
130 for index in range(metaObject.methodCount()): |
|
131 metaMethod = metaObject.method(index) |
|
132 if metaMethod.methodType() == QMetaMethod.MethodType.Signal: |
|
133 signatureDict = { |
|
134 "methods": [] |
|
135 } |
|
136 signatureDict["signature"] = "on_{0}_{1}".format( |
|
137 name, |
|
138 bytes(metaMethod.methodSignature()).decode() |
|
139 ) |
|
140 |
|
141 signatureDict["methods"].append("on_{0}_{1}".format( |
|
142 name, |
|
143 bytes(metaMethod.methodSignature()) |
|
144 .decode().split("(")[0] |
|
145 )) |
|
146 signatureDict["methods"].append("{0}({1})".format( |
|
147 signatureDict["methods"][-1], |
|
148 ", ".join([ |
|
149 __mapType(t) |
|
150 for t in metaMethod.parameterTypes() |
|
151 ]) |
|
152 )) |
|
153 |
|
154 returnType = __mapType( |
|
155 metaMethod.typeName().encode()) |
|
156 if returnType == 'void': |
|
157 returnType = "" |
|
158 signatureDict["return_type"] = returnType |
|
159 parameterTypesList = [ |
|
160 __mapType(t) |
|
161 for t in metaMethod.parameterTypes() |
|
162 ] |
|
163 signatureDict["parameter_types"] = parameterTypesList |
|
164 pyqtSignature = ", ".join(parameterTypesList) |
|
165 signatureDict["pyqt_signature"] = pyqtSignature |
|
166 |
|
167 parameterNames = metaMethod.parameterNames() |
|
168 if parameterNames: |
|
169 for index in range(len(parameterNames)): |
|
170 if not parameterNames[index]: |
|
171 parameterNames[index] = QByteArray( |
|
172 "p{0:d}".format(index).encode("utf-8") |
|
173 ) |
|
174 parameterNamesList = [bytes(n).decode() |
|
175 for n in parameterNames] |
|
176 signatureDict["parameter_names"] = parameterNamesList |
|
177 methNamesSig = ", ".join(parameterNamesList) |
|
178 |
|
179 if methNamesSig: |
|
180 pythonSignature = "on_{0}_{1}(self, {2})".format( |
|
181 name, |
|
182 bytes(metaMethod.methodSignature()) |
|
183 .decode().split("(")[0], |
|
184 methNamesSig) |
|
185 else: |
|
186 pythonSignature = "on_{0}_{1}(self)".format( |
|
187 name, |
|
188 bytes(metaMethod.methodSignature()) |
|
189 .decode().split("(")[0]) |
|
190 signatureDict["python_signature"] = pythonSignature |
|
191 |
|
192 objectDict["methods"].append(signatureDict) |
|
193 |
|
194 objectsList.append(objectDict) |
|
195 |
|
196 print(json.dumps(objectsList)) |
|
197 sys.exit(0) |
|
198 except (AttributeError, ImportError, |
|
199 xml.etree.ElementTree.ParseError) as err: |
|
200 print(str(err)) |
|
201 sys.exit(1) |
|
202 |
|
203 |
|
204 if __name__ == "__main__": |
|
205 if len(sys.argv) != 4: |
|
206 print("Wrong number of arguments.") |
|
207 sys.exit(1) |
|
208 |
|
209 if sys.argv[1] == "object_name": |
|
210 objectName(sys.argv[2], sys.argv[3]) |
|
211 elif sys.argv[1] == "class_name": |
|
212 className(sys.argv[2], sys.argv[3]) |
|
213 elif sys.argv[1] == "signatures": |
|
214 signatures(sys.argv[2], sys.argv[3]) |
|
215 else: |
|
216 print("Unknow operation given.") |
|
217 sys.exit(1) |
|
218 |
|
219 # |
|
220 # eflag: noqa = M701, M801 |