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 253 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 264 elif self.typeref == 'ArrayBuffer': | 264 elif self.typeref == 'ArrayBuffer': |
| 265 properties['type'] = 'binary' | 265 properties['type'] = 'binary' |
| 266 properties['isInstanceOf'] = 'ArrayBuffer' | 266 properties['isInstanceOf'] = 'ArrayBuffer' |
| 267 elif self.typeref == 'FileEntry': | 267 elif self.typeref == 'FileEntry': |
| 268 properties['type'] = 'object' | 268 properties['type'] = 'object' |
| 269 properties['isInstanceOf'] = 'FileEntry' | 269 properties['isInstanceOf'] = 'FileEntry' |
| 270 if 'additionalProperties' not in properties: | 270 if 'additionalProperties' not in properties: |
| 271 properties['additionalProperties'] = OrderedDict() | 271 properties['additionalProperties'] = OrderedDict() |
| 272 properties['additionalProperties']['type'] = 'any' | 272 properties['additionalProperties']['type'] = 'any' |
| 273 elif self.parent.GetPropertyLocal('Union'): | 273 elif self.parent.GetPropertyLocal('Union'): |
| 274 choices = [] | |
| 275 properties['choices'] = [Typeref(node.GetProperty('TYPEREF'), | 274 properties['choices'] = [Typeref(node.GetProperty('TYPEREF'), |
| 276 node, | 275 node, |
| 277 OrderedDict()).process(callbacks) | 276 OrderedDict()).process(callbacks) |
| 278 for node in self.parent.GetChildren() | 277 for node in self.parent.GetChildren() |
| 279 if node.cls == 'Option'] | 278 if node.cls == 'Option'] |
| 280 elif self.typeref is None: | 279 elif self.typeref is None: |
| 281 properties['type'] = 'function' | 280 properties['type'] = 'function' |
| 282 else: | 281 else: |
| 283 if self.typeref in callbacks: | 282 if self.typeref in callbacks: |
| 284 # Do not override name and description if they are already specified. | 283 # Do not override name and description if they are already specified. |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 296 | 295 |
| 297 class Enum(object): | 296 class Enum(object): |
| 298 ''' | 297 ''' |
| 299 Given an IDL Enum node, converts into a Python dictionary that the JSON | 298 Given an IDL Enum node, converts into a Python dictionary that the JSON |
| 300 schema compiler expects to see. | 299 schema compiler expects to see. |
| 301 ''' | 300 ''' |
| 302 def __init__(self, enum_node): | 301 def __init__(self, enum_node): |
| 303 self.node = enum_node | 302 self.node = enum_node |
| 304 self.description = '' | 303 self.description = '' |
| 305 | 304 |
| 306 def process(self, callbacks): | 305 def process(self): |
| 307 enum = [] | 306 enum = [] |
| 308 for node in self.node.GetChildren(): | 307 for node in self.node.GetChildren(): |
| 309 if node.cls == 'EnumItem': | 308 if node.cls == 'EnumItem': |
| 310 enum_value = {'name': node.GetName()} | 309 enum_value = {'name': node.GetName()} |
| 311 for child in node.GetChildren(): | 310 for child in node.GetChildren(): |
| 312 if child.cls == 'Comment': | 311 if child.cls == 'Comment': |
| 313 enum_value['description'] = ProcessComment(child.GetName())[0] | 312 enum_value['description'] = ProcessComment(child.GetName())[0] |
| 314 else: | 313 else: |
| 315 raise ValueError('Did not process %s %s' % (child.cls, child)) | 314 raise ValueError('Did not process %s %s' % (child.cls, child)) |
| 316 enum.append(enum_value) | 315 enum.append(enum_value) |
| 317 elif node.cls == 'Comment': | 316 elif node.cls == 'Comment': |
| 318 self.description = ProcessComment(node.GetName())[0] | 317 self.description = ProcessComment(node.GetName())[0] |
| 319 else: | 318 else: |
| 320 sys.exit('Did not process %s %s' % (node.cls, node)) | 319 sys.exit('Did not process %s %s' % (node.cls, node)) |
| 321 result = {'id' : self.node.GetName(), | 320 result = {'id' : self.node.GetName(), |
| 322 'description': self.description, | 321 'description': self.description, |
| 323 'type': 'string', | 322 'type': 'string', |
| 324 'enum': enum} | 323 'enum': enum} |
| 325 for property_name in ( | 324 for property_name in ( |
| 326 'inline_doc', 'noinline_doc', 'nodoc', 'cpp_enum_prefix_override',): | 325 'inline_doc', 'noinline_doc', 'nodoc', 'cpp_enum_prefix_override',): |
| 327 if self.node.GetProperty(property_name): | 326 if self.node.GetProperty(property_name): |
| 328 result[property_name] = self.node.GetProperty(property_name) | 327 result[property_name] = self.node.GetProperty(property_name) |
| 329 if self.node.GetProperty('deprecated'): | 328 if self.node.GetProperty('deprecated'): |
| 330 result[deprecated] = self.node.GetProperty('deprecated') | 329 result['deprecated'] = self.node.GetProperty('deprecated') |
| 331 return result | 330 return result |
| 332 | 331 |
| 333 | 332 |
| 334 class Namespace(object): | 333 class Namespace(object): |
| 335 ''' | 334 ''' |
| 336 Given an IDLNode representing an IDL namespace, converts into a Python | 335 Given an IDLNode representing an IDL namespace, converts into a Python |
| 337 dictionary that the JSON schema compiler expects to see. | 336 dictionary that the JSON schema compiler expects to see. |
| 338 ''' | 337 ''' |
| 339 | 338 |
| 340 def __init__(self, | 339 def __init__(self, |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 362 if node.cls == 'Dictionary': | 361 if node.cls == 'Dictionary': |
| 363 self.types.append(Dictionary(node).process(self.callbacks)) | 362 self.types.append(Dictionary(node).process(self.callbacks)) |
| 364 elif node.cls == 'Callback': | 363 elif node.cls == 'Callback': |
| 365 k, v = Member(node).process(self.callbacks) | 364 k, v = Member(node).process(self.callbacks) |
| 366 self.callbacks[k] = v | 365 self.callbacks[k] = v |
| 367 elif node.cls == 'Interface' and node.GetName() == 'Functions': | 366 elif node.cls == 'Interface' and node.GetName() == 'Functions': |
| 368 self.functions = self.process_interface(node) | 367 self.functions = self.process_interface(node) |
| 369 elif node.cls == 'Interface' and node.GetName() == 'Events': | 368 elif node.cls == 'Interface' and node.GetName() == 'Events': |
| 370 self.events = self.process_interface(node) | 369 self.events = self.process_interface(node) |
| 371 elif node.cls == 'Enum': | 370 elif node.cls == 'Enum': |
| 372 self.types.append(Enum(node).process(self.callbacks)) | 371 self.types.append(Enum(node).process()) |
| 373 else: | 372 else: |
| 374 sys.exit('Did not process %s %s' % (node.cls, node)) | 373 sys.exit('Did not process %s %s' % (node.cls, node)) |
| 375 if self.compiler_options is not None: | 374 if self.compiler_options is not None: |
| 376 compiler_options = self.compiler_options | 375 compiler_options = self.compiler_options |
| 377 else: | 376 else: |
| 378 compiler_options = {} | 377 compiler_options = {} |
| 379 return {'namespace': self.namespace.GetName(), | 378 return {'namespace': self.namespace.GetName(), |
| 380 'description': self.description, | 379 'description': self.description, |
| 381 'nodoc': self.nodoc, | 380 'nodoc': self.nodoc, |
| 382 'types': self.types, | 381 'types': self.types, |
| 383 'functions': self.functions, | 382 'functions': self.functions, |
| 384 'internal': self.internal, | 383 'internal': self.internal, |
| 385 'events': self.events, | 384 'events': self.events, |
| 386 'platforms': self.platforms, | 385 'platforms': self.platforms, |
| 387 'compiler_options': compiler_options, | 386 'compiler_options': compiler_options, |
| 388 'deprecated': self.deprecated} | 387 'deprecated': self.deprecated} |
| 389 | 388 |
| 390 def process_interface(self, node): | 389 def process_interface(self, node): |
| 391 members = [] | 390 members = [] |
| 392 for member in node.GetChildren(): | 391 for member in node.GetChildren(): |
| 393 if member.cls == 'Member': | 392 if member.cls == 'Member': |
| 394 name, properties = Member(member).process(self.callbacks) | 393 properties = Member(member).process(self.callbacks)[1] |
|
not at google - send to devlin
2015/01/14 22:44:41
ditto
| |
| 395 members.append(properties) | 394 members.append(properties) |
| 396 return members | 395 return members |
| 397 | 396 |
| 398 | 397 |
| 399 class IDLSchema(object): | 398 class IDLSchema(object): |
| 400 ''' | 399 ''' |
| 401 Given a list of IDLNodes and IDLAttributes, converts into a Python list | 400 Given a list of IDLNodes and IDLAttributes, converts into a Python list |
| 402 of api_defs that the JSON schema compiler expects to see. | 401 of api_defs that the JSON schema compiler expects to see. |
| 403 ''' | 402 ''' |
| 404 | 403 |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 479 print json.dumps(schema, indent=2) | 478 print json.dumps(schema, indent=2) |
| 480 else: | 479 else: |
| 481 contents = sys.stdin.read() | 480 contents = sys.stdin.read() |
| 482 idl = idl_parser.IDLParser().ParseData(contents, '<stdin>') | 481 idl = idl_parser.IDLParser().ParseData(contents, '<stdin>') |
| 483 schema = IDLSchema(idl).process() | 482 schema = IDLSchema(idl).process() |
| 484 print json.dumps(schema, indent=2) | 483 print json.dumps(schema, indent=2) |
| 485 | 484 |
| 486 | 485 |
| 487 if __name__ == '__main__': | 486 if __name__ == '__main__': |
| 488 Main() | 487 Main() |
| OLD | NEW |