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 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(paths): |
| 21 """Returns a generator which yields interface IDL. |
| 22 Args: |
| 23 paths: IDL file path list |
| 24 Returns: |
| 25 a generator which yields interface node objects |
| 26 """ |
| 27 parser = BlinkIDLParser(debug=False) |
| 28 for path in paths: |
| 29 definitions = parse_file(parser, 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 |
| 39 Returns: |
| 40 str which is |interface_node| file path under WebKit directory |
| 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_nodes): |
| 47 """Returns a generator which yields partial interface. |
| 48 Args: |
| 49 interface_nodes: a generator which is interface IDL |
| 50 Return: |
| 51 a generator which yields partial interface node |
| 52 """ |
| 53 for interface_node in interface_nodes: |
| 54 if interface_node.GetProperty(_partial): |
| 55 yield interface_node |
| 56 |
| 57 |
| 58 def filter_non_partial(interface_nodes): |
| 59 """Returns a generator which yields interface node. |
| 60 Args: |
| 61 interface_nodes: a generator which is interface IDL node |
| 62 Returns: |
| 63 a generator which yields interface node |
| 64 """ |
| 65 for interface_node in interface_nodes: |
| 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 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_node): |
| 81 """Returns type of attribute or operation's argument. |
| 82 Args: |
| 83 attribute_node: attribute node object |
| 84 Returns: |
| 85 str which is Attribute object type |
| 86 """ |
| 87 return attribute_node.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 extattribute_nodes = node.GetOneOf('ExtAttributes') |
| 95 if extattribute_nodes: |
| 96 for extattribute_node in extattribute_nodes.GetChildren(): |
| 97 yield extattribute_node |
| 98 |
| 99 |
| 100 def extattr_to_dict(extattribute_nodes): |
| 101 """Returns a generator which yields Extattribute's object dictionary |
| 102 Args: |
| 103 extattribute_nodes: interface, attribute or operation node which has extat
tribute |
| 104 Returns: |
| 105 a generator which yields extattribute dictionary |
| 106 """ |
| 107 for extattribute_node in extattribute_nodes: |
| 108 yield { |
| 109 'Name': extattribute_node.GetName(), |
| 110 } |
| 111 |
| 112 |
| 113 def attributes_to_dict(attribute_nodes): |
| 114 """Returns a generator which yields dictioary of Extattribute object informa
tion. |
| 115 Args: |
| 116 attribute_nodes: interface node object |
| 117 Returns: |
| 118 a generator which yields dictionary of attribite information |
| 119 """ |
| 120 for attribute_node in attribute_nodes: |
| 121 yield { |
| 122 'Name': attribute_node.GetName(), |
| 123 'Type': get_attribute_type(attribute_node), |
| 124 'ExtAttributes': list(extattr_to_dict(get_attributes(attribute_node)
)), |
| 125 'Readonly': attribute_node.GetProperty('READONLY', default=False), |
| 126 'Static': attribute_node.GetProperty('STATIC', default=False), |
| 127 } |
| 128 |
| 129 |
| 130 def get_operations(interface_node): |
| 131 """Returns list of Operations object under the interface. |
| 132 Args: |
| 133 interface: interface node object |
| 134 Returns: |
| 135 list which is list of oparation object |
| 136 """ |
| 137 return interface_node.GetListOf('Operation') |
| 138 |
| 139 |
| 140 def get_arguments(operation_node): |
| 141 """Returns list of Arguments object under the operation object. |
| 142 Args: |
| 143 operation_node: interface node object |
| 144 Returns: |
| 145 list of argument object |
| 146 """ |
| 147 argument_node = operation_node.GetOneOf('Arguments') |
| 148 return argument_node.GetListOf('Argument') |
| 149 |
| 150 |
| 151 def argument_dict(argument_nodes): |
| 152 """Returns generator which yields dictionary of Argument object information. |
| 153 Args: |
| 154 arguments: interface node object |
| 155 Returns: |
| 156 a generator which yields dictionary of argument information |
| 157 """ |
| 158 for argument_node in argument_nodes: |
| 159 yield { |
| 160 'Name': argument_node.GetName(), |
| 161 'Type': get_argument_type(argument_node), |
| 162 } |
| 163 |
| 164 |
| 165 def get_operation_name(operation_node): |
| 166 """Returns openration object name. |
| 167 Args: |
| 168 operation_node: operation object in interface node object |
| 169 Returns: |
| 170 str which is operation's name |
| 171 """ |
| 172 if operation_node.GetProperty('GETTER'): |
| 173 return '__getter__' |
| 174 elif operation_node.GetProperty('SETTER'): |
| 175 return '__setter__' |
| 176 elif operation_node.GetProperty('DELETER'): |
| 177 return '__deleter__' |
| 178 else: |
| 179 return operation_node.GetName() |
| 180 |
| 181 |
| 182 def operation_dict(operation_nodes): |
| 183 """Returns a generator which yields dictionary of Operation object informati
on. |
| 184 Args: |
| 185 operation_nodes: interface node object |
| 186 Returns: |
| 187 a generator which yields dictionary of operation's informantion |
| 188 """ |
| 189 for operation_node in operation_nodes: |
| 190 yield { |
| 191 'Name': get_operation_name(operation_node), |
| 192 'Arguments': list(argument_dict(get_arguments(operation_node))), |
| 193 'Type': get_operation_type(operation_node), |
| 194 'ExtAttributes': list(extattr_to_dict(get_attributes(operation_node)
)), |
| 195 'Static': operation_node.GetProperty('STATIC', default=False), |
| 196 } |
| 197 |
| 198 |
| 199 def inherit_to_dict(interface_node): |
| 200 if interface_node.GetOneOf('Inherit'): |
| 201 yield {'Name': interface_node.GetOneOf('Inherit').GetName()} |
| 202 |
| 203 |
| 204 def get_consts(interface_node): |
| 205 """Returns list of Constant object. |
| 206 Args: |
| 207 interface_node: interface node object |
| 208 Returns: |
| 209 list which is list of constant object |
| 210 """ |
| 211 return interface_node.GetListOf('Const') |
| 212 |
| 213 |
| 214 def get_const_type(node): |
| 215 """Returns constant's type. |
| 216 Args: |
| 217 node: interface node's attribute or operation object |
| 218 Returns: |
| 219 node.GetChildren()[0].GetName(): str, constant object's name |
| 220 """ |
| 221 return node.GetChildren()[0].GetName() |
| 222 |
| 223 |
| 224 def get_const_value(node): |
| 225 """Returns constant's value. |
| 226 Args: |
| 227 node: interface node's attribute or operation object |
| 228 Returns: |
| 229 node.GetChildren()[1].GetName(): list, list of oparation object |
| 230 """ |
| 231 return node.GetChildren()[1].GetName() |
| 232 |
| 233 |
| 234 def const_dict(const_nodes): |
| 235 """Returns generator which yields dictionary of constant object information. |
| 236 Args: |
| 237 const_nodes: interface node object |
| 238 Returns: |
| 239 a generator which yields dictionary of constant object information |
| 240 """ |
| 241 for const_node in const_nodes: |
| 242 yield { |
| 243 'Name': const_node.GetName(), |
| 244 'Type': get_const_type(const_node), |
| 245 'Value': get_const_value(const_node), |
| 246 'ExtAttributes': list(extattr_to_dict(get_attributes(const_node))), |
| 247 } |
| 248 |
| 249 |
| 250 def interface_to_dict(interface_node): |
| 251 """Returns dictioary whose key is interface name and value is interface dict
ioary. |
| 252 Args: |
| 253 interface_node: interface node |
| 254 Returns: |
| 255 dictionary, {interface name: interface node dictionary} |
| 256 """ |
| 257 return { |
| 258 'Attributes': list(attributes_to_dict(get_attributes(interface_node))), |
| 259 'Operations': list(operation_dict(get_operations(interface_node))), |
| 260 'ExtAttributes': list(extattr_to_dict(get_attributes(interface_node))), |
| 261 'Consts': list(const_dict(get_consts(interface_node))), |
| 262 'Inherit': list(inherit_to_dict(interface_node)), |
| 263 'FilePath': get_filepath(interface_node), |
| 264 } |
| 265 |
| 266 |
| 267 def merge_dict(interface_dict, partial_dict): |
| 268 """Returns list of interface information dictioary. |
| 269 Args: |
| 270 interface_dict: interface node dictionary |
| 271 partial_dict: partial interface node dictionary |
| 272 Returns: |
| 273 list which is list of interface node's dictionry merged with partial inter
face node |
| 274 """ |
| 275 for key in partial_dict.keys(): |
| 276 if key in interface_dict: |
| 277 interface_dict[key]['Attributes'].append(partial_dict[key]['Attribut
es']) |
| 278 interface_dict[key]['Operations'].append(partial_dict[key]['Operatio
ns']) |
| 279 interface_dict[key]['Consts'].append(partial_dict[key]['Consts']) |
| 280 interface_dict[key].setdefault('Partial_FilePaths', []).append(parti
al_dict[key]['FilePath']) |
| 281 return interface_dict |
| 282 |
| 283 |
| 284 def export_to_jsonfile(dictionary, json_file): |
| 285 """Returns jsonfile which is dumped each interface information dictionary to
json. |
| 286 Args: |
| 287 dictioary: interface dictionary |
| 288 json_file: json file for output |
| 289 Returns: |
| 290 json file which is contained each interface node dictionary |
| 291 """ |
| 292 with open(json_file, 'w') as f: |
| 293 json.dump(dictionary, f, sort_keys=True) |
| 294 |
| 295 |
| 296 def main(args): |
| 297 path_file = args[0] |
| 298 json_file = args[1] |
| 299 file_to_list = utilities.read_file_to_list(path_file) |
| 300 interface_dict = {interface.GetName(): interface_to_dict(interface) for inte
rface in filter_non_partial(get_interfaces(file_to_list))} |
| 301 partial_dict = {interface.GetName(): interface_to_dict(interface) for interf
ace in filter_partial(get_interfaces(file_to_list))} |
| 302 dictionary = merge_dict(interface_dict, partial_dict) |
| 303 export_to_jsonfile(dictionary, json_file) |
| 304 |
| 305 |
| 306 if __name__ == '__main__': |
| 307 main(sys.argv[1:]) |
OLD | NEW |