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 | |
bashi
2015/09/30 03:13:15
Please add a description (What this script does).
natsukoa
2015/10/05 01:47:11
Done.
| |
7 """ | |
8 | |
9 import os | |
10 import sys | |
11 import json | |
bashi
2015/09/30 03:13:15
nit: sort imports in alphabetical order.
natsukoa
2015/10/05 01:47:10
Done.
| |
12 import utilities | |
13 | |
14 from blink_idl_parser import parse_file, BlinkIDLParser | |
15 | |
16 _class_name = 'Interface' | |
bashi
2015/09/30 03:13:15
nit: use upper case for consts.
_class_name -> _C
natsukoa
2015/10/05 01:47:10
Done.
| |
17 _partial = 'Partial' | |
bashi
2015/09/30 03:13:16
_partial -> _PARTIAL
natsukoa
2015/10/05 01:47:11
Done.
| |
18 | |
19 | |
20 def get_definitions(paths): | |
21 """Returns interface IDL node. | |
bashi
2015/09/30 03:13:15
Is this function used? If not, remove it.
natsukoa
2015/10/05 01:47:11
I forgot to change the description, but I'm using
| |
22 Args: | |
23 paths: list of IDL file path | |
24 Returns: | |
25 a generator which yields interface node objects | |
26 """ | |
27 parser = BlinkIDLParser(debug=False) | |
bashi
2015/09/30 03:13:16
nit: you can omit debug=False
natsukoa
2015/10/05 01:47:10
Really? I couldn't remove it because debug informa
bashi
2015/10/05 07:23:05
The default value of |debug| is False, which means
natsukoa
2015/10/05 08:59:04
oh...
I got it.
| |
28 for path in paths: | |
29 definitions = parse_file(parser, path) | |
30 for definition in definitions.GetChildren(): | |
31 yield definition | |
32 | |
33 | |
34 def get_interface_node(definition): | |
bashi
2015/09/30 03:13:15
Please add a comment to describe what this functio
natsukoa
2015/10/05 01:47:11
Done.
| |
35 if definition.GetClass() == _class_name: | |
36 return definition | |
bashi
2015/09/30 03:13:16
What should be a return value when definition.GetC
| |
37 | |
38 | |
39 def get_implements_node(definition): | |
40 if definition.GetClass() == 'Implements': | |
41 return definition | |
bashi
2015/09/30 03:13:16
ditto.
natsukoa
2015/10/05 01:47:11
Done.
| |
42 | |
43 | |
44 def get_filepath(interface_node): | |
45 """Returns relative path under the WebKit directory which |interface_node| i s defined. | |
46 Args: | |
47 interface_node: IDL interface | |
48 Returns: | |
49 str which is |interface_node| file path | |
50 """ | |
51 filename = interface_node.GetProperty('FILENAME') | |
52 return os.path.relpath(filename).strip('../chromium/src/third_party/WebKit') | |
53 | |
54 | |
55 def filter_partial(interface_nodes): | |
56 """Returns partial interface node. | |
57 Args: | |
58 interface_nodes: a generator which is interface IDL | |
59 Return: | |
60 a generator which yields partial interface node | |
61 """ | |
62 for interface_node in interface_nodes: | |
63 if interface_node.GetProperty(_partial): | |
64 yield interface_node | |
65 | |
66 | |
67 def filter_non_partial(interface_nodes): | |
68 """Returns interface node. | |
69 Args: | |
70 interface_nodes: a generator which is interface IDL node | |
71 Returns: | |
72 a generator which yields interface node | |
73 """ | |
74 for interface_node in interface_nodes: | |
75 if not interface_node.GetProperty(_partial): | |
76 yield interface_node | |
77 | |
78 | |
79 def get_attribute_node(interface_node): | |
Yuki
2015/09/30 04:24:21
I'd recommend to name this function get_attribute_
Yuki
2015/10/05 04:33:43
Could you address this comment, too?
natsukoa
2015/10/05 08:59:04
Done.
| |
80 """Returns list of Attribute if the interface have one. | |
81 Args: | |
82 interface_node: interface node object | |
83 Returns: | |
84 a list of attribute | |
85 """ | |
86 return interface_node.GetListOf('Attribute') | |
87 | |
88 | |
89 def get_attribute_type(attribute_node): | |
90 """Returns type of attribute. | |
91 Args: | |
92 attribute_node: attribute node object | |
93 Returns: | |
94 str which is type of Attribute | |
95 """ | |
96 return attribute_node.GetOneOf('Type').GetChildren()[0].GetName() | |
97 | |
98 | |
99 get_operation_type = get_attribute_type | |
100 get_argument_type = get_attribute_type | |
101 | |
102 | |
103 def get_extattribute_node(node): | |
104 """Returns list of ExtAttribute. | |
105 Args: | |
106 IDL node object | |
107 Returns: | |
108 a list of ExtAttrbute | |
109 """ | |
110 if node.GetOneOf('ExtAttributes'): | |
111 return node.GetOneOf('ExtAttributes').GetListOf('ExtAttribute') | |
112 else: | |
113 return [] | |
114 | |
115 | |
116 def extattr_node_to_dict(extattr): | |
117 """Returns a generator which yields Extattribute's object dictionary | |
118 Args: | |
119 extattribute_nodes: list of extended attribute | |
120 Returns: | |
121 a generator which yields extattribute dictionary | |
122 """ | |
123 return { | |
124 'Name': extattr.GetName(), | |
125 } | |
126 | |
127 | |
128 def attribute_node_to_dict(attribute_node): | |
129 """Returns dictioary of attribute object information. | |
130 Args: | |
131 attribute_nodes: list of attribute node object | |
132 Returns: | |
133 a generator which yields dictionary of attribite information | |
134 """ | |
135 return { | |
136 'Name': attribute_node.GetName(), | |
137 'Type': get_attribute_type(attribute_node), | |
138 'ExtAttributes': [extattr_node_to_dict(extattr) for extattr in get_extat tribute_node(attribute_node) if extattr_node_to_dict(extattr)], | |
Yuki
2015/09/30 04:24:22
Do we need |if extattr_node_to_dict(extattr)| ?
D
natsukoa
2015/10/05 01:47:10
I checked the difference between with |if extattr_
| |
139 'Readonly': attribute_node.GetProperty('READONLY', default=False), | |
140 'Static': attribute_node.GetProperty('STATIC', default=False), | |
141 } | |
142 | |
143 | |
144 def get_operation_node(interface_node): | |
145 """Returns Operations object under the interface. | |
146 Args: | |
147 interface: interface node object | |
148 Returns: | |
149 list which is list of oparation object | |
150 """ | |
151 return interface_node.GetListOf('Operation') | |
152 | |
153 | |
154 def get_argument_node(operation_node): | |
155 """Returns Argument object under the operation object. | |
156 Args: | |
157 operation_node: operation node object | |
158 Returns: | |
159 list of argument object | |
160 """ | |
161 return operation_node.GetOneOf('Arguments').GetListOf('Argument') | |
162 | |
163 | |
164 def argument_node_to_dict(argument_node): | |
165 """Returns generator which yields dictionary of Argument object information. | |
166 Args: | |
167 arguments: list of argument node object | |
168 Returns: | |
169 a generator which yields dictionary of argument information | |
170 """ | |
171 return { | |
172 'Name': argument_node.GetName(), | |
173 'Type': get_argument_type(argument_node), | |
174 } | |
175 | |
176 | |
177 def get_operation_name(operation_node): | |
178 """Returns openration object name. | |
179 Args: | |
180 operation_node: operation node object | |
181 Returns: | |
182 str which is operation's name | |
183 """ | |
184 if operation_node.GetProperty('GETTER'): | |
185 return '__getter__' | |
186 elif operation_node.GetProperty('SETTER'): | |
187 return '__setter__' | |
188 elif operation_node.GetProperty('DELETER'): | |
189 return '__deleter__' | |
190 else: | |
191 return operation_node.GetName() | |
192 | |
193 | |
194 def operation_node_to_dict(operation_node): | |
195 """Returns dictionary of Operation object information. | |
196 Args: | |
197 operation_nodes: list of operation node object | |
198 Returns: | |
199 dictionary of operation's informantion | |
200 """ | |
201 return { | |
202 'Name': get_operation_name(operation_node), | |
203 'Arguments': [argument_node_to_dict(argument) for argument in get_argume nt_node(operation_node) if argument_node_to_dict(argument)], | |
204 'Type': get_operation_type(operation_node), | |
205 'ExtAttributes': [extattr_node_to_dict(extattr) for extattr in get_extat tribute_node(operation_node) if extattr_node_to_dict(extattr)], | |
206 'Static': operation_node.GetProperty('STATIC', default=False), | |
207 } | |
208 | |
209 | |
210 def get_inherit_node(interface_node): | |
Yuki
2015/09/30 04:24:21
By definition, there must be at most one parent in
Yuki
2015/10/05 04:33:43
Could you address this comment, too?
natsukoa
2015/10/05 08:59:04
I know there is no interface that have number of i
Yuki
2015/10/05 10:03:20
If you think so, then you should reject such wrong
| |
211 if interface_node.GetListOf('Inherit'): | |
212 return interface_node.GetListOf('Inherit') | |
213 else: | |
214 return [] | |
215 | |
216 | |
217 def inherit_node_to_dict(inherit): | |
218 return {'Name': inherit.GetName()} | |
219 | |
220 | |
221 def get_const_node(interface_node): | |
222 """Returns Constant object. | |
223 Args: | |
224 interface_node: interface node object | |
225 Returns: | |
226 list which is list of constant object | |
227 """ | |
228 return interface_node.GetListOf('Const') | |
229 | |
230 | |
231 def get_const_type(const_node): | |
232 """Returns constant's type. | |
233 Args: | |
234 const_node: constant node object | |
235 Returns: | |
236 node.GetChildren()[0].GetName(): str, constant object's name | |
237 """ | |
238 return const_node.GetChildren()[0].GetName() | |
239 | |
240 | |
241 def get_const_value(const_node): | |
242 """Returns constant's value. | |
243 Args: | |
244 const_node: constant node object | |
245 Returns: | |
246 node.GetChildren()[1].GetName(): list, list of oparation object | |
247 """ | |
248 return const_node.GetChildren()[1].GetName() | |
249 | |
250 | |
251 def const_node_to_dict(const_node): | |
252 """Returns dictionary of constant object information. | |
253 Args: | |
254 const_nodes: list of interface node object which has constant | |
255 Returns: | |
256 a generator which yields dictionary of constant object information | |
257 """ | |
258 return { | |
259 'Name': const_node.GetName(), | |
260 'Type': get_const_type(const_node), | |
261 'Value': get_const_value(const_node), | |
262 'ExtAttributes': [extattr_node_to_dict(extattr) for extattr in get_extat tribute_node(const_node) if extattr_node_to_dict(extattr)], | |
263 } | |
264 | |
265 | |
266 def interface_node_to_dict(interface_node): | |
267 """Returns dictioary whose key is interface name and value is interface dict ioary. | |
268 Args: | |
269 interface_node: interface node | |
270 Returns: | |
271 dictionary, {interface name: interface node dictionary} | |
272 """ | |
273 return { | |
274 'Name': interface_node.GetName(), | |
275 'Attributes': [attribute_node_to_dict(attr) for attr in get_attribute_no de(interface_node) if attribute_node_to_dict(attr)], | |
276 'Operations': [operation_node_to_dict(operation) for operation in get_op eration_node(interface_node) if operation_node_to_dict(operation)], | |
277 'ExtAttributes': [extattr_node_to_dict(extattr) for extattr in get_extat tribute_node(interface_node) if extattr_node_to_dict(extattr)], | |
278 'Consts': [const_node_to_dict(const) for const in get_const_node(interfa ce_node)], | |
Yuki
2015/09/30 04:24:21
Please choose the order carefully. Why are they s
natsukoa
2015/10/05 01:47:11
I changed the order that
1. Constants
2. Attribu
| |
279 'Inherit': [inherit_node_to_dict(inherit) for inherit in get_inherit_nod e(interface_node)], | |
280 'FilePath': get_filepath(interface_node), | |
281 } | |
282 | |
283 | |
284 def merge_dict(interface_dict, partial_dict): | |
bashi
2015/09/30 03:13:16
merge_dict -> merge_partial_dicts ?
interface_dict
natsukoa
2015/10/05 01:47:11
Done.
| |
285 """Returns interface information dictioary. | |
286 Args: | |
287 interface_dict: interface node dictionary | |
288 partial_dict: partial interface node dictionary | |
289 Returns: | |
290 a dictronary merged with interface_dict and partial_dict | |
291 """ | |
292 for key in partial_dict.keys(): | |
bashi
2015/09/30 03:13:16
key -> interface_name ?
natsukoa
2015/10/05 01:47:10
Done.
| |
293 interface_dict[key]['Attributes'].extend(partial_dict[key]['Attributes'] ) if partial_dict[key]['Attributes'] != [] else None | |
bashi
2015/09/30 03:13:16
(1) What if |interface_dict| doesn't have |key|? D
natsukoa
2015/10/05 01:47:10
I didn't define any specific Consts, but I could m
| |
294 interface_dict[key]['Operations'].extend(partial_dict[key]['Operations'] )if partial_dict[key]['Operations'] else None | |
295 interface_dict[key]['Consts'].extend(partial_dict[key]['Consts']) if par tial_dict[key]['Consts'] else None | |
296 interface_dict[key].setdefault('Partial_FilePaths', []).append(partial_d ict[key]['FilePath']) | |
297 return interface_dict | |
298 | |
299 | |
300 def merge_implement_nodes(interface_dict, implement_nodes): | |
301 for key in implement_nodes: | |
bashi
2015/09/30 03:13:15
See comment on merge_dict(). Almost the same can b
| |
302 interface_dict[key.GetName()]['Attributes'].extend(interface_dict[key.Ge tProperty('REFERENCE')]['Attributes']) | |
303 interface_dict[key.GetName()]['Operations'].extend(interface_dict[key.Ge tProperty('REFERENCE')]['Operations']) | |
304 interface_dict[key.GetName()]['Consts'].extend(interface_dict[key.GetPro perty('REFERENCE')]['Consts']) | |
305 return interface_dict | |
306 | |
307 | |
308 def export_to_jsonfile(dictionary, json_file): | |
309 """Returns jsonfile which is dumped each interface information dictionary to json. | |
310 Args: | |
311 dictioary: interface dictionary | |
312 json_file: json file for output | |
313 Returns: | |
314 json file which is contained each interface node dictionary | |
315 """ | |
316 with open(json_file, 'w') as f: | |
317 # TODO(natsukoa): Remove indent | |
318 json.dump(dictionary, f, sort_keys=True, indent=4) | |
319 | |
320 | |
321 def main(args): | |
322 path_file = args[0] | |
323 json_file = args[1] | |
324 file_to_list = utilities.read_file_to_list(path_file) | |
325 implement_nodes = [get_implements_node(definition) for definition in get_def initions(file_to_list) if get_implements_node(definition)] | |
bashi
2015/09/30 03:13:16
Could you format this (and below dicts) so that we
natsukoa
2015/10/05 01:47:11
I misunderstood 'predicate'. I fixed it.
| |
326 interface_dict = {get_interface_node(definition).GetName(): interface_node_t o_dict(get_interface_node(definition)) for definition in filter_non_partial(get_ definitions(file_to_list)) if get_interface_node(definition)} | |
327 interface_dict = merge_implement_nodes(interface_dict, implement_nodes) | |
328 partial_dict = {get_interface_node(definition).GetName(): interface_node_to_ dict(get_interface_node(definition)) for definition in filter_partial(get_defini tions(file_to_list)) if get_interface_node(definition)} | |
329 dictionary = merge_dict(interface_dict, partial_dict) | |
330 export_to_jsonfile(dictionary, json_file) | |
331 | |
332 | |
333 if __name__ == '__main__': | |
334 main(sys.argv[1:]) | |
OLD | NEW |