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 _CLASS_NAME = 'Interface' | |
Yuki
2015/10/05 04:33:43
_CLASS_NAME => _INTERFACE_CLASS_NAME or something
natsukoa
2015/10/05 08:59:04
Done.
| |
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(debug=False) | |
32 for path in paths: | |
33 definitions = parse_file(parser, path) | |
34 for definition in definitions.GetChildren(): | |
35 yield definition | |
36 | |
37 | |
38 def get_interface_node(definition): | |
Yuki
2015/10/05 04:33:44
get_interface_node => **is_**interface_node
natsukoa
2015/10/05 08:59:04
I remove get_interface_node().
filter_partial and
| |
39 """Returns interface node. | |
40 Args: | |
41 definition: IDL node | |
42 Returns: | |
43 interface node | |
44 """ | |
45 if definition.GetClass() == _CLASS_NAME: | |
46 return definition | |
47 else: | |
48 return None | |
49 | |
50 | |
51 def is_implements(definition): | |
52 """Returns implement node. | |
53 Args: | |
54 definition: IDL node | |
55 Returns: | |
56 implement node | |
57 """ | |
58 if definition.GetClass() == 'Implements': | |
59 return True | |
60 else: | |
61 return False | |
62 | |
63 | |
64 def filter_partial(interface_nodes): | |
65 """Returns partial interface node. | |
66 Args: | |
67 interface_nodes: a generator which is interface IDL | |
68 Return: | |
69 a generator which yields partial interface node | |
70 """ | |
71 for interface_node in interface_nodes: | |
72 if interface_node.GetProperty(_PARTIAL): | |
73 yield interface_node | |
74 | |
75 | |
76 def filter_non_partial(interface_nodes): | |
77 """Returns interface node. | |
78 Args: | |
79 interface_nodes: a generator which is interface IDL node | |
80 Returns: | |
81 a generator which yields interface node | |
82 """ | |
83 for interface_node in interface_nodes: | |
84 if not interface_node.GetProperty(_PARTIAL): | |
85 yield interface_node | |
86 | |
87 | |
88 def get_filepath(interface_node): | |
89 """Returns relative path under the WebKit directory which |interface_node| i s defined. | |
90 Args: | |
91 interface_node: IDL interface | |
92 Returns: | |
93 str which is |interface_node| file path | |
94 """ | |
95 filename = interface_node.GetProperty('FILENAME') | |
96 return os.path.relpath(filename).strip(_STRIP_FILEPATH) | |
97 | |
98 | |
99 def get_const_node(interface_node): | |
Yuki
2015/10/05 04:33:43
get_const_node**_list**.
Ditto for all other case
natsukoa
2015/10/05 08:59:04
Done.
| |
100 """Returns Constant object. | |
101 Args: | |
102 interface_node: interface node object | |
103 Returns: | |
104 list which is list of constant object | |
105 """ | |
106 return interface_node.GetListOf('Const') | |
107 | |
108 | |
109 def get_const_type(const_node): | |
110 """Returns constant's type. | |
111 Args: | |
112 const_node: constant node object | |
113 Returns: | |
114 node.GetChildren()[0].GetName(): str, constant object's name | |
115 """ | |
116 return const_node.GetChildren()[0].GetName() | |
117 | |
118 | |
119 def get_const_value(const_node): | |
120 """Returns constant's value. | |
121 Args: | |
122 const_node: constant node object | |
123 Returns: | |
124 node.GetChildren()[1].GetName(): list, list of oparation object | |
125 """ | |
126 return const_node.GetChildren()[1].GetName() | |
127 | |
128 | |
129 def const_node_to_dict(const_node): | |
130 """Returns dictionary of constant object information. | |
131 Args: | |
132 const_nodes: list of interface node object which has constant | |
133 Returns: | |
134 a generator which yields dictionary of constant object information | |
135 """ | |
136 return { | |
137 'Name': const_node.GetName(), | |
138 'Type': get_const_type(const_node), | |
139 'Value': get_const_value(const_node), | |
140 'ExtAttributes': [extattr_node_to_dict(extattr) for extattr in get_extat tribute_node(const_node)], | |
141 } | |
142 | |
143 | |
144 def get_attribute_node(interface_node): | |
145 """Returns list of Attribute if the interface have one. | |
146 Args: | |
147 interface_node: interface node object | |
148 Returns: | |
149 a list of attribute | |
150 """ | |
151 return interface_node.GetListOf('Attribute') | |
152 | |
153 | |
154 def get_attribute_type(attribute_node): | |
155 """Returns type of attribute. | |
156 Args: | |
157 attribute_node: attribute node object | |
158 Returns: | |
159 str which is type of Attribute | |
160 """ | |
161 return attribute_node.GetOneOf('Type').GetChildren()[0].GetName() | |
162 | |
163 | |
164 get_operation_type = get_attribute_type | |
165 get_argument_type = get_attribute_type | |
166 | |
167 | |
168 def attribute_node_to_dict(attribute_node): | |
169 """Returns dictioary of attribute object information. | |
170 Args: | |
171 attribute_nodes: list of attribute node object | |
172 Returns: | |
173 a generator which yields dictionary of attribite information | |
174 """ | |
175 return { | |
176 'Name': attribute_node.GetName(), | |
177 'Type': get_attribute_type(attribute_node), | |
178 'ExtAttributes': [extattr_node_to_dict(extattr) for extattr in get_extat tribute_node(attribute_node)], | |
179 'Readonly': attribute_node.GetProperty('READONLY', default=False), | |
180 'Static': attribute_node.GetProperty('STATIC', default=False), | |
181 } | |
182 | |
183 | |
184 def get_operation_node(interface_node): | |
185 """Returns Operations object under the interface. | |
186 Args: | |
187 interface: interface node object | |
188 Returns: | |
189 list which is list of oparation object | |
190 """ | |
191 return interface_node.GetListOf('Operation') | |
192 | |
193 | |
194 def get_argument_node(operation_node): | |
195 """Returns Argument object under the operation object. | |
196 Args: | |
197 operation_node: operation node object | |
198 Returns: | |
199 list of argument object | |
200 """ | |
201 return operation_node.GetOneOf('Arguments').GetListOf('Argument') | |
202 | |
203 | |
204 def argument_node_to_dict(argument_node): | |
205 """Returns generator which yields dictionary of Argument object information. | |
206 Args: | |
207 arguments: list of argument node object | |
208 Returns: | |
209 a generator which yields dictionary of argument information | |
210 """ | |
211 return { | |
212 'Name': argument_node.GetName(), | |
213 'Type': get_argument_type(argument_node), | |
214 } | |
215 | |
216 | |
217 def get_operation_name(operation_node): | |
218 """Returns openration object name. | |
219 Args: | |
220 operation_node: operation node object | |
221 Returns: | |
222 str which is operation's name | |
223 """ | |
224 if operation_node.GetProperty('GETTER'): | |
225 return '__getter__' | |
226 elif operation_node.GetProperty('SETTER'): | |
227 return '__setter__' | |
228 elif operation_node.GetProperty('DELETER'): | |
229 return '__deleter__' | |
230 else: | |
231 return operation_node.GetName() | |
232 | |
233 | |
234 def operation_node_to_dict(operation_node): | |
235 """Returns dictionary of Operation object information. | |
236 Args: | |
237 operation_nodes: list of operation node object | |
238 Returns: | |
239 dictionary of operation's informantion | |
240 """ | |
241 return { | |
242 'Name': get_operation_name(operation_node), | |
243 'Arguments': [argument_node_to_dict(argument) for argument in get_argume nt_node(operation_node) if argument_node_to_dict(argument)], | |
244 'Type': get_operation_type(operation_node), | |
245 'ExtAttributes': [extattr_node_to_dict(extattr) for extattr in get_extat tribute_node(operation_node)], | |
246 'Static': operation_node.GetProperty('STATIC', default=False), | |
247 } | |
248 | |
249 | |
250 def get_extattribute_node(node): | |
251 """Returns list of ExtAttribute. | |
252 Args: | |
253 IDL node object | |
254 Returns: | |
255 a list of ExtAttrbute | |
256 """ | |
257 if node.GetOneOf('ExtAttributes'): | |
258 return node.GetOneOf('ExtAttributes').GetListOf('ExtAttribute') | |
259 else: | |
260 return [] | |
261 | |
262 | |
263 def extattr_node_to_dict(extattr): | |
264 """Returns a generator which yields Extattribute's object dictionary | |
265 Args: | |
266 extattribute_nodes: list of extended attribute | |
267 Returns: | |
268 a generator which yields extattribute dictionary | |
269 """ | |
270 return { | |
271 'Name': extattr.GetName(), | |
272 } | |
273 | |
274 | |
275 def get_inherit_node(interface_node): | |
276 if interface_node.GetListOf('Inherit'): | |
277 return interface_node.GetListOf('Inherit') | |
278 else: | |
279 return [] | |
280 | |
281 | |
282 def inherit_node_to_dict(inherit): | |
283 return {'Parent': inherit.GetName()} | |
284 | |
285 | |
286 def interface_node_to_dict(interface_node): | |
287 """Returns dictioary whose key is interface name and value is interface dict ioary. | |
288 Args: | |
289 interface_node: interface node | |
290 Returns: | |
291 dictionary, {interface name: interface node dictionary} | |
292 """ | |
293 return { | |
294 'Name': interface_node.GetName(), | |
295 'FilePath': get_filepath(interface_node), | |
296 'Consts': [const_node_to_dict(const) for const in get_const_node(interfa ce_node)], | |
297 'Attributes': [attribute_node_to_dict(attr) for attr in get_attribute_no de(interface_node) if attribute_node_to_dict(attr)], | |
298 'Operations': [operation_node_to_dict(operation) for operation in get_op eration_node(interface_node) if operation_node_to_dict(operation)], | |
299 'ExtAttributes': [extattr_node_to_dict(extattr) for extattr in get_extat tribute_node(interface_node)], | |
300 'Inherit': [inherit_node_to_dict(inherit) for inherit in get_inherit_nod e(interface_node)], | |
301 } | |
302 | |
303 | |
304 def merge_partial_dicts(interfaces_dict, partials_dict): | |
305 """Returns interface information dictioary. | |
306 Args: | |
307 interfaces_dict: interface node dictionary | |
308 partial_dict: partial interface node dictionary | |
309 Returns: | |
310 a dictronary merged with interfaces_dict and partial_dict | |
311 """ | |
312 for interface_name, partial in partials_dict.iteritems(): | |
313 interface = interfaces_dict.get(interface_name) | |
314 if not interface: | |
315 continue | |
316 else: | |
317 for member in _MEMBERS: | |
318 interface[member].extend(partial.get(member)) | |
319 interface.setdefault('Partial_FilePaths', []).append(partial['FilePa th']) | |
320 return interfaces_dict | |
321 | |
322 | |
323 def merge_implement_nodes(interfaces_dict, implement_nodes): | |
324 """Returns dict of interface information combined with referenced interface information | |
325 Args: | |
326 interfaces_dict: dict of interface information | |
327 implement_nodes: list of implemented interface node | |
328 Returns: | |
329 interfaces_dict: dict of interface information combine into implements nod e | |
330 """ | |
331 for implement in implement_nodes: | |
332 reference = implement.GetProperty('REFERENCE') | |
333 implement = implement.GetName() | |
334 if not reference: | |
335 continue | |
336 else: | |
337 for member in _MEMBERS: | |
338 interfaces_dict[implement][member].extend(interfaces_dict[refere nce].get(member)) | |
339 return interfaces_dict | |
340 | |
341 | |
342 # TODO(natsukoa): Remove indent | |
343 def export_to_jsonfile(dictionary, json_file): | |
344 """Returns jsonfile which is dumped each interface information dictionary to json. | |
345 Args: | |
346 dictioary: interface dictionary | |
347 json_file: json file for output | |
348 Returns: | |
349 json file which is contained each interface node dictionary | |
350 """ | |
351 with open(json_file, 'w') as f: | |
352 json.dump(dictionary, f, sort_keys=True, indent=4) | |
353 | |
354 | |
355 def main(args): | |
356 path_file = args[0] | |
357 json_file = args[1] | |
358 file_to_list = utilities.read_file_to_list(path_file) | |
Yuki
2015/10/05 04:33:43
file_to_list => file_list or path_list
natsukoa
2015/10/05 08:59:04
Done.
| |
359 implement_nodes = [definition | |
360 for definition in get_definitions(file_to_list) | |
361 if is_implements(definition)] | |
362 interfaces_dict = {get_interface_node(definition).GetName(): interface_node_ to_dict(get_interface_node(definition)) | |
363 for definition in filter_non_partial(get_definitions(file _to_list)) | |
364 if get_interface_node(definition)} | |
365 partials_dict = {get_interface_node(definition).GetName(): interface_node_to _dict(get_interface_node(definition)) | |
366 for definition in filter_partial(get_definitions(file_to_li st)) | |
367 if get_interface_node(definition)} | |
368 dictionary = merge_partial_dicts(interfaces_dict, partials_dict) | |
369 interfaces_dict = merge_implement_nodes(interfaces_dict, implement_nodes) | |
370 export_to_jsonfile(dictionary, json_file) | |
371 | |
372 | |
373 if __name__ == '__main__': | |
374 main(sys.argv[1:]) | |
OLD | NEW |