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 |
11 | 11 |
12 from json_parse import OrderedDict | 12 from json_parse import OrderedDict |
13 import schema_util | |
14 | 13 |
15 # This file is a peer to json_schema.py. Each of these files understands a | 14 # This file is a peer to json_schema.py. Each of these files understands a |
16 # certain format describing APIs (either JSON or IDL), reads files written | 15 # certain format describing APIs (either JSON or IDL), reads files written |
17 # in that format into memory, and emits them as a Python array of objects | 16 # in that format into memory, and emits them as a Python array of objects |
18 # corresponding to those APIs, where the objects are formatted in a way that | 17 # corresponding to those APIs, where the objects are formatted in a way that |
19 # the JSON schema compiler understands. compiler.py drives both idl_schema.py | 18 # the JSON schema compiler understands. compiler.py drives both idl_schema.py |
20 # and json_schema.py. | 19 # and json_schema.py. |
21 | 20 |
22 # idl_parser expects to be able to import certain files in its directory, | 21 # idl_parser expects to be able to import certain files in its directory, |
23 # so let's set things up the way it wants. | 22 # so let's set things up the way it wants. |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
99 parameters = [] | 98 parameters = [] |
100 return_type = None | 99 return_type = None |
101 if self.node.GetProperty('TYPEREF') not in ('void', None): | 100 if self.node.GetProperty('TYPEREF') not in ('void', None): |
102 return_type = Typeref(self.node.GetProperty('TYPEREF'), | 101 return_type = Typeref(self.node.GetProperty('TYPEREF'), |
103 self.node, | 102 self.node, |
104 {'name': self.node.GetName()}).process(callbacks) | 103 {'name': self.node.GetName()}).process(callbacks) |
105 # The IDL parser doesn't allow specifying return types as optional. | 104 # The IDL parser doesn't allow specifying return types as optional. |
106 # Instead we infer any object return values to be optional. | 105 # Instead we infer any object return values to be optional. |
107 # TODO(asargent): fix the IDL parser to support optional return types. | 106 # TODO(asargent): fix the IDL parser to support optional return types. |
108 if return_type.get('type') == 'object' or '$ref' in return_type: | 107 if return_type.get('type') == 'object' or '$ref' in return_type: |
109 return_type['optional'] = True; | 108 return_type['optional'] = True |
110 for node in self.node.children: | 109 for node in self.node.children: |
111 parameter = Param(node).process(callbacks) | 110 parameter = Param(node).process(callbacks) |
112 if parameter['name'] in self.comment: | 111 if parameter['name'] in self.comment: |
113 parameter['description'] = self.comment[parameter['name']] | 112 parameter['description'] = self.comment[parameter['name']] |
114 parameters.append(parameter) | 113 parameters.append(parameter) |
115 return (self.node.GetName(), parameters, return_type) | 114 return (self.node.GetName(), parameters, return_type) |
116 | 115 |
117 class Param(object): | 116 class Param(object): |
118 ''' | 117 ''' |
119 Given a Param node representing a function parameter, converts into a Python | 118 Given a Param node representing a function parameter, converts into a Python |
(...skipping 288 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
408 ''' | 407 ''' |
409 Dump a json serialization of parse result for the IDL files whose names | 408 Dump a json serialization of parse result for the IDL files whose names |
410 were passed in on the command line. | 409 were passed in on the command line. |
411 ''' | 410 ''' |
412 for filename in sys.argv[1:]: | 411 for filename in sys.argv[1:]: |
413 schema = Load(filename) | 412 schema = Load(filename) |
414 print json.dumps(schema, indent=2) | 413 print json.dumps(schema, indent=2) |
415 | 414 |
416 if __name__ == '__main__': | 415 if __name__ == '__main__': |
417 Main() | 416 Main() |
OLD | NEW |