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 This script collects and organizes interface information and that information du
mps into json file. |
| 9 """ |
| 10 |
| 11 import json |
| 12 import os |
| 13 import sys |
| 14 import utilities |
| 15 |
| 16 |
| 17 from blink_idl_parser import parse_file, BlinkIDLParser |
| 18 |
| 19 _INTERFACE_CLASS_NAME = 'Interface' |
| 20 _IMPLEMENT_CLASS_NAME = 'Implements' |
| 21 _PARTIAL = 'Partial' |
| 22 _STRIP_FILEPATH = '../chromium/src/third_party/WebKit' |
| 23 _NAME = 'Name' |
| 24 _TYPE = 'Type' |
| 25 _FILEPATH = 'FilePath' |
| 26 _CONST = 'Const' |
| 27 _ATTRIBUTE = 'Attribute' |
| 28 _OPERATION = 'Operation' |
| 29 _ARGUMENT = 'Argument' |
| 30 _EXTATTRIBUTE = 'ExtAttribute' |
| 31 _INHERIT = 'Inherit' |
| 32 _MEMBERS = ['Consts', 'Attributes', 'Operations'] |
| 33 |
| 34 |
| 35 def get_definitions(paths): |
| 36 """Returns IDL node. |
| 37 Args: |
| 38 paths: list of IDL file path |
| 39 Returns: |
| 40 a generator which yields IDL node objects |
| 41 """ |
| 42 parser = BlinkIDLParser() |
| 43 for path in paths: |
| 44 definitions = parse_file(parser, path) |
| 45 for definition in definitions.GetChildren(): |
| 46 yield definition |
| 47 |
| 48 |
| 49 def is_implements(definition): |
| 50 """Returns True if class of |definition| is Implement, otherwise False. |
| 51 Args: |
| 52 definition: IDL node |
| 53 Returns: |
| 54 boolean |
| 55 """ |
| 56 return definition.GetClass() == _IMPLEMENT_CLASS_NAME |
| 57 |
| 58 |
| 59 def is_non_partial(definition): |
| 60 """Returns True if class of |definition| is 'interface' class and not 'parti
al' interface, otherwise False. |
| 61 Args: |
| 62 definition: IDL node |
| 63 Returns: |
| 64 boolean |
| 65 """ |
| 66 if definition.GetClass() == _INTERFACE_CLASS_NAME and not definition.GetProp
erty(_PARTIAL): |
| 67 return True |
| 68 else: |
| 69 return False |
| 70 |
| 71 |
| 72 def is_partial(definition): |
| 73 """Returns True if |definition| is 'partial interface' class, otherwise Fals
e. |
| 74 Args: |
| 75 definition: IDL node |
| 76 Return: |
| 77 boolean |
| 78 """ |
| 79 if definition.GetClass() == _INTERFACE_CLASS_NAME and definition.GetProperty
(_PARTIAL): |
| 80 return True |
| 81 else: |
| 82 return False |
| 83 |
| 84 |
| 85 def get_filepath(interface_node): |
| 86 """Returns relative path to the IDL in which "interface_node| is defined. |
| 87 Args: |
| 88 interface_node: IDL interface |
| 89 Returns: |
| 90 str which is |interface_node|'s file path |
| 91 """ |
| 92 filename = interface_node.GetProperty('FILENAME') |
| 93 return os.path.relpath(filename).strip(_STRIP_FILEPATH) |
| 94 |
| 95 |
| 96 def get_const_node_list(interface_node): |
| 97 """Returns Const node. |
| 98 Args: |
| 99 interface_node: interface node |
| 100 Returns: |
| 101 list of const node |
| 102 """ |
| 103 return interface_node.GetListOf(_CONST) |
| 104 |
| 105 |
| 106 def get_const_type(const_node): |
| 107 """Returns const's type. |
| 108 Args: |
| 109 const_node: const node |
| 110 Returns: |
| 111 node.GetChildren()[0].GetName(): str which is constant type |
| 112 """ |
| 113 return const_node.GetChildren()[0].GetName() |
| 114 |
| 115 |
| 116 def get_const_value(const_node): |
| 117 """Returns const's value. |
| 118 Args: |
| 119 const_node: const node |
| 120 Returns: |
| 121 node.GetChildren()[1].GetName(): name of oparation's value |
| 122 """ |
| 123 return const_node.GetChildren()[1].GetName() |
| 124 |
| 125 |
| 126 def const_node_to_dict(const_node): |
| 127 """Returns dictionary of const's information. |
| 128 Args: |
| 129 const_node: const node |
| 130 Returns: |
| 131 dictionary of const's information |
| 132 """ |
| 133 return { |
| 134 _NAME: const_node.GetName(), |
| 135 _TYPE: get_const_type(const_node), |
| 136 'Value': get_const_value(const_node), |
| 137 'ExtAttributes': [extattr_node_to_dict(extattr) for extattr in get_extat
tribute_node_list(const_node)], |
| 138 } |
| 139 |
| 140 |
| 141 def get_attribute_node_list(interface_node): |
| 142 """Returns list of Attribute if the interface have one. |
| 143 Args: |
| 144 interface_node: interface node |
| 145 Returns: |
| 146 list of attribute node |
| 147 """ |
| 148 return interface_node.GetListOf(_ATTRIBUTE) |
| 149 |
| 150 |
| 151 def get_attribute_type(attribute_node): |
| 152 """Returns type of attribute. |
| 153 Args: |
| 154 attribute_node: attribute node |
| 155 Returns: |
| 156 name of attribute's type |
| 157 """ |
| 158 return attribute_node.GetOneOf(_TYPE).GetChildren()[0].GetName() |
| 159 |
| 160 |
| 161 get_operation_type = get_attribute_type |
| 162 get_argument_type = get_attribute_type |
| 163 |
| 164 |
| 165 def attribute_node_to_dict(attribute_node): |
| 166 """Returns dictioary of attribute's information. |
| 167 Args: |
| 168 attribute_node: attribute node |
| 169 Returns: |
| 170 dictionary of attribute's information |
| 171 """ |
| 172 return { |
| 173 _NAME: attribute_node.GetName(), |
| 174 _TYPE: get_attribute_type(attribute_node), |
| 175 'ExtAttributes': [extattr_node_to_dict(extattr) for extattr in get_extat
tribute_node_list(attribute_node)], |
| 176 'Readonly': attribute_node.GetProperty('READONLY', default=False), |
| 177 'Static': attribute_node.GetProperty('STATIC', default=False), |
| 178 } |
| 179 |
| 180 |
| 181 def get_operation_node_list(interface_node): |
| 182 """Returns operations node list. |
| 183 Args: |
| 184 interface_node: interface node |
| 185 Returns: |
| 186 list of oparation node |
| 187 """ |
| 188 return interface_node.GetListOf(_OPERATION) |
| 189 |
| 190 |
| 191 def get_argument_node_list(operation_node): |
| 192 """Returns list of argument. |
| 193 Args: |
| 194 operation_node: operation node |
| 195 Returns: |
| 196 list of argument node |
| 197 """ |
| 198 return operation_node.GetOneOf('Arguments').GetListOf(_ARGUMENT) |
| 199 |
| 200 |
| 201 def argument_node_to_dict(argument_node): |
| 202 """Returns dictionary of argument's information. |
| 203 Args: |
| 204 argument_node: argument node |
| 205 Returns: |
| 206 dictionary of argument's information |
| 207 """ |
| 208 return { |
| 209 _NAME: argument_node.GetName(), |
| 210 _TYPE: get_argument_type(argument_node), |
| 211 } |
| 212 |
| 213 |
| 214 def get_operation_name(operation_node): |
| 215 """Returns openration's name. |
| 216 Args: |
| 217 operation_node: operation node |
| 218 Returns: |
| 219 name of operation |
| 220 """ |
| 221 if operation_node.GetProperty('GETTER'): |
| 222 return '__getter__' |
| 223 elif operation_node.GetProperty('SETTER'): |
| 224 return '__setter__' |
| 225 elif operation_node.GetProperty('DELETER'): |
| 226 return '__deleter__' |
| 227 else: |
| 228 return operation_node.GetName() |
| 229 |
| 230 |
| 231 def operation_node_to_dict(operation_node): |
| 232 """Returns dictionary of operation's information. |
| 233 Args: |
| 234 operation_node: operation node |
| 235 Returns: |
| 236 dictionary of operation's informantion |
| 237 """ |
| 238 return { |
| 239 _NAME: get_operation_name(operation_node), |
| 240 'Arguments': [argument_node_to_dict(argument) for argument in get_argume
nt_node_list(operation_node) if argument_node_to_dict(argument)], |
| 241 _TYPE: get_operation_type(operation_node), |
| 242 'ExtAttributes': [extattr_node_to_dict(extattr) for extattr in get_extat
tribute_node_list(operation_node)], |
| 243 'Static': operation_node.GetProperty('STATIC', default=False), |
| 244 } |
| 245 |
| 246 |
| 247 def get_extattribute_node_list(node): |
| 248 """Returns list of ExtAttribute. |
| 249 Args: |
| 250 node: IDL node |
| 251 Returns: |
| 252 list of ExtAttrbute |
| 253 """ |
| 254 if node.GetOneOf('ExtAttributes'): |
| 255 return node.GetOneOf('ExtAttributes').GetListOf(_EXTATTRIBUTE) |
| 256 else: |
| 257 return [] |
| 258 |
| 259 |
| 260 def extattr_node_to_dict(extattr): |
| 261 """Returns dictionary of ExtAttribute's information. |
| 262 Args: |
| 263 extattr: ExtAttribute node |
| 264 Returns: |
| 265 dictionary of ExtAttribute's information |
| 266 """ |
| 267 return { |
| 268 _NAME: extattr.GetName(), |
| 269 } |
| 270 |
| 271 |
| 272 def inherit_node_to_dict(interface_node): |
| 273 """Returns dictionary of inherit. |
| 274 Args: |
| 275 interface_node: interface node |
| 276 Returns: |
| 277 dictioanry of inherit's information |
| 278 """ |
| 279 inherit = interface_node.GetOneOf(_INHERIT) |
| 280 if inherit: |
| 281 return {'Parent': inherit.GetName()} |
| 282 else: |
| 283 return [] |
| 284 |
| 285 |
| 286 def interface_node_to_dict(interface_node): |
| 287 """Returns dictioary whose key is interface name and value is dictioary of i
nterface's information. |
| 288 Args: |
| 289 interface_node: interface node |
| 290 Returns: |
| 291 dictionary, {interface name: interface information dictionary} |
| 292 """ |
| 293 return { |
| 294 _NAME: interface_node.GetName(), |
| 295 _FILEPATH: get_filepath(interface_node), |
| 296 'Consts': [const_node_to_dict(const) for const in get_const_node_list(in
terface_node)], |
| 297 'Attributes': [attribute_node_to_dict(attr) for attr in get_attribute_no
de_list(interface_node) if attr], |
| 298 'Operations': [operation_node_to_dict(operation) for operation in get_op
eration_node_list(interface_node) if operation], |
| 299 'ExtAttributes': [extattr_node_to_dict(extattr) for extattr in get_extat
tribute_node_list(interface_node)], |
| 300 _INHERIT: inherit_node_to_dict(interface_node) |
| 301 } |
| 302 |
| 303 |
| 304 def merge_partial_dicts(interfaces_dict, partials_dict): |
| 305 """Returns interface information dictioary. |
| 306 Args: |
| 307 interfaces_dict: interface node dictionary |
| 308 partial_dict: partial interface node dictionary |
| 309 Returns: |
| 310 a dictronary merged with interfaces_dict and partial_dict |
| 311 """ |
| 312 for interface_name, partial in partials_dict.iteritems(): |
| 313 interface = interfaces_dict.get(interface_name) |
| 314 if not interface: |
| 315 continue |
| 316 else: |
| 317 for member in _MEMBERS: |
| 318 interface[member].extend(partial.get(member)) |
| 319 interface.setdefault('Partial_FilePaths', []).append(partial[_FILEPA
TH]) |
| 320 return interfaces_dict |
| 321 |
| 322 |
| 323 def merge_implement_node(interfaces_dict, implement_nodes): |
| 324 """Returns dict of interface information combined with referenced interface
information |
| 325 Args: |
| 326 interfaces_dict: dict of interface information |
| 327 implement_nodes: list of implemented interface node |
| 328 Returns: |
| 329 interfaces_dict: dict of interface information combine into implements nod
e |
| 330 """ |
| 331 for implement in implement_nodes: |
| 332 reference = implement.GetProperty('REFERENCE') |
| 333 implement = implement.GetName() |
| 334 if not reference: |
| 335 continue |
| 336 else: |
| 337 for member in _MEMBERS: |
| 338 interfaces_dict[implement][member].extend(interfaces_dict[refere
nce].get(member)) |
| 339 return interfaces_dict |
| 340 |
| 341 |
| 342 # TODO(natsukoa): Remove indent |
| 343 def export_to_jsonfile(dictionary, json_file): |
| 344 """Returns jsonfile which is dumped each interface information dictionary to
json. |
| 345 Args: |
| 346 dictioary: interface dictionary |
| 347 json_file: json file for output |
| 348 Returns: |
| 349 json file which is contained each interface node dictionary |
| 350 """ |
| 351 with open(json_file, 'w') as f: |
| 352 json.dump(dictionary, f, sort_keys=True, indent=4) |
| 353 |
| 354 |
| 355 def main(args): |
| 356 path_file = args[0] |
| 357 json_file = args[1] |
| 358 path_list = utilities.read_file_to_list(path_file) |
| 359 implement_nodes = [definition |
| 360 for definition in get_definitions(path_list) |
| 361 if is_implements(definition)] |
| 362 interfaces_dict = {definition.GetName(): interface_node_to_dict(definition) |
| 363 for definition in get_definitions(path_list) |
| 364 if is_non_partial(definition)} |
| 365 partials_dict = {definition.GetName(): interface_node_to_dict(definition) |
| 366 for definition in get_definitions(path_list) |
| 367 if is_partial(definition)} |
| 368 dictionary = merge_partial_dicts(interfaces_dict, partials_dict) |
| 369 interfaces_dict = merge_implement_node(interfaces_dict, implement_nodes) |
| 370 export_to_jsonfile(dictionary, json_file) |
| 371 |
| 372 |
| 373 if __name__ == '__main__': |
| 374 main(sys.argv[1:]) |
OLD | NEW |