forked from mottosso/Qt.py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build_membership.py
332 lines (278 loc) · 8.15 KB
/
build_membership.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
import json
def build_membership():
"""Generate a .json file with all members of PySide2"""
# NOTE: PySide2, as of this writing, is incomplete.
# In it's __all__ module is a module, `QtOpenGL`
# that does no exists. This causes `import *` to fail.
from PySide2 import __all__
__all__.remove("QtOpenGL")
# These modules do not exist pre-Qt 5,
# so do not bother testing for them.
__all__.remove("QtSql")
__all__.remove("QtSvg")
# These should be present in PySide2,
# but are not as of this writing.
for missing in ("QtWidgets",
"QtXml",
"QtHelp",
"QtPrintSupport"):
__all__.append(missing)
# Why `import *`?
#
# PySide, and PyQt, perform magic that triggers via Python's
# import mechanism. If we try and sidestep it in any way, say
# by using `imp.load_module` or `__import__`, the mechanism
# will not trigger and the compiled libraries will not get loaded.
#
# Wildcard was the only way I could think of to import everything,
# without hardcoding the members, such as QtCore into the function.
from PySide2 import *
# Serialise members
members = {}
for name, module in locals().copy().items():
if name.startswith("_"):
continue
if name in ("json", "members", "missing"):
continue
members[name] = list(member for member in dir(module)
if not member.startswith("_"))
# Write to disk
with open("reference_members.json", "w") as f:
json.dump(members, f, indent=4)
def build_tests():
"""Build membership tests
Members only available in Qt 5 are excluded, along with member
exclusive to a paricular binding.
"""
header = """\
#
# AUTOMATICALLY GENERATED MEMBERSHIP TEST, DO NOT MODIFY
#
import os
import json
with open("reference_members.json") as f:
reference_members = json.load(f)
excluded = {excluded}
""".format(excluded=json.dumps(excluded, indent=4))
test = """\
def test_{binding}_members():
os.environ["QT_PREFERRED_BINDING"] = "{Binding}"
if "PyQt" in "{Binding}":
# PyQt4 and 5 performs some magic here
# that must take place before attempting
# to import with wildcard.
from Qt import Qt as _
if "PySide2" == "{Binding}":
# PySide2, as of this writing, doesn't include
# these modules in it's __all__ list; leaving
# the wildcard import below untrue.
from Qt import __all__
for missing in ("QtWidgets",
"QtXml",
"QtHelp",
"QtPrintSupport"):
__all__.append(missing)
from Qt import *
if "PySide" == "{Binding}":
# Qt 4 bindings do not include QtWidgets
# in their __all__ list. And who knows what else.
#
# TODO: This needs a more robust implementation.
from Qt import QtWidgets
target_members = dict()
for name, module in locals().copy().items():
if name.startswith("_"):
continue
target_members[name] = dir(module)
missing = dict()
for module, members in reference_members.items():
for member in members:
# Ignore those that have no Qt 4-equivalent.
if member in excluded.get(module, []):
continue
if member not in target_members.get(module, []):
if module not in missing:
missing[module] = []
missing[module].append(member)
message = ""
for module, members in missing.items():
message += "\\n%s: \\n - %s" % (module, "\\n - ".join(members))
assert not missing, "{Binding} is missing members: %s" % message
"""
tests = list(test.format(Binding=binding,
binding=binding.lower())
for binding in ["PyQt5",
"PyQt4",
"PySide"])
with open("test_membership.py", "w") as f:
contents = header + "\n".join(tests)
f.write(contents)
# Do not consider these members.
#
# Some of these are either:
# 1. Unique to a particular binding
# 2. Unique to Qt 5
# 3. Not yet included in PySide2
#
# TODO: Clearly mark which are which. (3) should
# eventually be removed from this dictionary.
excluded = {
"QtCore": [
# missing from PySide
"Connection",
"QBasicMutex",
"QFileDevice",
"QItemSelectionRange",
"QJsonArray",
"QJsonDocument",
"QJsonParseError",
"QJsonValue",
"QMessageLogContext",
"QtInfoMsg",
"qInstallMessageHandler",
# missing from PyQt4
"ClassInfo",
"MetaFunction",
"QFactoryInterface",
"QSortFilterProxyModel",
"QStringListModel",
"QT_TRANSLATE_NOOP3",
"QT_TRANSLATE_NOOP_UTF8",
"__moduleShutdown",
"__version__", # (2) unique to PyQt
"__version_info__", # (2) unique to PyQt
"qAcos",
"qAsin",
"qAtan",
"qAtan2",
"qExp",
"qFabs",
"qFastCos",
"qFastSin",
"qFuzzyIsNull",
"qTan",
"qtTrId",
# missing from PyQt5
"SIGNAL",
"SLOT",
],
"QtGui": [
# missing from PySide
"QGuiApplication", # (2) unique to Qt 5
"QPagedPaintDevice",
"QSurface",
"QSurfaceFormat",
"QTouchDevice",
"QWindow", # (2) unique to Qt 5
# missing from PyQt4
"QAccessibleEvent",
"QToolBarChangeEvent",
# missing from PyQt5
"QMatrix",
"QPyTextObject",
"QStringListModel",
],
"QtWebKit": [
# missing from PyQt4
"WebCore",
# missing from PyQt5
"__doc__",
"__file__",
"__name__",
"__package__",
],
"QtScript": [
# missing from PyQt4
"QScriptExtensionInterface",
"QScriptExtensionPlugin",
"QScriptProgram",
"QScriptable",
# missing from PyQt5
"QScriptClass",
"QScriptClassPropertyIterator",
"QScriptContext",
"QScriptContextInfo",
"QScriptEngine",
"QScriptEngineAgent",
"QScriptString",
"QScriptValue",
"QScriptValueIterator",
"__doc__",
"__file__",
"__name__",
"__package__",
],
"QtNetwork": [
# missing from PyQt4
"QIPv6Address",
],
"QtPrintSupport": [
# PyQt4
"QAbstractPrintDialog",
"QPageSetupDialog",
"QPrintDialog",
"QPrintEngine",
"QPrintPreviewDialog",
"QPrintPreviewWidget",
"QPrinter",
"QPrinterInfo",
],
"QtWidgets": [
# PyQt4
"QTileRules",
# PyQt5
"QGraphicsItemAnimation",
"QTileRules",
],
"QtHelp": [
# PySide
"QHelpContentItem",
"QHelpContentModel",
"QHelpContentWidget",
"QHelpEngine",
"QHelpEngineCore",
"QHelpIndexModel",
"QHelpIndexWidget",
"QHelpSearchEngine",
"QHelpSearchQuery",
"QHelpSearchQueryWidget",
"QHelpSearchResultWidget",
],
"QtXml": [
# PySide
"QDomAttr",
"QDomCDATASection",
"QDomCharacterData",
"QDomComment",
"QDomDocument",
"QDomDocumentFragment",
"QDomDocumentType",
"QDomElement",
"QDomEntity",
"QDomEntityReference",
"QDomImplementation",
"QDomNamedNodeMap",
"QDomNode",
"QDomNodeList",
"QDomNotation",
"QDomProcessingInstruction",
"QDomText",
"QXmlAttributes",
"QXmlContentHandler",
"QXmlDTDHandler",
"QXmlDeclHandler",
"QXmlDefaultHandler",
"QXmlEntityResolver",
"QXmlErrorHandler",
"QXmlInputSource",
"QXmlLexicalHandler",
"QXmlLocator",
"QXmlNamespaceSupport",
"QXmlParseException",
"QXmlReader",
"QXmlSimpleReader",
],
}
if __name__ == '__main__':
build_membership()
build_tests()