-
Notifications
You must be signed in to change notification settings - Fork 1
/
parser.py
290 lines (217 loc) · 8.7 KB
/
parser.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
import ast
import os
from collections import defaultdict
from termcolor import colored
from tabulate import tabulate
import consts
from resolver import Resolver
from fields import Fields
def parse_module(filename):
with open(filename, 'rb') as f:
module = f.read()
try:
return filename, ast.parse(module)
except TypeError:
raise TypeError('Bad file {}'.format(filename))
def parse_directory(location):
for filename in walk(location):
yield parse_module(filename)
def walk(location):
for dirpath, dirs, files in os.walk(location):
for f in files:
filename, ext = os.path.splitext(f)
if ext == '.py':
yield os.path.join(dirpath, f)
class ClassVisitor(ast.NodeVisitor):
"""
Registers top-level class definitions into a ClassRegistry.
"""
def __init__(self, filename=None, classes=None, *args, **kwargs):
self.classes = classes
self.filename = filename
super(ClassVisitor, self).__init__(*args, **kwargs)
def visit_ClassDef(self, node):
self.classes.add(node, self.filename)
self.generic_visit(node)
class ClassDiff(object):
def __init__(self, added=None, removed=None):
self.added = added or []
self.removed = removed or []
def __contains__(self, thing):
return thing in self.added or thing in self.removed
# python 2 compat
def __nonzero__(self):
return bool(self.added + self.removed)
def __bool__(self):
return self.__nonzero__()
class ClassRegistry(object):
"""
Data store for classes.
ClassRegistry.nodes
- what is the node of this class?
- class_name:str -> class_node: ast.ClassDef
ClassRegistry.classes
- which classes are defined in this file?
- filename:str -> classes: [str]
ClassRegistry.class_source
- which files defined this class
- class_name:str -> filenames: [str]
"""
IGNORED_CLASSES = (
'Meta',
'Messages',
)
def __init__(self):
self.nodes = {}
self.classes = defaultdict(list)
self.class_source = defaultdict(list)
def add(self, node, filename):
node_name = node.name
if node_name in self.IGNORED_CLASSES:
return
if node_name in self.nodes:
# TODO: fix this case.. there are sometimes valid cases of
# conflicting names
msg = ('[WARNING] Re-definition of {} in {} that was previously '
'defined in {}\n').format(
node_name,
filename,
', '.join(self.class_source[node_name])
)
msg = colored(msg, consts.Colours.WARNING)
print(msg)
self.nodes[node_name] = node
self.classes[filename].append(node_name)
self.class_source[node_name].append(filename)
def difference(self, other):
"""Diffs two registries with self being the base."""
self_classes = set(self.nodes.keys())
other_classes = set(other.nodes.keys())
added = sorted(self_classes.difference(other_classes))
removed = sorted(other_classes.difference(self_classes))
return ClassDiff(added=added, removed=removed)
def get_classes_in_file(self, filename):
return self.classes[filename]
class DynamicFieldsVisitor(ast.NodeVisitor):
def visit_Assign(self, node):
pass
class FieldFinder(object):
def __init__(self, serializer_registry, view_registry):
self.serializer_registry = serializer_registry
self.view_registry = view_registry
self._dynamic_field_map = {}
self.memo_dict = {}
@classmethod
def is_init_method(cls, node):
return isinstance(node, ast.FunctionDef) and node.name == '__init__'
@classmethod
def is_meta(cls, node):
"""True iff node is the DRF Meta nested class."""
return isinstance(node, ast.ClassDef) and node.name == 'Meta'
@classmethod
def is_class_var(cls, node):
return isinstance(node, ast.Assign)
@property
def dynamic_fields(self):
if not self._dynamic_field_map:
nodes = self.view_registry.nodes
for class_name, class_node in nodes.iteritems():
props = {
'include_filters': None,
'expand_filters': None,
'exclude_filters': None,
'serializer_class': None
}
for node in class_node.body:
if not self.is_class_var(node):
continue
lhs, rhs = Resolver.resolve(node)
if lhs in props:
props[lhs] = self.resolve_view_var(rhs)
serializer_name = props.pop('serializer_class')
truncated_props = {
key: val
for key, val in props.iteritems()
if val
}
if truncated_props:
self._dynamic_field_map[serializer_name] = truncated_props
return self._dynamic_field_map
def resolve_view_var(self, node):
try:
return Resolver.resolve(node)
except AttributeError:
raise
def augment_field(self, previous, current):
previous.update_representations(current.representations)
return previous
def find_serializer_fields(self, serializer_name):
nodes = self.serializer_registry.nodes
if serializer_name in self.memo_dict:
return self.memo_dict[serializer_name]
class_node = nodes[serializer_name]
fields = Fields()
init_node = None
# Look at own class variables first, this trumps everything else
for node in class_node.body:
if self.is_class_var(node):
# explicit class var trumps Meta
fields.add(Resolver.drf_field_assignment(node), overwrite=True)
elif self.is_meta(node):
fields.extend(Resolver.drf_meta_fields(node))
elif self.is_init_method(node):
init_node = node
# add fields from bases, in left to right order. The bases of the base
# trumps the neighbour of the base if there's overlap.
for base in class_node.bases:
base = Resolver.resolve(base)
if base == 'object':
continue
if base not in nodes:
# TODO: ???
continue
base_class_vars = self.find_serializer_fields(base)
fields.extend(base_class_vars)
# Check for dynamic fields that were inherited from direct ancestors.
# TODO: Find a better way to support inheritance
parent_in_dynamic_fields = any(
getattr(parent_class, 'attr', None) in self.dynamic_fields or
getattr(parent_class, 'id', None) in self.dynamic_fields
for parent_class in class_node.bases)
# dynamic fields trump or augment existing fields
if serializer_name in self.dynamic_fields or parent_in_dynamic_fields:
if init_node:
dynamic_fields = Resolver.init_method(init_node)
for field_name, field in dynamic_fields.iteritems():
if field_name not in fields:
fields.add(field)
continue
previous_field = fields[field_name]
augmented_field = self.augment_field(previous_field, field)
fields.add(augmented_field, overwrite=True)
self.memo_dict[serializer_name] = fields
return fields
def difference(self, other):
return self.serializer_registry.difference(other.serializer_registry)
@classmethod
def crawl(cls, serializer_directory, view_directory, files=None):
files = files or []
serializer_registry = ClassRegistry()
view_registry = ClassRegistry()
for filename, tree in parse_directory(serializer_directory):
ClassVisitor(filename=filename, classes=serializer_registry).visit(tree)
for filename, tree in parse_directory(view_directory):
ClassVisitor(filename=filename, classes=view_registry).visit(tree)
for filename in files:
filename, tree = parse_module(filename)
ClassVisitor(filename=filename, classes=serializer_registry).visit(tree)
return cls(serializer_registry, view_registry)
def fmt_serializer(node, fields):
output = ('{}({})\n'
'{}\n')
table_data = tabulate(fields, headers="keys", tablefmt='grid')
return output.format(
node.name,
', '.join(Resolver.resolve(base) for base in node.bases),
table_data
)