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