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 306 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 317 ''' | 317 ''' |
| 318 Given an IDLNode representing an IDL namespace, converts into a Python | 318 Given an IDLNode representing an IDL namespace, converts into a Python |
| 319 dictionary that the JSON schema compiler expects to see. | 319 dictionary that the JSON schema compiler expects to see. |
| 320 ''' | 320 ''' |
| 321 | 321 |
| 322 def __init__(self, | 322 def __init__(self, |
| 323 namespace_node, | 323 namespace_node, |
| 324 description, | 324 description, |
| 325 nodoc=False, | 325 nodoc=False, |
| 326 internal=False, | 326 internal=False, |
| 327 platforms=None): | 327 platforms=None, |
| 328 compiler_options={}): | |
|
not at google - send to devlin
2013/11/02 00:01:52
don't put objects as default values. they're share
Haojian Wu
2013/11/02 01:27:33
Done.
| |
| 328 self.namespace = namespace_node | 329 self.namespace = namespace_node |
| 329 self.nodoc = nodoc | 330 self.nodoc = nodoc |
| 330 self.internal = internal | 331 self.internal = internal |
| 331 self.platforms = platforms | 332 self.platforms = platforms |
| 333 self.compiler_options = compiler_options | |
| 332 self.events = [] | 334 self.events = [] |
| 333 self.functions = [] | 335 self.functions = [] |
| 334 self.types = [] | 336 self.types = [] |
| 335 self.callbacks = OrderedDict() | 337 self.callbacks = OrderedDict() |
| 336 self.description = description | 338 self.description = description |
| 337 | 339 |
| 338 def process(self): | 340 def process(self): |
| 339 for node in self.namespace.children: | 341 for node in self.namespace.children: |
| 340 if node.cls == 'Dictionary': | 342 if node.cls == 'Dictionary': |
| 341 self.types.append(Dictionary(node).process(self.callbacks)) | 343 self.types.append(Dictionary(node).process(self.callbacks)) |
| 342 elif node.cls == 'Callback': | 344 elif node.cls == 'Callback': |
| 343 k, v = Member(node).process(self.callbacks) | 345 k, v = Member(node).process(self.callbacks) |
| 344 self.callbacks[k] = v | 346 self.callbacks[k] = v |
| 345 elif node.cls == 'Interface' and node.GetName() == 'Functions': | 347 elif node.cls == 'Interface' and node.GetName() == 'Functions': |
| 346 self.functions = self.process_interface(node) | 348 self.functions = self.process_interface(node) |
| 347 elif node.cls == 'Interface' and node.GetName() == 'Events': | 349 elif node.cls == 'Interface' and node.GetName() == 'Events': |
| 348 self.events = self.process_interface(node) | 350 self.events = self.process_interface(node) |
| 349 elif node.cls == 'Enum': | 351 elif node.cls == 'Enum': |
| 350 self.types.append(Enum(node).process(self.callbacks)) | 352 self.types.append(Enum(node).process(self.callbacks)) |
| 351 else: | 353 else: |
| 352 sys.exit('Did not process %s %s' % (node.cls, node)) | 354 sys.exit('Did not process %s %s' % (node.cls, node)) |
| 353 return {'namespace': self.namespace.GetName(), | 355 return {'namespace': self.namespace.GetName(), |
| 354 'description': self.description, | 356 'description': self.description, |
| 355 'nodoc': self.nodoc, | 357 'nodoc': self.nodoc, |
| 356 'types': self.types, | 358 'types': self.types, |
| 357 'functions': self.functions, | 359 'functions': self.functions, |
| 358 'internal': self.internal, | 360 'internal': self.internal, |
| 359 'events': self.events, | 361 'events': self.events, |
| 360 'platforms': self.platforms} | 362 'platforms': self.platforms, |
| 363 'compiler_options': self.compiler_options} | |
| 361 | 364 |
| 362 def process_interface(self, node): | 365 def process_interface(self, node): |
| 363 members = [] | 366 members = [] |
| 364 for member in node.children: | 367 for member in node.children: |
| 365 if member.cls == 'Member': | 368 if member.cls == 'Member': |
| 366 name, properties = Member(member).process(self.callbacks) | 369 name, properties = Member(member).process(self.callbacks) |
| 367 members.append(properties) | 370 members.append(properties) |
| 368 return members | 371 return members |
| 369 | 372 |
| 370 | 373 |
| 371 class IDLSchema(object): | 374 class IDLSchema(object): |
| 372 ''' | 375 ''' |
| 373 Given a list of IDLNodes and IDLAttributes, converts into a Python list | 376 Given a list of IDLNodes and IDLAttributes, converts into a Python list |
| 374 of api_defs that the JSON schema compiler expects to see. | 377 of api_defs that the JSON schema compiler expects to see. |
| 375 ''' | 378 ''' |
| 376 | 379 |
| 377 def __init__(self, idl): | 380 def __init__(self, idl): |
| 378 self.idl = idl | 381 self.idl = idl |
| 379 | 382 |
| 380 def process(self): | 383 def process(self): |
| 381 namespaces = [] | 384 namespaces = [] |
| 382 nodoc = False | 385 nodoc = False |
| 383 internal = False | 386 internal = False |
| 384 description = None | 387 description = None |
| 385 platforms = None | 388 platforms = None |
| 389 compiler_options = {} | |
|
not at google - send to devlin
2013/11/02 00:01:52
=None?
Haojian Wu
2013/11/02 01:27:33
Done.
| |
| 386 for node in self.idl: | 390 for node in self.idl: |
| 387 if node.cls == 'Namespace': | 391 if node.cls == 'Namespace': |
| 388 if not description: | 392 if not description: |
| 389 # TODO(kalman): Go back to throwing an error here. | 393 # TODO(kalman): Go back to throwing an error here. |
| 390 print('%s must have a namespace-level comment. This will ' | 394 print('%s must have a namespace-level comment. This will ' |
| 391 'appear on the API summary page.' % node.GetName()) | 395 'appear on the API summary page.' % node.GetName()) |
| 392 description = '' | 396 description = '' |
| 393 namespace = Namespace(node, description, nodoc, internal, platforms) | 397 namespace = Namespace(node, description, nodoc, internal, platforms, |
| 398 compiler_options) | |
|
not at google - send to devlin
2013/11/02 00:01:52
name optional arguments. platforms=platforms, comp
Haojian Wu
2013/11/02 01:27:33
Done.
| |
| 394 namespaces.append(namespace.process()) | 399 namespaces.append(namespace.process()) |
| 395 nodoc = False | 400 nodoc = False |
| 396 internal = False | 401 internal = False |
| 397 platforms = None | 402 platforms = None |
|
not at google - send to devlin
2013/11/02 00:01:52
reset compiler_options here?
Haojian Wu
2013/11/02 01:27:33
Done.
| |
| 398 elif node.cls == 'Copyright': | 403 elif node.cls == 'Copyright': |
| 399 continue | 404 continue |
| 400 elif node.cls == 'Comment': | 405 elif node.cls == 'Comment': |
| 401 description = node.GetName() | 406 description = node.GetName() |
| 402 elif node.cls == 'ExtAttribute': | 407 elif node.cls == 'ExtAttribute': |
| 403 if node.name == 'nodoc': | 408 if node.name == 'nodoc': |
| 404 nodoc = bool(node.value) | 409 nodoc = bool(node.value) |
| 405 elif node.name == 'internal': | 410 elif node.name == 'internal': |
| 406 internal = bool(node.value) | 411 internal = bool(node.value) |
| 407 elif node.name == 'platforms': | 412 elif node.name == 'platforms': |
| 408 platforms = list(node.value) | 413 platforms = list(node.value) |
| 414 elif node.name == 'implemented_in': | |
| 415 compiler_options = dict(implemented_in=node.value) | |
|
not at google - send to devlin
2013/11/02 00:01:52
personally I would prefer {'implemented_in': node.
Haojian Wu
2013/11/02 01:27:33
Done.
| |
| 409 else: | 416 else: |
| 410 continue | 417 continue |
| 411 else: | 418 else: |
| 412 sys.exit('Did not process %s %s' % (node.cls, node)) | 419 sys.exit('Did not process %s %s' % (node.cls, node)) |
| 413 return namespaces | 420 return namespaces |
| 414 | 421 |
| 415 | 422 |
| 416 def Load(filename): | 423 def Load(filename): |
| 417 ''' | 424 ''' |
| 418 Given the filename of an IDL file, parses it and returns an equivalent | 425 Given the filename of an IDL file, parses it and returns an equivalent |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 433 Dump a json serialization of parse result for the IDL files whose names | 440 Dump a json serialization of parse result for the IDL files whose names |
| 434 were passed in on the command line. | 441 were passed in on the command line. |
| 435 ''' | 442 ''' |
| 436 for filename in sys.argv[1:]: | 443 for filename in sys.argv[1:]: |
| 437 schema = Load(filename) | 444 schema = Load(filename) |
| 438 print json.dumps(schema, indent=2) | 445 print json.dumps(schema, indent=2) |
| 439 | 446 |
| 440 | 447 |
| 441 if __name__ == '__main__': | 448 if __name__ == '__main__': |
| 442 Main() | 449 Main() |
| OLD | NEW |