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