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