Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 #! /usr/bin/env python | 1 #! /usr/bin/env python |
| 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
| 5 | 5 |
| 6 import itertools | 6 import itertools |
| 7 import json | 7 import json |
| 8 import os.path | 8 import os.path |
| 9 import re | 9 import re |
| 10 import sys | 10 import sys |
| (...skipping 325 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 336 dictionary that the JSON schema compiler expects to see. | 336 dictionary that the JSON schema compiler expects to see. |
| 337 ''' | 337 ''' |
| 338 | 338 |
| 339 def __init__(self, | 339 def __init__(self, |
| 340 namespace_node, | 340 namespace_node, |
| 341 description, | 341 description, |
| 342 nodoc=False, | 342 nodoc=False, |
| 343 internal=False, | 343 internal=False, |
| 344 platforms=None, | 344 platforms=None, |
| 345 compiler_options=None, | 345 compiler_options=None, |
| 346 deprecated=None): | 346 deprecated=None, |
| 347 documentation_options=None): | |
| 347 self.namespace = namespace_node | 348 self.namespace = namespace_node |
| 348 self.nodoc = nodoc | 349 self.nodoc = nodoc |
| 349 self.internal = internal | 350 self.internal = internal |
| 350 self.platforms = platforms | 351 self.platforms = platforms |
| 351 self.compiler_options = compiler_options | 352 self.compiler_options = compiler_options |
| 352 self.events = [] | 353 self.events = [] |
| 353 self.functions = [] | 354 self.functions = [] |
| 354 self.types = [] | 355 self.types = [] |
| 355 self.callbacks = OrderedDict() | 356 self.callbacks = OrderedDict() |
| 356 self.description = description | 357 self.description = description |
| 357 self.deprecated = deprecated | 358 self.deprecated = deprecated |
| 359 self.documentation_options = documentation_options | |
| 358 | 360 |
| 359 def process(self): | 361 def process(self): |
| 360 for node in self.namespace.GetChildren(): | 362 for node in self.namespace.GetChildren(): |
| 361 if node.cls == 'Dictionary': | 363 if node.cls == 'Dictionary': |
| 362 self.types.append(Dictionary(node).process(self.callbacks)) | 364 self.types.append(Dictionary(node).process(self.callbacks)) |
| 363 elif node.cls == 'Callback': | 365 elif node.cls == 'Callback': |
| 364 k, v = Member(node).process(self.callbacks) | 366 k, v = Member(node).process(self.callbacks) |
| 365 self.callbacks[k] = v | 367 self.callbacks[k] = v |
| 366 elif node.cls == 'Interface' and node.GetName() == 'Functions': | 368 elif node.cls == 'Interface' and node.GetName() == 'Functions': |
| 367 self.functions = self.process_interface(node) | 369 self.functions = self.process_interface(node) |
| 368 elif node.cls == 'Interface' and node.GetName() == 'Events': | 370 elif node.cls == 'Interface' and node.GetName() == 'Events': |
| 369 self.events = self.process_interface(node) | 371 self.events = self.process_interface(node) |
| 370 elif node.cls == 'Enum': | 372 elif node.cls == 'Enum': |
| 371 self.types.append(Enum(node).process()) | 373 self.types.append(Enum(node).process()) |
| 372 else: | 374 else: |
| 373 sys.exit('Did not process %s %s' % (node.cls, node)) | 375 sys.exit('Did not process %s %s' % (node.cls, node)) |
| 374 if self.compiler_options is not None: | 376 if self.compiler_options is not None: |
|
not at google - send to devlin
2015/03/03 23:50:59
We should write both of these like:
compiler_opti
lfg
2015/03/04 21:13:02
Done.
| |
| 375 compiler_options = self.compiler_options | 377 compiler_options = self.compiler_options |
| 376 else: | 378 else: |
| 377 compiler_options = {} | 379 compiler_options = {} |
| 380 if self.documentation_options is not None: | |
| 381 documentation_options = self.documentation_options | |
| 382 else: | |
| 383 documentation_options = {} | |
| 378 return {'namespace': self.namespace.GetName(), | 384 return {'namespace': self.namespace.GetName(), |
| 379 'description': self.description, | 385 'description': self.description, |
| 380 'nodoc': self.nodoc, | 386 'nodoc': self.nodoc, |
| 381 'types': self.types, | 387 'types': self.types, |
| 382 'functions': self.functions, | 388 'functions': self.functions, |
| 383 'internal': self.internal, | 389 'internal': self.internal, |
| 384 'events': self.events, | 390 'events': self.events, |
| 385 'platforms': self.platforms, | 391 'platforms': self.platforms, |
| 386 'compiler_options': compiler_options, | 392 'compiler_options': compiler_options, |
| 387 'deprecated': self.deprecated} | 393 'deprecated': self.deprecated, |
| 394 'documentation_options': documentation_options} | |
| 388 | 395 |
| 389 def process_interface(self, node): | 396 def process_interface(self, node): |
| 390 members = [] | 397 members = [] |
| 391 for member in node.GetChildren(): | 398 for member in node.GetChildren(): |
| 392 if member.cls == 'Member': | 399 if member.cls == 'Member': |
| 393 _, properties = Member(member).process(self.callbacks) | 400 _, properties = Member(member).process(self.callbacks) |
| 394 members.append(properties) | 401 members.append(properties) |
| 395 return members | 402 return members |
| 396 | 403 |
| 397 | 404 |
| 398 class IDLSchema(object): | 405 class IDLSchema(object): |
| 399 ''' | 406 ''' |
| 400 Given a list of IDLNodes and IDLAttributes, converts into a Python list | 407 Given a list of IDLNodes and IDLAttributes, converts into a Python list |
| 401 of api_defs that the JSON schema compiler expects to see. | 408 of api_defs that the JSON schema compiler expects to see. |
| 402 ''' | 409 ''' |
| 403 | 410 |
| 404 def __init__(self, idl): | 411 def __init__(self, idl): |
| 405 self.idl = idl | 412 self.idl = idl |
| 406 | 413 |
| 407 def process(self): | 414 def process(self): |
| 408 namespaces = [] | 415 namespaces = [] |
| 409 nodoc = False | 416 nodoc = False |
| 410 internal = False | 417 internal = False |
| 411 description = None | 418 description = None |
| 412 platforms = None | 419 platforms = None |
| 413 compiler_options = {} | 420 compiler_options = {} |
| 414 deprecated = None | 421 deprecated = None |
| 422 documentation_options = {} | |
| 415 for node in self.idl: | 423 for node in self.idl: |
| 416 if node.cls == 'Namespace': | 424 if node.cls == 'Namespace': |
| 417 if not description: | 425 if not description: |
| 418 # TODO(kalman): Go back to throwing an error here. | 426 # TODO(kalman): Go back to throwing an error here. |
| 419 print('%s must have a namespace-level comment. This will ' | 427 print('%s must have a namespace-level comment. This will ' |
| 420 'appear on the API summary page.' % node.GetName()) | 428 'appear on the API summary page.' % node.GetName()) |
| 421 description = '' | 429 description = '' |
| 422 namespace = Namespace(node, description, nodoc, internal, | 430 namespace = Namespace(node, description, nodoc, internal, |
| 423 platforms=platforms, | 431 platforms=platforms, |
| 424 compiler_options=compiler_options or None, | 432 compiler_options=compiler_options or None, |
| 425 deprecated=deprecated) | 433 deprecated=deprecated, |
| 434 documentation_options=documentation_options) | |
| 426 namespaces.append(namespace.process()) | 435 namespaces.append(namespace.process()) |
| 427 nodoc = False | 436 nodoc = False |
| 428 internal = False | 437 internal = False |
| 429 platforms = None | 438 platforms = None |
| 430 compiler_options = None | 439 compiler_options = None |
| 431 elif node.cls == 'Copyright': | 440 elif node.cls == 'Copyright': |
| 432 continue | 441 continue |
| 433 elif node.cls == 'Comment': | 442 elif node.cls == 'Comment': |
| 434 description = node.GetName() | 443 description = node.GetName() |
| 435 elif node.cls == 'ExtAttribute': | 444 elif node.cls == 'ExtAttribute': |
| 436 if node.name == 'nodoc': | 445 if node.name == 'nodoc': |
| 437 nodoc = bool(node.value) | 446 nodoc = bool(node.value) |
| 438 elif node.name == 'internal': | 447 elif node.name == 'internal': |
| 439 internal = bool(node.value) | 448 internal = bool(node.value) |
| 440 elif node.name == 'platforms': | 449 elif node.name == 'platforms': |
| 441 platforms = list(node.value) | 450 platforms = list(node.value) |
| 442 elif node.name == 'implemented_in': | 451 elif node.name == 'implemented_in': |
| 443 compiler_options['implemented_in'] = node.value | 452 compiler_options['implemented_in'] = node.value |
| 444 elif node.name == 'camel_case_enum_to_string': | 453 elif node.name == 'camel_case_enum_to_string': |
| 445 compiler_options['camel_case_enum_to_string'] = node.value | 454 compiler_options['camel_case_enum_to_string'] = node.value |
| 446 elif node.name == 'deprecated': | 455 elif node.name == 'deprecated': |
| 447 deprecated = str(node.value) | 456 deprecated = str(node.value) |
| 457 elif node.name == 'documentation_options_title': | |
| 458 documentation_options['title'] = node.value | |
| 459 elif node.name == 'documentation_options_namespace': | |
| 460 documentation_options['namespace'] = node.value | |
| 461 elif node.name == 'documentation_options_documented_in': | |
| 462 documentation_options['documented_in'] = node.value | |
|
not at google - send to devlin
2015/03/03 23:50:59
Let's make this a little less wordy:
documentation
lfg
2015/03/04 21:13:02
Done.
| |
| 448 else: | 463 else: |
| 449 continue | 464 continue |
| 450 else: | 465 else: |
| 451 sys.exit('Did not process %s %s' % (node.cls, node)) | 466 sys.exit('Did not process %s %s' % (node.cls, node)) |
| 452 return namespaces | 467 return namespaces |
| 453 | 468 |
| 454 | 469 |
| 455 def Load(filename): | 470 def Load(filename): |
| 456 ''' | 471 ''' |
| 457 Given the filename of an IDL file, parses it and returns an equivalent | 472 Given the filename of an IDL file, parses it and returns an equivalent |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 478 print json.dumps(schema, indent=2) | 493 print json.dumps(schema, indent=2) |
| 479 else: | 494 else: |
| 480 contents = sys.stdin.read() | 495 contents = sys.stdin.read() |
| 481 idl = idl_parser.IDLParser().ParseData(contents, '<stdin>') | 496 idl = idl_parser.IDLParser().ParseData(contents, '<stdin>') |
| 482 schema = IDLSchema(idl).process() | 497 schema = IDLSchema(idl).process() |
| 483 print json.dumps(schema, indent=2) | 498 print json.dumps(schema, indent=2) |
| 484 | 499 |
| 485 | 500 |
| 486 if __name__ == '__main__': | 501 if __name__ == '__main__': |
| 487 Main() | 502 Main() |
| OLD | NEW |