Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(21)

Side by Side Diff: Source/bindings/scripts/collect_idls_into_json.py

Issue 1323493002: Collect IDL files and exract interface node object's information (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Fix style Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | Source/bindings/scripts/interface_node_path.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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
9 import os
10 import sys
11 import json
12 import utilities
13
14 from blink_idl_parser import parse_file, BlinkIDLParser
15
16 _class_name = 'Interface'
17 _partial = 'Partial'
18
19
20 def get_interfaces(path):
21 """Returns a generator which yields interface IDL nodes.
22 Args:
23 path_file: text file path
24 Returns:
25 a generator which yields interface node objects
26 """
27 parser = BlinkIDLParser(debug=False)
28 for idl_path in path:
29 definitions = parse_file(parser, idl_path)
30 for definition in definitions.GetChildren():
31 if definition.GetClass() == _class_name:
32 yield definition
33
34
35 def get_filepath(interface_node):
36 """Returns relative path to the IDL file in which the |interface_node| is de fined.
37 Args:
38 interface_node: IDL interface node
39 Returns:
40 str which is |interface_node| file path
41 """
42 filename = interface_node.GetProperty('FILENAME')
43 return os.path.relpath(filename).strip('../chromium/src/third_party/WebKit')
44
45
46 def filter_partial(interface_node_list):
47 """Returns a generator which yields partial interface node.
48 Args:
49 interface_node_list: a generator which is interface IDL node
50 Return:
51 a generator which yields partial interface node
52 """
53 for interface_node in interface_node_list:
54 if interface_node.GetProperty(_partial):
55 yield interface_node
56
57
58 def filter_non_partial(interface_node_list):
59 """Returns a generator which yields interface node.
60 Args:
61 interface_node_list: a generator which is interface IDL node
62 Returns:
63 a generator which yields interface node
64 """
65 for interface_node in interface_node_list:
66 if not interface_node.GetProperty(_partial):
67 yield interface_node
68
69
70 def get_attributes(interface_node):
71 """Returns list of Attribute if the interface_node have one.
72 Args:
73 interface_node: interface node object
74 Returns:
75 a list of attribute
76 """
77 return interface_node.GetListOf('Attribute')
78
79
80 def get_attribute_type(attribute):
81 """Returns type of attribute or operation's argument.
82 Args:
83 node: attribute node object
84 Returns:
85 str which is Attribute object type
86 """
87 return attribute.GetOneOf('Type').GetChildren()[0].GetName()
88
89 get_operation_type = get_attribute_type
90 get_argument_type = get_attribute_type
91
92
93 def get_extattributes(node):
94 """Returns a generator which yields list of ExtAttribute.
95 Args:
96 node which has ExtAttribute
97 Returns:
98 a generator which yields list of ExtAttribute
99 """
100 extattributes = node.GetOneOf('ExtAttributes')
101 if extattributes:
102 for extattribute in extattributes.GetChildren():
103 yield extattribute
104
105
106 def extattr_dict(extattribute):
Yuki 2015/09/17 05:45:06 extattr_to_dict
natsukoa 2015/09/17 07:34:59 Done.
107 """Returns a generator which yields Extattribute's object dictionary
108 Args:
109 node: interface, attribute or operation node which has extattribute
110 Returns:
111 a generator which yields extattribute dictionary
112 """
113 for extattr_node in extattribute:
114 yield {
115 'Name': extattr_node.GetName(),
116 }
117
118
119 def attributes_dict(attribute_list):
120 """Returns a generator which yields dictioary of Extattribute object informa tion.
121 Args:
122 interface_node: interface node object
123 Returns:
124 a generator which yields dictionary of attribite information
125 """
126 for attribute in attribute_list:
127 yield {
128 'Name': attribute.GetName(),
129 'Type': get_attribute_type(attribute),
130 'ExtAttributes': [extattr for extattr in extattr_dict(get_extattribu tes(attribute))],
Yuki 2015/09/17 05:45:06 list(extattr_dict(...))
natsukoa 2015/09/17 07:34:59 Done.
131 'Readonly': attribute.GetProperty('READONLY', default=False),
132 'Static': attribute.GetProperty('STATIC', default=False),
133 }
134
135
136 def get_operations(interface_node):
137 """Returns list of Operations object under the interface_node.
138 Args:
139 interface_node: interface node object
140 Returns:
141 list which is list of oparation object
142 """
143 return interface_node.GetListOf('Operation')
144
145
146 def get_arguments(operation):
147 """Returns list of Arguments object under the operation object.
148 Args:
149 operation: interface node object
150 Returns:
151 list which is list of argument object
152 """
153 argument_node = operation.GetOneOf('Arguments')
154 return argument_node.GetListOf('Argument')
155
156
157 def argument_dict(argument):
158 """Returns generator which yields dictionary of Argument object information.
159 Args:
160 argument: interface node object
161 Returns:
162 a generator which yields dictionary of argument information
163 """
164 for arg_name in argument:
165 yield {
166 'Name': arg_name.GetName(),
167 'Type': get_argument_type(arg_name),
168 }
169
170
171 def get_operation_name(operation):
172 """Returns openration object name.
173 Args:
174 operation: operation object in interface node object
175 Returns:
176 str which is operation's name
177 """
178 if operation.GetProperty('GETTER'):
179 return '__getter__'
180 elif operation.GetProperty('SETTER'):
181 return '__setter__'
182 elif operation.GetProperty('DELETER'):
183 return '__deleter__'
184 else:
185 return operation.GetName()
186
187
188 def operation_dict(operations):
189 """Returns a generator which yields dictionary of Operation object informati on.
190 Args:
191 interface_node: interface node object
192 Returns:
193 a generator which yields dictionary of operation's informantion
194 """
195 for operation in operations:
196 yield {
197 'Name': get_operation_name(operation),
198 'Arguments': [args for args in argument_dict(get_arguments(operation ))],
199 'Type': get_operation_type(operation),
200 'ExtAttributes': [extattr for extattr in extattr_dict(get_extattribu tes(operation))],
201 'Static': operation.GetProperty('STATIC', default=False),
202 }
203
204
205 def get_inherit(interface_node):
Yuki 2015/09/17 05:45:06 Let's remove get_inherit and inherit_dict for the
natsukoa 2015/09/17 07:34:59 Done.
206 return interface_node.GetOneOf('Inherit')
207
208
209 def inherit_dict(inherit):
210 if inherit is None:
211 return []
212 else:
213 return {'Name': inherit.GetName()}
214
215
216 def get_consts(interface_node):
217 """Returns list of Constant object.
218 Args:
219 interface_node: interface node object
220 Returns:
221 list which is list of constant object
222 """
223 return interface_node.GetListOf('Const')
224
225
226 def get_const_type(node):
227 """Returns constant's type.
228 Args:
229 node: interface node's attribute or operation object
230 Returns:
231 node.GetChildren()[0].GetName(): str, constant object's name
232 """
233 return node.GetChildren()[0].GetName()
234
235
236 def get_const_value(node):
237 """Returns constant's value.
238 Args:
239 node: interface node's attribute or operation object
240 Returns:
241 node.GetChildren()[1].GetName(): list, list of oparation object
242 """
243 return node.GetChildren()[1].GetName()
244
245
246 def const_dict(consts):
247 """Returns generator which yields dictionary of constant object information.
248 Args:
249 interface_node: interface node object
250 Returns:
251 a generator which yields dictionary of constant object information
252 """
253 for const in consts:
254 yield {
255 'Name': const.GetName(),
256 'Type': get_const_type(const),
257 'Value': get_const_value(const),
258 'ExtAttributes': [extattr for extattr in extattr_dict(get_extattribu tes(const))],
259 }
260
261
262 def get_name(interface_node):
Yuki 2015/09/17 05:45:06 get_name(my_interface) is not shorter or simpler
natsukoa 2015/09/17 07:34:59 Actually, I don't need it. I just removed it.
263 return interface_node.GetName()
264
265
266 def get_dict(interface_node):
Yuki 2015/09/17 05:45:06 interface_to_dict
natsukoa 2015/09/17 07:34:59 Done.
267 """Returns dictioary whose key is interface name and value is interface dict ioary.
268 Args:
269 dictirary_list: list, list of interface node dictionary
270 Returns:
271 dictionary, {interface_node name: interface node dictionary}
272 """
273
274 return {
275 'Attributes': [attr for attr in attributes_dict(get_attributes(interface _node))],
276 'Operations': [operation for operation in operation_dict(get_operations( interface_node))],
277 'ExtAttributes': [extattr for extattr in extattr_dict(get_extattributes( interface_node))],
278 'Consts': [const for const in const_dict(get_consts(interface_node))],
279 'Inherit': inherit_dict(get_inherit(interface_node)),
280 'FilePath': get_filepath(interface_node),
281 }
282
283
284 def merge_dict(interface_dict, partial_dict):
285 """Returns list of interface_node information dictioary.
286 Args:
287 interface_dict: interface node dictionary
288 partial_dict: partial interface node dictionary
289 Returns:
290 list which is list of interface node's dictionry merged with partial inter face node
291 """
292 for key in partial_dict.keys():
293 if key in interface_dict:
294 interface_dict[key]['Attributes'].append(partial_dict[key]['Attribut es'])
295 interface_dict[key]['Operations'].append(partial_dict[key]['Operatio ns'])
296 interface_dict[key]['Consts'].append(partial_dict[key]['Consts'])
297 interface_dict[key].setdefault('Partial_FilePaths', []).append(parti al_dict[key]['FilePath'])
298 return interface_dict
299
300
301 # TODO(natsukoa): Supports a command line flag to indent the json
302 def export_to_jsonfile(dictionary, json_file):
303 """Returns jsonfile which is dumped each interface_node information dictiona ry to json.
304 Args:
305 dictioary: dict, output of format_dictinatry
306 json_file: json file for output
307 Returns:
308 json file which is contained each interface node dictionary
309 """
310 with open(json_file, 'w') as f:
311 json.dump(dictionary, f, sort_keys=True)
312
313
314 def main(args):
315 path_file = args[0]
316 json_file = args[1]
317 file_to_list = utilities.read_file_to_list(path_file)
318 interface_dict = {get_name(interface_node): get_dict(interface_node) for int erface_node in filter_non_partial(get_interfaces(file_to_list))}
319 partial_dict = {get_name(interface_node): get_dict(interface_node) for inter face_node in filter_partial(get_interfaces(file_to_list))}
320 dictionary = merge_dict(interface_dict, partial_dict)
321 export_to_jsonfile(dictionary, json_file)
322
323
324 if __name__ == '__main__':
325 main(sys.argv[1:])
OLDNEW
« no previous file with comments | « no previous file | Source/bindings/scripts/interface_node_path.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698