| 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 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 81 param_comment_start = cur_param.end() | 81 param_comment_start = cur_param.end() |
| 82 param_comment_end = next_param.start() if next_param else len(comment) | 82 param_comment_end = next_param.start() if next_param else len(comment) |
| 83 params[param_name] = (comment[param_comment_start:param_comment_end | 83 params[param_name] = (comment[param_comment_start:param_comment_end |
| 84 ].strip().replace('\n\n', '<br/><br/>') | 84 ].strip().replace('\n\n', '<br/><br/>') |
| 85 .replace('\n', '')) | 85 .replace('\n', '')) |
| 86 return (parent_comment, params) | 86 return (parent_comment, params) |
| 87 | 87 |
| 88 class Callspec(object): | 88 class Callspec(object): |
| 89 ''' | 89 ''' |
| 90 Given a Callspec node representing an IDL function declaration, converts into | 90 Given a Callspec node representing an IDL function declaration, converts into |
| 91 a name/value pair where the value is a list of function parameters. | 91 a tuple: |
| 92 (name, list of function parameters, return type) |
| 92 ''' | 93 ''' |
| 93 def __init__(self, callspec_node, comment): | 94 def __init__(self, callspec_node, comment): |
| 94 self.node = callspec_node | 95 self.node = callspec_node |
| 95 self.comment = comment | 96 self.comment = comment |
| 96 | 97 |
| 97 def process(self, callbacks): | 98 def process(self, callbacks): |
| 98 parameters = [] | 99 parameters = [] |
| 100 return_type = None |
| 101 if self.node.GetProperty('TYPEREF') not in ('void', None): |
| 102 return_type = Typeref(self.node.GetProperty('TYPEREF'), |
| 103 self.node, |
| 104 {'name': self.node.GetName()}).process(callbacks) |
| 105 # The IDL parser doesn't allow specifying return types as optional. |
| 106 # Instead we infer any object return values to be optional. |
| 107 # TODO(asargent): fix the IDL parser to support optional return types. |
| 108 if return_type.get('type') == 'object' or '$ref' in return_type: |
| 109 return_type['optional'] = True; |
| 99 for node in self.node.children: | 110 for node in self.node.children: |
| 100 parameter = Param(node).process(callbacks) | 111 parameter = Param(node).process(callbacks) |
| 101 if parameter['name'] in self.comment: | 112 if parameter['name'] in self.comment: |
| 102 parameter['description'] = self.comment[parameter['name']] | 113 parameter['description'] = self.comment[parameter['name']] |
| 103 parameters.append(parameter) | 114 parameters.append(parameter) |
| 104 return self.node.GetName(), parameters | 115 return (self.node.GetName(), parameters, return_type) |
| 105 | 116 |
| 106 class Param(object): | 117 class Param(object): |
| 107 ''' | 118 ''' |
| 108 Given a Param node representing a function parameter, converts into a Python | 119 Given a Param node representing a function parameter, converts into a Python |
| 109 dictionary that the JSON schema compiler expects to see. | 120 dictionary that the JSON schema compiler expects to see. |
| 110 ''' | 121 ''' |
| 111 def __init__(self, param_node): | 122 def __init__(self, param_node): |
| 112 self.node = param_node | 123 self.node = param_node |
| 113 | 124 |
| 114 def process(self, callbacks): | 125 def process(self, callbacks): |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 154 if self.node.GetProperty(property_name): | 165 if self.node.GetProperty(property_name): |
| 155 properties[property_name.lower()] = True | 166 properties[property_name.lower()] = True |
| 156 is_function = False | 167 is_function = False |
| 157 parameter_comments = OrderedDict() | 168 parameter_comments = OrderedDict() |
| 158 for node in self.node.children: | 169 for node in self.node.children: |
| 159 if node.cls == 'Comment': | 170 if node.cls == 'Comment': |
| 160 (parent_comment, parameter_comments) = ProcessComment(node.GetName()) | 171 (parent_comment, parameter_comments) = ProcessComment(node.GetName()) |
| 161 properties['description'] = parent_comment | 172 properties['description'] = parent_comment |
| 162 elif node.cls == 'Callspec': | 173 elif node.cls == 'Callspec': |
| 163 is_function = True | 174 is_function = True |
| 164 name, parameters = Callspec(node, parameter_comments).process(callbacks) | 175 name, parameters, return_type = (Callspec(node, parameter_comments) |
| 176 .process(callbacks)) |
| 165 properties['parameters'] = parameters | 177 properties['parameters'] = parameters |
| 178 if return_type is not None: |
| 179 properties['returns'] = return_type |
| 166 properties['name'] = name | 180 properties['name'] = name |
| 167 if is_function: | 181 if is_function: |
| 168 properties['type'] = 'function' | 182 properties['type'] = 'function' |
| 169 else: | 183 else: |
| 170 properties = Typeref(self.node.GetProperty('TYPEREF'), | 184 properties = Typeref(self.node.GetProperty('TYPEREF'), |
| 171 self.node, properties).process(callbacks) | 185 self.node, properties).process(callbacks) |
| 172 enum_values = self.node.GetProperty('legalValues') | 186 enum_values = self.node.GetProperty('legalValues') |
| 173 if enum_values: | 187 if enum_values: |
| 174 if properties['type'] == 'integer': | 188 if properties['type'] == 'integer': |
| 175 enum_values = map(int, enum_values) | 189 enum_values = map(int, enum_values) |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 219 properties['type'] = 'object' | 233 properties['type'] = 'object' |
| 220 if 'additionalProperties' not in properties: | 234 if 'additionalProperties' not in properties: |
| 221 properties['additionalProperties'] = OrderedDict() | 235 properties['additionalProperties'] = OrderedDict() |
| 222 properties['additionalProperties']['type'] = 'any' | 236 properties['additionalProperties']['type'] = 'any' |
| 223 instance_of = self.parent.GetProperty('instanceOf') | 237 instance_of = self.parent.GetProperty('instanceOf') |
| 224 if instance_of: | 238 if instance_of: |
| 225 properties['isInstanceOf'] = instance_of | 239 properties['isInstanceOf'] = instance_of |
| 226 elif self.typeref == 'ArrayBuffer': | 240 elif self.typeref == 'ArrayBuffer': |
| 227 properties['type'] = 'binary' | 241 properties['type'] = 'binary' |
| 228 properties['isInstanceOf'] = 'ArrayBuffer' | 242 properties['isInstanceOf'] = 'ArrayBuffer' |
| 243 elif self.typeref == 'FileEntry': |
| 244 properties['type'] = 'object' |
| 245 properties['isInstanceOf'] = 'FileEntry' |
| 246 if 'additionalProperties' not in properties: |
| 247 properties['additionalProperties'] = OrderedDict() |
| 248 properties['additionalProperties']['type'] = 'any' |
| 229 elif self.typeref is None: | 249 elif self.typeref is None: |
| 230 properties['type'] = 'function' | 250 properties['type'] = 'function' |
| 231 else: | 251 else: |
| 232 if self.typeref in callbacks: | 252 if self.typeref in callbacks: |
| 233 # Do not override name and description if they are already specified. | 253 # Do not override name and description if they are already specified. |
| 234 name = properties.get('name', None) | 254 name = properties.get('name', None) |
| 235 description = properties.get('description', None) | 255 description = properties.get('description', None) |
| 236 properties.update(callbacks[self.typeref]) | 256 properties.update(callbacks[self.typeref]) |
| 237 if description is not None: | 257 if description is not None: |
| 238 properties['description'] = description | 258 properties['description'] = description |
| (...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 373 ''' | 393 ''' |
| 374 Dump a json serialization of parse result for the IDL files whose names | 394 Dump a json serialization of parse result for the IDL files whose names |
| 375 were passed in on the command line. | 395 were passed in on the command line. |
| 376 ''' | 396 ''' |
| 377 for filename in sys.argv[1:]: | 397 for filename in sys.argv[1:]: |
| 378 schema = Load(filename) | 398 schema = Load(filename) |
| 379 print json.dumps(schema, indent=2) | 399 print json.dumps(schema, indent=2) |
| 380 | 400 |
| 381 if __name__ == '__main__': | 401 if __name__ == '__main__': |
| 382 Main() | 402 Main() |
| OLD | NEW |