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=None): | |
| 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': {} | |
| 364 if not self.compiler_options else self.compiler_options} | |
|
sergeygs
2013/11/02 06:20:44
I'd prefer:
'compiler_options': self.compiler_opt
Haojian Wu
2013/11/03 01:56:16
Done.
not at google - send to devlin
2013/11/04 15:16:49
The problem with "self.compiler_options or {}" is
| |
| 361 | 365 |
| 362 def process_interface(self, node): | 366 def process_interface(self, node): |
| 363 members = [] | 367 members = [] |
| 364 for member in node.children: | 368 for member in node.children: |
| 365 if member.cls == 'Member': | 369 if member.cls == 'Member': |
| 366 name, properties = Member(member).process(self.callbacks) | 370 name, properties = Member(member).process(self.callbacks) |
| 367 members.append(properties) | 371 members.append(properties) |
| 368 return members | 372 return members |
| 369 | 373 |
| 370 | 374 |
| 371 class IDLSchema(object): | 375 class IDLSchema(object): |
| 372 ''' | 376 ''' |
| 373 Given a list of IDLNodes and IDLAttributes, converts into a Python list | 377 Given a list of IDLNodes and IDLAttributes, converts into a Python list |
| 374 of api_defs that the JSON schema compiler expects to see. | 378 of api_defs that the JSON schema compiler expects to see. |
| 375 ''' | 379 ''' |
| 376 | 380 |
| 377 def __init__(self, idl): | 381 def __init__(self, idl): |
| 378 self.idl = idl | 382 self.idl = idl |
| 379 | 383 |
| 380 def process(self): | 384 def process(self): |
| 381 namespaces = [] | 385 namespaces = [] |
| 382 nodoc = False | 386 nodoc = False |
| 383 internal = False | 387 internal = False |
| 384 description = None | 388 description = None |
| 385 platforms = None | 389 platforms = None |
| 390 compiler_options = None | |
| 386 for node in self.idl: | 391 for node in self.idl: |
| 387 if node.cls == 'Namespace': | 392 if node.cls == 'Namespace': |
| 388 if not description: | 393 if not description: |
| 389 # TODO(kalman): Go back to throwing an error here. | 394 # TODO(kalman): Go back to throwing an error here. |
| 390 print('%s must have a namespace-level comment. This will ' | 395 print('%s must have a namespace-level comment. This will ' |
| 391 'appear on the API summary page.' % node.GetName()) | 396 'appear on the API summary page.' % node.GetName()) |
| 392 description = '' | 397 description = '' |
| 393 namespace = Namespace(node, description, nodoc, internal, platforms) | 398 namespace = Namespace(node, description, nodoc, internal, |
| 399 platforms=platforms, compiler_options=compiler_options) | |
|
sergeygs
2013/11/02 06:20:44
Align 'platform...' with 'node' in the preceding l
Haojian Wu
2013/11/03 01:56:16
Done. Thanks for the link.
| |
| 394 namespaces.append(namespace.process()) | 400 namespaces.append(namespace.process()) |
| 395 nodoc = False | 401 nodoc = False |
| 396 internal = False | 402 internal = False |
| 397 platforms = None | 403 platforms = None |
| 404 compiler_options = None | |
| 398 elif node.cls == 'Copyright': | 405 elif node.cls == 'Copyright': |
| 399 continue | 406 continue |
| 400 elif node.cls == 'Comment': | 407 elif node.cls == 'Comment': |
| 401 description = node.GetName() | 408 description = node.GetName() |
| 402 elif node.cls == 'ExtAttribute': | 409 elif node.cls == 'ExtAttribute': |
| 403 if node.name == 'nodoc': | 410 if node.name == 'nodoc': |
| 404 nodoc = bool(node.value) | 411 nodoc = bool(node.value) |
| 405 elif node.name == 'internal': | 412 elif node.name == 'internal': |
| 406 internal = bool(node.value) | 413 internal = bool(node.value) |
| 407 elif node.name == 'platforms': | 414 elif node.name == 'platforms': |
| 408 platforms = list(node.value) | 415 platforms = list(node.value) |
| 416 elif node.name == 'implemented_in': | |
| 417 compiler_options = {"implemented_in": node.value} | |
|
sergeygs
2013/11/02 06:20:44
Please use single quotes here: { 'implemented_in':
Haojian Wu
2013/11/03 01:56:16
Done. Thanks for the detailed explainations.
| |
| 409 else: | 418 else: |
| 410 continue | 419 continue |
| 411 else: | 420 else: |
| 412 sys.exit('Did not process %s %s' % (node.cls, node)) | 421 sys.exit('Did not process %s %s' % (node.cls, node)) |
| 413 return namespaces | 422 return namespaces |
| 414 | 423 |
| 415 | 424 |
| 416 def Load(filename): | 425 def Load(filename): |
| 417 ''' | 426 ''' |
| 418 Given the filename of an IDL file, parses it and returns an equivalent | 427 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 | 442 Dump a json serialization of parse result for the IDL files whose names |
| 434 were passed in on the command line. | 443 were passed in on the command line. |
| 435 ''' | 444 ''' |
| 436 for filename in sys.argv[1:]: | 445 for filename in sys.argv[1:]: |
| 437 schema = Load(filename) | 446 schema = Load(filename) |
| 438 print json.dumps(schema, indent=2) | 447 print json.dumps(schema, indent=2) |
| 439 | 448 |
| 440 | 449 |
| 441 if __name__ == '__main__': | 450 if __name__ == '__main__': |
| 442 Main() | 451 Main() |
| OLD | NEW |