OLD | NEW |
---|---|
(Empty) | |
1 #!/usr/bin/env python | |
2 | |
Yuki
2015/09/04 10:07:57
Need copyright notice.
natsukoa
2015/09/07 08:37:55
Done.
| |
3 """Usage: interface_to_json.py path_file.txt json_file.json | |
Yuki
2015/09/04 10:07:57
filename: "collect_idls_into_json.py"
natsukoa
2015/09/07 08:37:55
Done.
| |
4 | |
5 path_textfile: output of interface_nodo_path.py | |
6 | |
7 The goal of this script is to integrate and dump interface node information to j son file. | |
8 """ | |
9 | |
10 import os | |
11 import sys | |
12 import json | |
13 | |
14 from blink_idl_parser import parse_file, BlinkIDLParser | |
15 | |
16 | |
17 def load_filepaths(path_file): | |
18 """Returns a generator which yields absolute path of IDL files written in te xt file. | |
Yuki
2015/09/04 10:07:57
written in |path_file|.
natsukoa
2015/09/07 08:37:55
Done.
| |
19 Args: | |
20 path_file: text file | |
21 Return: | |
Yuki
2015/09/04 10:07:57
s/Return/Returns/
natsukoa
2015/09/07 08:37:55
Done.
| |
22 path: generator, absolute file path | |
23 """ | |
24 for line in open(path_file, 'r'): | |
25 path = line.strip() | |
26 yield path | |
27 | |
28 | |
29 def get_interfaces(path_file): | |
Yuki
2015/09/04 10:07:57
s/path_file/idl_paths/
natsukoa
2015/09/07 08:37:55
Done.
| |
30 """Returns generator which yields interface IDL nodes. | |
31 Args: | |
32 path_file: text file | |
33 Return: | |
34 definition: generator, interface node objects | |
35 """ | |
36 parser = BlinkIDLParser(debug=False) | |
37 class_name = 'Interface' | |
38 for node_path in load_filepaths(path_file): | |
Yuki
2015/09/04 10:07:57
s/load_file_paths(path_file)/idl_paths/
natsukoa
2015/09/07 08:37:55
Done.
| |
39 definitions = parse_file(parser, node_path) | |
40 for definition in definitions.GetChildren(): | |
41 if definition.GetClass() == class_name: | |
42 yield definition | |
43 | |
44 | |
45 def get_filepath(interface_node): | |
46 """Returns relative path which is contained in interface_node. | |
47 Args: | |
48 interface_node: interface node class object | |
49 Return: | |
50 os.path.relpath(filename): str, interface_node's file path | |
51 """ | |
52 filename = interface_node.GetProperty('FILENAME') | |
53 return os.path.relpath(filename) | |
54 | |
55 | |
56 def get_partial(interface_node_list): | |
57 """Returns generator which yields partial interface node. | |
58 Args: | |
59 interface_node_list: generator, interface node class object | |
60 Return: | |
61 interface_node: generator, interface node class object | |
62 """ | |
63 for interface_node in interface_node_list: | |
64 if interface_node.GetProperty('Partial', default=False): | |
65 yield interface_node | |
66 | |
67 | |
68 def get_non_partial(interface_node_list): | |
69 """Returns generator which yields interface node. | |
70 Args: | |
71 interface_node_list: generator interface node class object | |
72 Return: | |
73 interface_node: generator, interface node class object | |
74 """ | |
75 for interface_node in interface_node_list: | |
76 if not interface_node.GetProperty('Partial', default=False): | |
77 yield interface_node | |
78 | |
79 | |
80 def get_attributes(interface_node): | |
81 """Returns list of Attribute object. | |
82 Args: | |
83 interface_node: interface node object | |
84 Return: | |
85 interface_node.GetListOf('Attribute'): list, Attribute object list | |
86 """ | |
87 return interface_node.GetListOf('Attribute') | |
88 | |
89 | |
90 def get_type(node): | |
91 """Returns type of attribute or operation's type. | |
92 Args: | |
93 node: interface node object | |
94 """ | |
95 return node.GetListOf('Type')[0].GetChildren()[0].GetName() | |
Yuki
2015/09/04 10:07:57
def get_attribute_type(attribute_node):
....
natsukoa
2015/09/07 08:37:55
Done.
| |
96 | |
97 | |
98 def get_extattirbutes(node): | |
99 """Returns generator which yields list of Extattribute object. | |
100 Args: | |
101 node: interface node object | |
102 Return: | |
103 extattribute_list: generator, extattribute object list | |
104 """ | |
105 for extattributes in node.GetListOf('ExtAttributes'): | |
106 for extattribute_list in extattributes.GetChildren(): | |
107 yield extattribute_list | |
108 | |
109 | |
110 def extattr_dict(node): | |
111 """Returns generator which yields Extattribute's information dictionary. | |
112 Args: | |
113 node: interface node object | |
114 Return: | |
115 {'Name': extattribute.GetName()}: generator, extattribute dictionary | |
116 """ | |
Yuki
2015/09/04 10:07:57
def get_extattrs(node):
def get_extattr_nodes(
natsukoa
2015/09/07 08:37:55
Done.
| |
117 for extattribute in get_extattirbutes(node): | |
118 yield { | |
119 'Name': extattribute.GetName() | |
120 } | |
121 | |
122 | |
123 def attributes_dict(interface_node): | |
124 """Returns generator which yields dictioary of Extattribute object informati on. | |
125 Args: | |
126 interface_node: interface node object | |
127 Return: | |
128 attr_dict: generator, dictionary of attribite information | |
129 """ | |
130 for attribute in get_attributes(interface_node): | |
131 attr_dict = {} | |
132 attr_dict['Name'] = attribute.GetName() | |
133 attr_dict['Type'] = get_type(attribute) | |
134 attr_dict['ExtAttributes'] = [extattr for extattr in extattr_dict(attrib ute)] | |
135 yield attr_dict | |
136 | |
137 | |
138 def get_operations(interface_node): | |
139 """Returns list of Operations object under the interface_node. | |
140 Args: | |
141 interface_node: interface node object | |
142 Return: | |
143 interface_node.GetListOf('Operation'): list, list of oparation object | |
144 """ | |
145 return interface_node.GetListOf('Operation') | |
146 | |
147 | |
148 def get_arguments(operation): | |
149 """Returns list of Arguments object under the operation object. | |
150 Args: | |
151 operation: interface node object | |
152 Return: | |
153 argument_node.GetListOf('Argument'): list, list of argument object | |
154 """ | |
155 argument_node = operation.GetListOf('Arguments')[0] | |
156 return argument_node.GetListOf('Argument') | |
157 | |
158 | |
159 def argument_dict(argument): | |
160 """Returns generator which yields dictionary of Argument object information. | |
161 Args: | |
162 argument: interface node object | |
163 Return: | |
164 arg_dict: dict, generator of argument information's dictionary | |
165 """ | |
166 for arg_name in get_arguments(argument): | |
167 arg_dict = {} | |
168 arg_dict['Name'] = arg_name.GetName() | |
169 arg_dict['Type'] = get_type(arg_name) | |
170 yield arg_dict | |
171 | |
172 | |
173 def get_operation_name(operation): | |
174 """Returns openration object name. | |
175 Args: | |
176 operation: operation object in interface node object | |
177 Return: | |
178 str, operation's name | |
179 """ | |
180 if operation.GetProperty('GETTER', default=None): | |
181 return '__getter__' | |
182 elif operation.GetProperty('SETTER', default=None): | |
183 return '__setter__' | |
184 elif operation.GetProperty('DELETER', default=None): | |
185 return '__deleter__' | |
186 else: | |
187 return operation.GetName() | |
188 | |
189 | |
190 def operation_dict(interface_node): | |
191 """Returns generator which yields dictionary of Operation object information . | |
192 Args: | |
193 interface_node: interface node object | |
194 Return: | |
195 operate_dict: generator of operation dictionary | |
196 """ | |
197 for operation in get_operations(interface_node): | |
198 operate_dict = {} | |
199 operate_dict['Name'] = get_operation_name(operation) | |
200 operate_dict['Argument'] = [args for args in argument_dict(operation)] | |
201 operate_dict['Type'] = get_type(operation) | |
202 operate_dict['ExtAttributes'] = [extattr for extattr in extattr_dict(ope ration)] | |
203 yield operate_dict | |
204 | |
205 | |
206 def get_consts(interface_node): | |
207 """Returns list of Constant object. | |
208 Args: | |
209 interface_node: interface node object | |
210 Return: | |
211 interface_node.GetListOf('Const'): list, list of constant object | |
212 """ | |
213 return interface_node.GetListOf('Const') | |
214 | |
215 | |
216 def get_const_type(node): | |
217 """Returns constant's type. | |
218 Args: | |
219 node: interface node's attribute or operation object | |
220 Return: | |
221 node.GetChildren()[0].GetName(): str, constant object's name | |
222 """ | |
223 return node.GetChildren()[0].GetName() | |
224 | |
225 | |
226 def get_const_value(node): | |
227 """Returns constant's value. | |
228 Args: | |
229 node: interface node's attribute or operation object | |
230 Return: | |
231 node.GetChildren()[1].GetName(): list, list of oparation object | |
232 """ | |
233 return node.GetChildren()[1].GetName() | |
234 | |
235 | |
236 def const_dict(interface_node): | |
237 """Returns generator which yields dictionary of constant object information. | |
238 Args: | |
239 interface_node: interface node object | |
240 Return: | |
241 {}: generator, dict of constant object information | |
242 """ | |
243 for const in get_consts(interface_node): | |
244 yield { | |
245 'Name': const.GetName(), | |
246 'Type': get_const_type(const), | |
247 'Value': get_const_value(const) | |
248 } | |
249 | |
250 | |
251 def format_interface_dict(interface_node): | |
252 """Returns dictioanry of each interface_node information. | |
253 Args: | |
254 interface_node: interface node object | |
255 Return: | |
256 interface_dict: dict, dictionary of interface node information | |
257 """ | |
258 interface_dict = {} | |
259 interface_dict['Name'] = interface_node.GetName() | |
260 interface_dict['FilePath'] = get_filepath(interface_node) | |
261 interface_dict['Attribute'] = [attr for attr in attributes_dict(interface_no de)] | |
262 interface_dict['Operation'] = [operation for operation in operation_dict(int erface_node)] | |
263 interface_dict['ExtAttributes'] = [extattr for extattr in extattr_dict(inter face_node)] | |
264 interface_dict['Constant'] = [const for const in const_dict(interface_node) if const] | |
265 return interface_dict | |
266 | |
267 | |
268 def merge_partial_interface(interface_dict_list, partial_dict_list): | |
269 """Returns list of interface_node information dictioary. | |
270 Args: | |
271 interface_dict_list: list | |
272 partial_dict_list: list | |
273 Return: | |
274 interface_dict_list: list, list of interface node's dictionry merged with partial interface node | |
275 """ | |
276 for partial in partial_dict_list: | |
277 for interface in interface_dict_list: | |
278 if interface['Name'] == partial['Name']: | |
279 interface['Attribute'].append(partial['Attribute']) | |
280 interface['Operation'].append(partial['Operation']) | |
281 interface['ExtAttributes'].append(partial['ExtAttributes']) | |
282 interface.setdefault('Partial_FilePath', []).append(partial['Fil ePath']) | |
283 if interface['Constant']: | |
284 interface.setdefault('Constant', []).append(partial['Constan t']) | |
285 return interface_dict_list | |
286 | |
287 | |
288 def format_dictionary(dictionary_list): | |
289 """Returns dictioary which is changed structure of interface_dict_list. | |
290 Args: | |
291 dictirary_list: list, list of interface node dictionary | |
292 Return: | |
293 dictionary: dict, {interface_node name: interface node dictionary} | |
294 """ | |
295 dictionary = {} | |
296 for interface_dict in dictionary_list: | |
297 dictionary.setdefault(interface_dict['Name'], interface_dict) | |
298 return dictionary | |
299 | |
300 | |
301 def export_jsonfile(dictionary, json_file): | |
Yuki
2015/09/04 10:07:57
path = "foo/bar/baz.ext"
filepath = "foo/bar/baz.e
natsukoa
2015/09/07 08:37:55
Done.
| |
302 """Returns jsonfile which is dumped each interface_node information dictiona ry to json. | |
303 Args: | |
304 dictioary: dict, output of format_dictinatry | |
305 json_file: json file for output | |
306 Return: | |
307 json file which is contained each interface node dictionary | |
308 """ | |
309 filename = json_file | |
310 indent_size = None | |
311 with open(filename, 'w') as f: | |
312 json.dump(dictionary, f, sort_keys=True, indent=indent_size) | |
313 | |
314 | |
315 def main(args): | |
316 path_file = args[0] | |
317 json_file = args[1] | |
318 interface_dict_list = [format_interface_dict(interface_node) for interface_n ode in get_non_partial(get_interfaces(path_file))] | |
319 partial_dict_list = [format_interface_dict(interface_node) for interface_nod e in get_partial(get_interfaces(path_file))] | |
320 dictionary_list = merge_partial_interface(interface_dict_list, partial_dict_ list) | |
321 dictionary = format_dictionary(dictionary_list) | |
322 export_jsonfile(dictionary, json_file) | |
323 | |
324 | |
325 if __name__ == '__main__': | |
326 main(sys.argv[1:]) | |
OLD | NEW |