OLD | NEW |
---|---|
(Empty) | |
1 #!/usr/bin/env python | |
2 # Copyright 2015 The Chromium Authors. All rights reserved. | |
3 # Use of this source code is governed by a BSD-style license that can be | |
4 # found in the LICENSE file. | |
5 | |
6 """Usage: collect_idls_into_json.py path_file.txt json_file.json | |
7 | |
8 This script collects and organizes interface information and that information du mps into json file. | |
9 """ | |
10 | |
11 import json | |
12 import os | |
13 import sys | |
14 import utilities | |
15 | |
16 from blink_idl_parser import parse_file, BlinkIDLParser | |
17 | |
18 _INTERFACE_CLASS_NAME = 'Interface' | |
19 _PARTIAL = 'Partial' | |
20 _STRIP_FILEPATH = '../chromium/src/third_party/WebKit' | |
21 _MEMBERS = ['Attributes', 'Operations', 'Consts'] | |
22 | |
23 | |
24 def get_definitions(paths): | |
25 """Returns IDL node. | |
26 Args: | |
27 paths: list of IDL file path | |
28 Returns: | |
29 a generator which yields IDL node objects | |
30 """ | |
31 parser = BlinkIDLParser() | |
32 for path in paths: | |
33 definitions = parse_file(parser, path) | |
34 for definition in definitions.GetChildren(): | |
35 yield definition | |
36 | |
37 | |
38 def is_implements(definition): | |
39 """Returns implement node. | |
Yuki
2015/10/05 10:03:21
Let's update the comments accordingly.
| |
40 Args: | |
41 definition: IDL node | |
42 Returns: | |
43 implement node | |
44 """ | |
45 if definition.GetClass() == 'Implements': | |
Yuki
2015/10/05 10:03:21
The code is equivalent to
x = definition.GetC
natsukoa
2015/10/06 07:10:22
Done.
| |
46 return True | |
47 else: | |
48 return False | |
49 | |
50 | |
51 def filter_non_partial(definition): | |
Yuki
2015/10/05 10:03:21
filter_non_partial => is_non_partial
natsukoa
2015/10/06 07:10:22
Done.
| |
52 """Returns boolean. | |
Yuki
2015/10/05 10:03:21
"Returns boolean" doesn't make much sense.
One of
natsukoa
2015/10/06 07:10:21
Done.
| |
53 Args: | |
54 definition: IDL node | |
55 Returns: | |
56 True: its 'Interface' class node and not 'partial' node | |
Yuki
2015/10/05 10:03:21
For example,
Returns:
True if |definitio
natsukoa
2015/10/06 07:10:22
Done.
| |
57 False: its not 'interface' class node and not 'partial' node | |
58 """ | |
59 if definition.GetClass() == _INTERFACE_CLASS_NAME and not definition.GetProp erty(_PARTIAL): | |
60 return True | |
61 else: | |
62 return False | |
63 | |
64 | |
65 def filter_partial(definition): | |
66 """Returns boolean. | |
67 Args: | |
68 definition: IDL node | |
69 Return: | |
70 True: it's 'interface' class node and 'partial' node | |
71 False: it's not 'interface' class node and 'partial' node | |
72 """ | |
73 if definition.GetClass() == _INTERFACE_CLASS_NAME and definition.GetProperty (_PARTIAL): | |
74 return True | |
75 else: | |
76 return False | |
77 | |
78 | |
79 def get_filepath(interface_node): | |
80 """Returns relative path under the WebKit directory which |interface_node| i s defined. | |
Yuki
2015/10/05 10:03:21
For example,
Returns the relative path to the
natsukoa
2015/10/06 07:10:22
Done.
| |
81 Args: | |
82 interface_node: IDL interface | |
83 Returns: | |
84 str which is |interface_node| file path | |
Yuki
2015/10/05 10:03:21
|interface_node|'s file path
natsukoa
2015/10/06 07:10:22
Done.
| |
85 """ | |
86 filename = interface_node.GetProperty('FILENAME') | |
87 return os.path.relpath(filename).strip(_STRIP_FILEPATH) | |
88 | |
89 | |
90 def get_const_node_list(interface_node): | |
91 """Returns Constant object. | |
Yuki
2015/10/05 10:03:21
object?
Better to use the same terminology.
Your
natsukoa
2015/10/06 07:10:22
Done.
| |
92 Args: | |
93 interface_node: interface node object | |
94 Returns: | |
95 list which is list of constant object | |
96 """ | |
97 return interface_node.GetListOf('Const') | |
98 | |
99 | |
100 def get_const_type(const_node): | |
101 """Returns constant's type. | |
102 Args: | |
103 const_node: constant node object | |
104 Returns: | |
105 node.GetChildren()[0].GetName(): str, constant object's name | |
Yuki
2015/10/05 10:03:21
The comment seems strange.
natsukoa
2015/10/06 07:10:22
Done.
| |
106 """ | |
107 return const_node.GetChildren()[0].GetName() | |
108 | |
109 | |
110 def get_const_value(const_node): | |
111 """Returns constant's value. | |
112 Args: | |
113 const_node: constant node object | |
114 Returns: | |
115 node.GetChildren()[1].GetName(): list, list of oparation object | |
116 """ | |
117 return const_node.GetChildren()[1].GetName() | |
118 | |
119 | |
120 def const_node_to_dict(const_node): | |
121 """Returns dictionary of constant object information. | |
122 Args: | |
123 const_nodes: list of interface node object which has constant | |
Yuki
2015/10/05 10:03:21
nodes => node
Yuki
2015/10/05 10:03:21
nodes => node
Update the description, too.
natsukoa
2015/10/06 07:10:22
Done.
| |
124 Returns: | |
125 a generator which yields dictionary of constant object information | |
126 """ | |
127 return { | |
128 'Name': const_node.GetName(), | |
129 'Type': get_const_type(const_node), | |
130 'Value': get_const_value(const_node), | |
131 'ExtAttributes': [extattr_node_to_dict(extattr) for extattr in get_extat tribute_node_list(const_node)], | |
132 } | |
133 | |
134 | |
135 def get_attribute_node_list(interface_node): | |
136 """Returns list of Attribute if the interface have one. | |
137 Args: | |
138 interface_node: interface node object | |
139 Returns: | |
140 a list of attribute | |
141 """ | |
142 return interface_node.GetListOf('Attribute') | |
143 | |
144 | |
145 def get_attribute_type(attribute_node): | |
146 """Returns type of attribute. | |
147 Args: | |
148 attribute_node: attribute node object | |
149 Returns: | |
150 str which is type of Attribute | |
151 """ | |
152 return attribute_node.GetOneOf('Type').GetChildren()[0].GetName() | |
153 | |
154 | |
155 get_operation_type = get_attribute_type | |
156 get_argument_type = get_attribute_type | |
157 | |
158 | |
159 def attribute_node_to_dict(attribute_node): | |
160 """Returns dictioary of attribute object information. | |
161 Args: | |
162 attribute_nodes: list of attribute node object | |
Yuki
2015/10/05 10:03:21
attribute_nodes = > attribute_node
natsukoa
2015/10/06 07:10:22
Done.
| |
163 Returns: | |
164 a generator which yields dictionary of attribite information | |
165 """ | |
166 return { | |
167 'Name': attribute_node.GetName(), | |
168 'Type': get_attribute_type(attribute_node), | |
169 'ExtAttributes': [extattr_node_to_dict(extattr) for extattr in get_extat tribute_node_list(attribute_node)], | |
170 'Readonly': attribute_node.GetProperty('READONLY', default=False), | |
171 'Static': attribute_node.GetProperty('STATIC', default=False), | |
172 } | |
173 | |
174 | |
175 def get_operation_node_list(interface_node): | |
176 """Returns Operations object under the interface. | |
177 Args: | |
178 interface: interface node object | |
179 Returns: | |
180 list which is list of oparation object | |
181 """ | |
182 return interface_node.GetListOf('Operation') | |
183 | |
184 | |
185 def get_argument_node_list(operation_node): | |
186 """Returns Argument object under the operation object. | |
187 Args: | |
188 operation_node: operation node object | |
189 Returns: | |
190 list of argument object | |
191 """ | |
192 return operation_node.GetOneOf('Arguments').GetListOf('Argument') | |
193 | |
194 | |
195 def argument_node_to_dict(argument_node): | |
196 """Returns generator which yields dictionary of Argument object information. | |
197 Args: | |
198 arguments: list of argument node object | |
199 Returns: | |
200 a generator which yields dictionary of argument information | |
201 """ | |
202 return { | |
203 'Name': argument_node.GetName(), | |
204 'Type': get_argument_type(argument_node), | |
205 } | |
206 | |
207 | |
208 def get_operation_name(operation_node): | |
209 """Returns openration object name. | |
210 Args: | |
211 operation_node: operation node object | |
212 Returns: | |
213 str which is operation's name | |
214 """ | |
215 if operation_node.GetProperty('GETTER'): | |
216 return '__getter__' | |
217 elif operation_node.GetProperty('SETTER'): | |
218 return '__setter__' | |
219 elif operation_node.GetProperty('DELETER'): | |
220 return '__deleter__' | |
221 else: | |
222 return operation_node.GetName() | |
223 | |
224 | |
225 def operation_node_to_dict(operation_node): | |
226 """Returns dictionary of Operation object information. | |
227 Args: | |
228 operation_nodes: list of operation node object | |
229 Returns: | |
230 dictionary of operation's informantion | |
231 """ | |
232 return { | |
233 'Name': get_operation_name(operation_node), | |
234 'Arguments': [argument_node_to_dict(argument) for argument in get_argume nt_node_list(operation_node) if argument_node_to_dict(argument)], | |
235 'Type': get_operation_type(operation_node), | |
236 'ExtAttributes': [extattr_node_to_dict(extattr) for extattr in get_extat tribute_node_list(operation_node)], | |
237 'Static': operation_node.GetProperty('STATIC', default=False), | |
238 } | |
239 | |
240 | |
241 def get_extattribute_node_list(node): | |
242 """Returns list of ExtAttribute. | |
243 Args: | |
244 IDL node object | |
245 Returns: | |
246 a list of ExtAttrbute | |
247 """ | |
248 if node.GetOneOf('ExtAttributes'): | |
249 return node.GetOneOf('ExtAttributes').GetListOf('ExtAttribute') | |
250 else: | |
251 return [] | |
252 | |
253 | |
254 def extattr_node_to_dict(extattr): | |
255 """Returns a generator which yields Extattribute's object dictionary | |
256 Args: | |
257 extattribute_nodes: list of extended attribute | |
258 Returns: | |
259 a generator which yields extattribute dictionary | |
260 """ | |
261 return { | |
262 'Name': extattr.GetName(), | |
263 } | |
264 | |
265 | |
266 def get_inherit_node(interface_node): | |
267 if interface_node.GetListOf('Inherit'): | |
268 return interface_node.GetListOf('Inherit') | |
269 else: | |
270 return [] | |
271 | |
272 | |
273 def inherit_node_to_dict(inherit): | |
274 return {'Parent': inherit.GetName()} | |
275 | |
276 | |
277 def interface_node_to_dict(interface_node): | |
278 """Returns dictioary whose key is interface name and value is interface dict ioary. | |
279 Args: | |
280 interface_node: interface node | |
281 Returns: | |
282 dictionary, {interface name: interface node dictionary} | |
283 """ | |
284 return { | |
285 'Name': interface_node.GetName(), | |
286 'FilePath': get_filepath(interface_node), | |
287 'Consts': [const_node_to_dict(const) for const in get_const_node_list(in terface_node)], | |
288 'Attributes': [attribute_node_to_dict(attr) for attr in get_attribute_no de_list(interface_node) if attribute_node_to_dict(attr)], | |
289 'Operations': [operation_node_to_dict(operation) for operation in get_op eration_node_list(interface_node) if operation_node_to_dict(operation)], | |
290 'ExtAttributes': [extattr_node_to_dict(extattr) for extattr in get_extat tribute_node_list(interface_node)], | |
291 'Inherit': [inherit_node_to_dict(inherit) for inherit in get_inherit_nod e(interface_node)], | |
292 } | |
293 | |
294 | |
295 def merge_partial_dicts(interfaces_dict, partials_dict): | |
296 """Returns interface information dictioary. | |
297 Args: | |
298 interfaces_dict: interface node dictionary | |
299 partial_dict: partial interface node dictionary | |
300 Returns: | |
301 a dictronary merged with interfaces_dict and partial_dict | |
302 """ | |
303 for interface_name, partial in partials_dict.iteritems(): | |
304 interface = interfaces_dict.get(interface_name) | |
305 if not interface: | |
306 continue | |
307 else: | |
308 for member in _MEMBERS: | |
309 interface[member].extend(partial.get(member)) | |
310 interface.setdefault('Partial_FilePaths', []).append(partial['FilePa th']) | |
311 return interfaces_dict | |
312 | |
313 | |
314 def merge_implement_node(interfaces_dict, implement_nodes): | |
315 """Returns dict of interface information combined with referenced interface information | |
316 Args: | |
317 interfaces_dict: dict of interface information | |
318 implement_nodes: list of implemented interface node | |
319 Returns: | |
320 interfaces_dict: dict of interface information combine into implements nod e | |
321 """ | |
322 for implement in implement_nodes: | |
323 reference = implement.GetProperty('REFERENCE') | |
324 implement = implement.GetName() | |
325 if not reference: | |
326 continue | |
327 else: | |
328 for member in _MEMBERS: | |
329 interfaces_dict[implement][member].extend(interfaces_dict[refere nce].get(member)) | |
330 return interfaces_dict | |
331 | |
332 | |
333 # TODO(natsukoa): Remove indent | |
334 def export_to_jsonfile(dictionary, json_file): | |
335 """Returns jsonfile which is dumped each interface information dictionary to json. | |
336 Args: | |
337 dictioary: interface dictionary | |
338 json_file: json file for output | |
339 Returns: | |
340 json file which is contained each interface node dictionary | |
341 """ | |
342 with open(json_file, 'w') as f: | |
343 json.dump(dictionary, f, sort_keys=True, indent=4) | |
344 | |
345 | |
346 def main(args): | |
347 path_file = args[0] | |
348 json_file = args[1] | |
349 path_list = utilities.read_file_to_list(path_file) | |
350 implement_nodes = [definition | |
351 for definition in get_definitions(path_list) | |
352 if is_implements(definition)] | |
353 interfaces_dict = {definition.GetName(): interface_node_to_dict(definition) | |
354 for definition in get_definitions(path_list) | |
355 if filter_non_partial(definition)} | |
356 partials_dict = {definition.GetName(): interface_node_to_dict(definition) | |
357 for definition in get_definitions(path_list) | |
358 if filter_partial(definition)} | |
359 dictionary = merge_partial_dicts(interfaces_dict, partials_dict) | |
360 interfaces_dict = merge_implement_node(interfaces_dict, implement_nodes) | |
361 export_to_jsonfile(dictionary, json_file) | |
362 | |
363 | |
364 if __name__ == '__main__': | |
365 main(sys.argv[1:]) | |
OLD | NEW |