Chromium Code Reviews| 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 | |
| 9 import os | |
| 10 import sys | |
| 11 import json | |
| 12 | |
| 13 chromium_path = os.path.abspath( | |
|
Yuki
2015/09/10 10:23:04
We should no longer need this hack.
natsukoa
2015/09/17 04:35:07
Done.
| |
| 14 os.path.join(os.environ['HOME'], 'chromium', 'src')) | |
| 15 blink_bindings_path = os.path.join( | |
| 16 chromium_path, 'third_party', 'WebKit', 'Source', 'bindings', 'scripts') | |
| 17 sys.path.insert(0, blink_bindings_path) | |
| 18 | |
| 19 from blink_idl_parser import parse_file, BlinkIDLParser | |
| 20 import utilities | |
| 21 | |
| 22 | |
| 23 _class_name = 'Interface' | |
| 24 _partial = 'Partial' | |
| 25 | |
| 26 | |
| 27 def load_filepaths(path_file): | |
|
Yuki
2015/09/10 10:23:04
We should no longer need this function.
natsukoa
2015/09/17 04:35:07
Done.
| |
| 28 with open(path_file, 'r') as f: | |
| 29 for line in f: | |
| 30 path = line.strip() | |
| 31 yield path | |
| 32 | |
| 33 | |
| 34 def get_interfaces(path): | |
|
Yuki
2015/09/10 10:23:05
path?
Also inconsistent with the comment.
| |
| 35 """Returns a generator which yields interface IDL nodes. | |
| 36 Args: | |
| 37 path_file: text file path | |
| 38 Returns: | |
| 39 a generator which yields interface node objects | |
| 40 """ | |
| 41 parser = BlinkIDLParser(debug=False) | |
| 42 for idl_path in path: | |
| 43 definitions = parse_file(parser, idl_path) | |
| 44 for definition in definitions.GetChildren(): | |
| 45 if definition.GetClass() == _class_name: | |
| 46 yield definition | |
| 47 | |
| 48 | |
| 49 def get_filepath(interface_node): | |
| 50 """Returns relative path to the IDL file in which the |interface_node| is de fined. | |
| 51 Args: | |
| 52 interface_node: IDL interface node | |
| 53 Returns: | |
| 54 str which is |interface_node| file path | |
| 55 """ | |
| 56 filename = interface_node.GetProperty('FILENAME') | |
| 57 return os.path.relpath(filename) | |
| 58 | |
| 59 | |
| 60 def filter_partial(interface_node_list): | |
| 61 """Returns a generator which yields partial interface node. | |
| 62 Args: | |
| 63 interface_node_list: a generator which is interface IDL node | |
| 64 Return: | |
| 65 a generator which yields partial interface node | |
| 66 """ | |
| 67 for interface_node in interface_node_list: | |
| 68 if interface_node.GetProperty(_partial): | |
| 69 yield interface_node | |
| 70 | |
| 71 | |
| 72 def filter_non_partial(interface_node_list): | |
| 73 """Returns a generator which yields interface node. | |
| 74 Args: | |
| 75 interface_node_list: a generator which is interface IDL node | |
| 76 Returns: | |
| 77 a generator which yields interface node | |
| 78 """ | |
| 79 for interface_node in interface_node_list: | |
| 80 if not interface_node.GetProperty(_partial): | |
| 81 yield interface_node | |
| 82 | |
| 83 | |
| 84 def get_attributes(interface_node): | |
| 85 """Returns list of Attribute if the interface_node have one. | |
| 86 Args: | |
| 87 interface_node: interface node object | |
| 88 Returns: | |
| 89 a list of attribute | |
| 90 """ | |
| 91 return interface_node.GetListOf('Attribute') | |
| 92 | |
| 93 | |
| 94 def get_attribute_type(attribute): | |
| 95 """Returns type of attribute or operation's argument. | |
| 96 Args: | |
| 97 node: attribute node object | |
| 98 Returns: | |
| 99 str which is Attribute object type | |
| 100 """ | |
| 101 return attribute.GetOneOf('Type').GetChildren()[0].GetName() | |
| 102 | |
| 103 get_operation_type = get_attribute_type | |
| 104 get_argument_type = get_attribute_type | |
| 105 | |
| 106 | |
| 107 def get_extattributes(node): | |
| 108 """Returns a generator which yields Extattribute's object dictionary | |
| 109 Args: | |
| 110 node: interface, attribute or operation node which has extattribute | |
| 111 Returns: | |
| 112 a generator which yields extattribute dictionary | |
| 113 """ | |
| 114 def get_extattr_nodes(): | |
| 115 extattributes = node.GetOneOf('ExtAttributes') | |
| 116 if extattributes: | |
| 117 for extattribute in extattributes.GetChildren(): | |
| 118 yield extattribute | |
| 119 for extattr_node in get_extattr_nodes(): | |
| 120 yield { | |
| 121 'Name': extattr_node.GetName(), | |
| 122 } | |
| 123 | |
| 124 | |
| 125 def attributes_dict(interface_node): | |
| 126 """Returns a generator which yields dictioary of Extattribute object informa tion. | |
| 127 Args: | |
| 128 interface_node: interface node object | |
| 129 Returns: | |
| 130 a generator which yields dictionary of attribite information | |
| 131 """ | |
| 132 for attribute in get_attributes(interface_node): | |
| 133 yield { | |
| 134 'Name': attribute.GetName(), | |
| 135 'Type': get_attribute_type(attribute), | |
| 136 'ExtAttributes': [extattr for extattr in get_extattributes(attribute )], | |
| 137 } | |
| 138 | |
| 139 | |
| 140 def get_operations(interface_node): | |
| 141 """Returns list of Operations object under the interface_node. | |
| 142 Args: | |
| 143 interface_node: interface node object | |
| 144 Returns: | |
| 145 list which is list of oparation object | |
| 146 """ | |
| 147 return interface_node.GetListOf('Operation') | |
| 148 | |
| 149 | |
| 150 def get_arguments(operation): | |
| 151 """Returns list of Arguments object under the operation object. | |
| 152 Args: | |
| 153 operation: interface node object | |
| 154 Returns: | |
| 155 list which is list of argument object | |
| 156 """ | |
| 157 argument_node = operation.GetOneOf('Arguments') | |
| 158 return argument_node.GetListOf('Argument') | |
| 159 | |
| 160 | |
| 161 def argument_dict(argument): | |
| 162 """Returns generator which yields dictionary of Argument object information. | |
| 163 Args: | |
| 164 argument: interface node object | |
| 165 Returns: | |
| 166 a generator which yields dictionary of argument information | |
| 167 """ | |
| 168 for arg_name in get_arguments(argument): | |
| 169 yield { | |
| 170 'Name': arg_name.GetName(), | |
| 171 'Type': get_argument_type(arg_name), | |
| 172 } | |
| 173 | |
| 174 | |
| 175 def get_operation_name(operation): | |
| 176 """Returns openration object name. | |
| 177 Args: | |
| 178 operation: operation object in interface node object | |
| 179 Returns: | |
| 180 str which is operation's name | |
| 181 """ | |
| 182 if operation.GetProperty('GETTER', default=None): | |
| 183 return '__getter__' | |
| 184 elif operation.GetProperty('SETTER', default=None): | |
| 185 return '__setter__' | |
| 186 elif operation.GetProperty('DELETER', default=None): | |
| 187 return '__deleter__' | |
| 188 else: | |
| 189 return operation.GetName() | |
| 190 | |
| 191 | |
| 192 def operation_dict(interface_node): | |
| 193 """Returns a generator which yields dictionary of Operation object informati on. | |
| 194 Args: | |
| 195 interface_node: interface node object | |
| 196 Returns: | |
| 197 a generator which yields dictionary of operation's informantion | |
| 198 """ | |
| 199 for operation in get_operations(interface_node): | |
| 200 yield { | |
| 201 'Name': get_operation_name(operation), | |
| 202 'Argument': [args for args in argument_dict(operation)], | |
| 203 'Type': get_operation_type(operation), | |
| 204 'ExtAttributes': [extattr for extattr in get_extattributes(operation )], | |
| 205 } | |
| 206 | |
| 207 | |
| 208 def get_consts(interface_node): | |
| 209 """Returns list of Constant object. | |
| 210 Args: | |
| 211 interface_node: interface node object | |
| 212 Returns: | |
| 213 list which is list of constant object | |
| 214 """ | |
| 215 return interface_node.GetListOf('Const') | |
| 216 | |
| 217 | |
| 218 def get_const_type(node): | |
| 219 """Returns constant's type. | |
| 220 Args: | |
| 221 node: interface node's attribute or operation object | |
| 222 Returns: | |
| 223 node.GetChildren()[0].GetName(): str, constant object's name | |
| 224 """ | |
| 225 return node.GetChildren()[0].GetName() | |
| 226 | |
| 227 | |
| 228 def get_const_value(node): | |
| 229 """Returns constant's value. | |
| 230 Args: | |
| 231 node: interface node's attribute or operation object | |
| 232 Returns: | |
| 233 node.GetChildren()[1].GetName(): list, list of oparation object | |
| 234 """ | |
| 235 return node.GetChildren()[1].GetName() | |
| 236 | |
| 237 | |
| 238 def const_dict(interface_node): | |
| 239 """Returns generator which yields dictionary of constant object information. | |
| 240 Args: | |
| 241 interface_node: interface node object | |
| 242 Returns: | |
| 243 a generator which yields dictionary of constant object information | |
| 244 """ | |
| 245 for const in get_consts(interface_node): | |
| 246 yield { | |
| 247 'Name': const.GetName(), | |
| 248 'Type': get_const_type(const), | |
| 249 'Value': get_const_value(const), | |
| 250 'ExtAttributes': [extattr for extattr in get_extattributes(const)], | |
| 251 } | |
| 252 | |
| 253 | |
| 254 def format_interface_to_dict(interface_node): | |
| 255 """Returns dictioanry of each interface_node information. | |
| 256 Args: | |
| 257 interface_node: interface node object | |
| 258 Returns: | |
| 259 dictionary which has interface node information | |
| 260 """ | |
| 261 return { | |
| 262 'Name': interface_node.GetName(), | |
| 263 'FilePath': get_filepath(interface_node), | |
| 264 'Attributes': [attr for attr in attributes_dict(interface_node)], | |
| 265 'Operations': [operation for operation in operation_dict(interface_node) ], | |
| 266 'ExtAttributes': [extattr for extattr in get_extattributes(interface_nod e)], | |
| 267 'Consts': [const for const in const_dict(interface_node)], | |
| 268 } | |
| 269 | |
| 270 | |
| 271 def merge_partial_interface(interface_dict_list, partial_dict_list): | |
| 272 """Returns list of interface_node information dictioary. | |
| 273 Args: | |
| 274 interface_dict_list: list of interface node dictionary | |
| 275 partial_dict_list: list of partial interface node dictionary | |
| 276 Returns: | |
| 277 list which is list of interface node's dictionry merged with partial inter face node | |
| 278 """ | |
| 279 for partial in partial_dict_list: | |
| 280 for interface in interface_dict_list: | |
| 281 if interface['Name'] == partial['Name']: | |
| 282 interface['Attributes'].append(partial['Attributes']) | |
| 283 interface['Operations'].append(partial['Operations']) | |
| 284 # TODO(natsukoa): filter extattribute of Web IDL or Blink | |
| 285 # interface['ExtAttributes'].append(partial['ExtAttributes']) | |
| 286 interface['Consts'].append(partial['Consts']) | |
| 287 interface.setdefault('Partial_FilePath', []).append(partial['Fil ePath']) | |
| 288 return interface_dict_list | |
| 289 | |
| 290 | |
| 291 def format_list_to_dict(dictionary_list): | |
| 292 """Returns dictioary whose key is interface name and value is interface dict ioary. | |
| 293 Args: | |
| 294 dictirary_list: list, list of interface node dictionary | |
| 295 Returns: | |
| 296 dictionary, {interface_node name: interface node dictionary} | |
| 297 """ | |
| 298 dictionary = {} | |
| 299 for interface_dict in dictionary_list: | |
| 300 dictionary[interface_dict['Name']] = interface_dict | |
| 301 return dictionary | |
| 302 | |
| 303 | |
| 304 # TODO(natsukoa): Supports a command line flag to indent the json | |
| 305 def export_to_jsonfile(dictionary, json_file): | |
| 306 """Returns jsonfile which is dumped each interface_node information dictiona ry to json. | |
| 307 Args: | |
| 308 dictioary: dict, output of format_dictinatry | |
| 309 json_file: json file for output | |
| 310 Returns: | |
| 311 json file which is contained each interface node dictionary | |
| 312 """ | |
| 313 # TODO(natsukoa): Remove indent_size | |
| 314 indent_size = 4 | |
| 315 with open(json_file, 'w') as f: | |
| 316 json.dump(dictionary, f, sort_keys=True, indent=indent_size) | |
| 317 | |
| 318 | |
| 319 def main(args): | |
| 320 path_file = args[0] | |
| 321 json_file = args[1] | |
| 322 file_to_list = utilities.read_file_to_list(path_file) | |
| 323 interface_dict_list = [format_interface_to_dict(interface_node) for interfac e_node in filter_non_partial(get_interfaces(file_to_list))] | |
| 324 partial_dict_list = [format_interface_to_dict(interface_node) for interface_ node in filter_partial(get_interfaces(file_to_list))] | |
| 325 dictionary_list = merge_partial_interface(interface_dict_list, partial_dict_ list) | |
| 326 dictionary = format_list_to_dict(dictionary_list) | |
| 327 export_to_jsonfile(dictionary, json_file) | |
| 328 | |
| 329 | |
| 330 if __name__ == '__main__': | |
| 331 main(sys.argv[1:]) | |
| OLD | NEW |