forked from mottosso/Qt.py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
membership.py
218 lines (172 loc) · 6.8 KB
/
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
import os
import pkgutil
import json
from optparse import OptionParser
from functools import reduce
from pprint import pprint
# This is where all files are read from and saved to
PREFIX = '/Qt.py'
SKIP_MODULES = [
'PyQt4.uic.pyuic', # Problematic as it is executed on import
'PyQt5.uic.pyuic' # Problematic as it is executed on import
]
SKIP_MEMBERS = [
'qApp' # See main README.md on qApp
]
# Will contain all modules for the current binding
MODULES = []
# Flags from environment variables
QT_VERBOSE = bool(os.getenv("QT_VERBOSE"))
def read_json(filename):
"""Read JSON, return dict"""
with open(filename, 'r') as data_file:
return json.load(data_file)
def write_json(dictionary, filename):
"""Write dictionary to JSON"""
with open(filename, 'w') as data_file:
json.dump(dictionary, data_file, indent=4, sort_keys=True)
print('--> Wrote ' + os.path.basename(filename))
def compare(dicts):
"""Compare by iteration"""
common_members = {}
common_keys = reduce(lambda x, y: x & y, map(dict.keys, dicts))
for k in common_keys:
common_members[k] = list(
reduce(lambda x, y: x & y, [set(d[k]) for d in dicts]))
return common_members
def copy_qtgui_to_modules():
"""Copies the QtGui list of PySide/PyQt4 into QtWidgets"""
pyside_filepath = PREFIX + '/PySide.json'
pyqt4_filepath = PREFIX + '/PyQt4.json'
pyside = read_json(pyside_filepath)
pyqt4 = read_json(pyqt4_filepath)
# When Qt4 was moved to Qt5, they split QtGui into QtGui, QtWidgets, and
# QtPrintSupport.
pyside['QtWidgets'] = pyside['QtGui']
pyqt4['QtWidgets'] = pyqt4['QtGui']
pyside['QtPrintSupport'] = pyside['QtGui']
pyqt4['QtPrintSupport'] = pyqt4['QtGui']
write_json(pyside, pyside_filepath)
print('--> Copied QtGui to QtWidgets and QtPrintSupport for {0}'.format(
os.path.basename(pyside_filepath)))
write_json(pyqt4, pyqt4_filepath)
print('--> Copied QtGui to QtWidgets and QtPrintSupport for {0}'.format(
os.path.basename(pyqt4_filepath)))
def sort_common_members():
"""Sorts the keys and members"""
filename = PREFIX + '/common_members.json'
sorted_json_data = {}
json_data = read_json(filename)
all_keys = []
for key, value in json_data.items():
all_keys.append(key)
sorted_keys = sorted(all_keys)
for key in sorted_keys:
if len(json_data[key]) > 0:
# Only add modules which have common members
sorted_json_data[key] = sorted(json_data[key])
print('--> Sorted/cleaned ' + os.path.basename(filename))
write_json(sorted_json_data, filename)
def generate_common_members():
"""Generate JSON with commonly shared members"""
pyside = read_json(PREFIX + '/PySide.json')
pyside2 = read_json(PREFIX + '/PySide2.json')
pyqt4 = read_json(PREFIX + '/PyQt4.json')
pyqt5 = read_json(PREFIX + '/PyQt5.json')
dicts = [pyside, pyside2, pyqt4, pyqt5]
common_members = compare(dicts)
write_json(common_members, PREFIX + '/common_members.json')
if __name__ == '__main__':
# Parse commandline arguments
parser = OptionParser()
parser.add_option('--binding', dest='binding', metavar='BINDING')
parser.add_option(
'--copy-qtgui',
action='store_true',
dest='copy',
default=False)
parser.add_option(
'--generate-common-members',
action='store_true',
dest='generate',
default=False)
parser.add_option(
'--sort-common-members',
action='store_true',
dest='sort',
default=False)
(options, args) = parser.parse_args()
if options.copy:
copy_qtgui_to_modules()
elif options.generate:
generate_common_members()
elif options.sort:
sort_common_members()
else:
# Import <binding>
binding = __import__(options.binding)
for importer, modname, ispkg in pkgutil.walk_packages(
path=binding.__path__,
prefix=binding.__name__ + '.',
onerror=lambda x: None):
if modname not in SKIP_MODULES:
MODULES.append(modname)
basemodule = modname[:modname.rfind('.')]
submodule = modname[modname.rfind('.')+1:]
try:
import_statement = (
'from ' + basemodule + ' import ' + submodule)
exec(import_statement)
# print(import_statement)
except (ImportError, AttributeError, SyntaxError) as error:
# SyntaxError catched here because e.g. _port3
# running on Python 2...
print('WARNING: Skipped import', modname, error)
try:
raw_members = [] # avoid Hound errors
exec('raw_members = dir(' + submodule + ')')
members = []
for member in raw_members:
if member not in SKIP_MEMBERS and \
not member.startswith('_'):
try:
import_statement = (
'from ' + basemodule + '.' + submodule +
' import ' + member)
exec(import_statement)
# print(import_statement)
MODULES.append(modname + '.' + member)
except (AttributeError, SyntaxError) as error:
# SyntaxError catched here because e.g. _port3
# running on Python 2...
print('WARNING: Skipped import',
modname, error)
except (NameError) as error:
print('WARNING: Skipped dir() command', modname, error)
# Remove duplicates and sort
MODULES = sorted(list(set(MODULES)))
if QT_VERBOSE:
# Print all modules (for debugging)
for module in MODULES:
print(module)
# Create dictionary
members = {}
for module in MODULES:
key = module.split('.')[1]
if key not in members:
members[key] = []
try:
value = module.split('.')[2]
members[key].append(value)
except IndexError:
pass
# Sort and remove duplicates
sorted_members = {}
for key, value in members.copy().items():
sorted_members[key] = sorted(list(set(value)))
if QT_VERBOSE:
# Debug
pprint(sorted_members)
# Write to disk
filepath = PREFIX + '/' + binding.__name__ + '.json'
write_json(sorted_members, filepath)