| OLD | NEW |
| 1 # Copyright 2016 The Chromium Authors. All rights reserved. | 1 # Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 import argparse | 5 import argparse |
| 6 import os.path | 6 import os.path |
| 7 import sys | 7 import sys |
| 8 import re | 8 import re |
| 9 try: | 9 try: |
| 10 import json | 10 import json |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 107 return { | 107 return { |
| 108 'return_type': 'std::unique_ptr<headless::%s::%s>' % ( | 108 'return_type': 'std::unique_ptr<headless::%s::%s>' % ( |
| 109 namespace, type['id']), | 109 namespace, type['id']), |
| 110 'pass_type': 'std::unique_ptr<headless::%s::%s>' % (namespace, type['id']), | 110 'pass_type': 'std::unique_ptr<headless::%s::%s>' % (namespace, type['id']), |
| 111 'to_raw_type': '*%s', | 111 'to_raw_type': '*%s', |
| 112 'to_raw_return_type': '%s.get()', | 112 'to_raw_return_type': '%s.get()', |
| 113 'to_pass_type': 'std::move(%s)', | 113 'to_pass_type': 'std::move(%s)', |
| 114 'type': 'std::unique_ptr<headless::%s::%s>' % (namespace, type['id']), | 114 'type': 'std::unique_ptr<headless::%s::%s>' % (namespace, type['id']), |
| 115 'raw_type': 'headless::%s::%s' % (namespace, type['id']), | 115 'raw_type': 'headless::%s::%s' % (namespace, type['id']), |
| 116 'raw_pass_type': 'headless::%s::%s*' % (namespace, type['id']), | 116 'raw_pass_type': 'headless::%s::%s*' % (namespace, type['id']), |
| 117 'raw_return_type': 'headless::%s::%s*' % (namespace, type['id']), | 117 'raw_return_type': 'const headless::%s::%s*' % (namespace, type['id']), |
| 118 } | 118 } |
| 119 | 119 |
| 120 | 120 |
| 121 def CreateEnumTypeDefinition(domain_name, type): | 121 def CreateEnumTypeDefinition(domain_name, type): |
| 122 namespace = CamelCaseToHackerStyle(domain_name) | 122 namespace = CamelCaseToHackerStyle(domain_name) |
| 123 return { | 123 return { |
| 124 'return_type': 'headless::%s::%s' % (namespace, type['id']), | 124 'return_type': 'headless::%s::%s' % (namespace, type['id']), |
| 125 'pass_type': 'headless::%s::%s' % (namespace, type['id']), | 125 'pass_type': 'headless::%s::%s' % (namespace, type['id']), |
| 126 'to_raw_type': '%s', | 126 'to_raw_type': '%s', |
| 127 'to_raw_return_type': '%s', | 127 'to_raw_return_type': '%s', |
| 128 'to_pass_type': '%s', | 128 'to_pass_type': '%s', |
| 129 'type': 'headless::%s::%s' % (namespace, type['id']), | 129 'type': 'headless::%s::%s' % (namespace, type['id']), |
| 130 'raw_type': 'headless::%s::%s' % (namespace, type['id']), | 130 'raw_type': 'headless::%s::%s' % (namespace, type['id']), |
| 131 'raw_pass_type': 'headless::%s::%s' % (namespace, type['id']), | 131 'raw_pass_type': 'headless::%s::%s' % (namespace, type['id']), |
| 132 'raw_return_type': 'headless::%s::%s' % (namespace, type['id']), | 132 'raw_return_type': 'headless::%s::%s' % (namespace, type['id']), |
| 133 } | 133 } |
| 134 | 134 |
| 135 | 135 |
| 136 def CreateObjectTypeDefinition(): | 136 def CreateObjectTypeDefinition(): |
| 137 return { | 137 return { |
| 138 'return_type': 'std::unique_ptr<base::DictionaryValue>', | 138 'return_type': 'std::unique_ptr<base::DictionaryValue>', |
| 139 'pass_type': 'std::unique_ptr<base::DictionaryValue>', | 139 'pass_type': 'std::unique_ptr<base::DictionaryValue>', |
| 140 'to_raw_type': '*%s', | 140 'to_raw_type': '*%s', |
| 141 'to_raw_return_type': '%s.get()', | 141 'to_raw_return_type': '%s.get()', |
| 142 'to_pass_type': 'std::move(%s)', | 142 'to_pass_type': 'std::move(%s)', |
| 143 'type': 'std::unique_ptr<base::DictionaryValue>', | 143 'type': 'std::unique_ptr<base::DictionaryValue>', |
| 144 'raw_type': 'base::DictionaryValue', | 144 'raw_type': 'base::DictionaryValue', |
| 145 'raw_pass_type': 'base::DictionaryValue*', | 145 'raw_pass_type': 'base::DictionaryValue*', |
| 146 'raw_return_type': 'base::DictionaryValue*', | 146 'raw_return_type': 'const base::DictionaryValue*', |
| 147 } | 147 } |
| 148 | 148 |
| 149 | 149 |
| 150 def WrapObjectTypeDefinition(type): | 150 def WrapObjectTypeDefinition(type): |
| 151 id = type.get('id', 'base::Value') | 151 id = type.get('id', 'base::Value') |
| 152 return { | 152 return { |
| 153 'return_type': 'std::unique_ptr<%s>' % id, | 153 'return_type': 'std::unique_ptr<%s>' % id, |
| 154 'pass_type': 'std::unique_ptr<%s>' % id, | 154 'pass_type': 'std::unique_ptr<%s>' % id, |
| 155 'to_raw_type': '*%s', | 155 'to_raw_type': '*%s', |
| 156 'to_raw_return_type': '%s.get()', | 156 'to_raw_return_type': '%s.get()', |
| 157 'to_pass_type': 'std::move(%s)', | 157 'to_pass_type': 'std::move(%s)', |
| 158 'type': 'std::unique_ptr<%s>' % id, | 158 'type': 'std::unique_ptr<%s>' % id, |
| 159 'raw_type': id, | 159 'raw_type': id, |
| 160 'raw_pass_type': '%s*' % id, | 160 'raw_pass_type': '%s*' % id, |
| 161 'raw_return_type': '%s*' % id, | 161 'raw_return_type': 'const %s*' % id, |
| 162 } | 162 } |
| 163 | 163 |
| 164 | 164 |
| 165 def CreateAnyTypeDefinition(): | 165 def CreateAnyTypeDefinition(): |
| 166 return { | 166 return { |
| 167 'return_type': 'std::unique_ptr<base::Value>', | 167 'return_type': 'std::unique_ptr<base::Value>', |
| 168 'pass_type': 'std::unique_ptr<base::Value>', | 168 'pass_type': 'std::unique_ptr<base::Value>', |
| 169 'to_raw_type': '*%s', | 169 'to_raw_type': '*%s', |
| 170 'to_raw_return_type': '%s.get()', | 170 'to_raw_return_type': '%s.get()', |
| 171 'to_pass_type': 'std::move(%s)', | 171 'to_pass_type': 'std::move(%s)', |
| 172 'type': 'std::unique_ptr<base::Value>', | 172 'type': 'std::unique_ptr<base::Value>', |
| 173 'raw_type': 'base::Value', | 173 'raw_type': 'base::Value', |
| 174 'raw_pass_type': 'base::Value*', | 174 'raw_pass_type': 'base::Value*', |
| 175 'raw_return_type': 'base::Value*', | 175 'raw_return_type': 'const base::Value*', |
| 176 } | 176 } |
| 177 | 177 |
| 178 | 178 |
| 179 def CreateStringTypeDefinition(domain): | 179 def CreateStringTypeDefinition(domain): |
| 180 return { | 180 return { |
| 181 'return_type': 'std::string', | 181 'return_type': 'std::string', |
| 182 'pass_type': 'const std::string&', | 182 'pass_type': 'const std::string&', |
| 183 'to_pass_type': '%s', | 183 'to_pass_type': '%s', |
| 184 'to_raw_type': '%s', | 184 'to_raw_type': '%s', |
| 185 'to_raw_return_type': '%s', | 185 'to_raw_return_type': '%s', |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 222 def WrapArrayDefinition(type): | 222 def WrapArrayDefinition(type): |
| 223 return { | 223 return { |
| 224 'return_type': 'std::vector<%s>' % type['type'], | 224 'return_type': 'std::vector<%s>' % type['type'], |
| 225 'pass_type': 'std::vector<%s>' % type['type'], | 225 'pass_type': 'std::vector<%s>' % type['type'], |
| 226 'to_raw_type': '%s', | 226 'to_raw_type': '%s', |
| 227 'to_raw_return_type': '&%s', | 227 'to_raw_return_type': '&%s', |
| 228 'to_pass_type': 'std::move(%s)', | 228 'to_pass_type': 'std::move(%s)', |
| 229 'type': 'std::vector<%s>' % type['type'], | 229 'type': 'std::vector<%s>' % type['type'], |
| 230 'raw_type': 'std::vector<%s>' % type['type'], | 230 'raw_type': 'std::vector<%s>' % type['type'], |
| 231 'raw_pass_type': 'std::vector<%s>*' % type['type'], | 231 'raw_pass_type': 'std::vector<%s>*' % type['type'], |
| 232 'raw_return_type': 'std::vector<%s>*' % type['type'], | 232 'raw_return_type': 'const std::vector<%s>*' % type['type'], |
| 233 } | 233 } |
| 234 | 234 |
| 235 | 235 |
| 236 def CreateTypeDefinitions(json_api): | 236 def CreateTypeDefinitions(json_api): |
| 237 for domain in json_api['domains']: | 237 for domain in json_api['domains']: |
| 238 if not ('types' in domain): | 238 if not ('types' in domain): |
| 239 continue | 239 continue |
| 240 for type in domain['types']: | 240 for type in domain['types']: |
| 241 if type['type'] == 'object': | 241 if type['type'] == 'object': |
| 242 if 'properties' in type: | 242 if 'properties' in type: |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 323 result_type = { | 323 result_type = { |
| 324 'id': ToTitleCase(command['name']) + 'Result', | 324 'id': ToTitleCase(command['name']) + 'Result', |
| 325 'type': 'object', | 325 'type': 'object', |
| 326 'description': 'Result for the %s command.' % ToTitleCase( | 326 'description': 'Result for the %s command.' % ToTitleCase( |
| 327 command['name']), | 327 command['name']), |
| 328 'properties': command['returns'] | 328 'properties': command['returns'] |
| 329 } | 329 } |
| 330 domain['types'].append(result_type) | 330 domain['types'].append(result_type) |
| 331 | 331 |
| 332 | 332 |
| 333 def SynthesizeEventTypes(json_api): |
| 334 """Generate types for events and their properties.""" |
| 335 for domain in json_api['domains']: |
| 336 if not 'types' in domain: |
| 337 domain['types'] = [] |
| 338 for event in domain.get('events', []): |
| 339 if not 'parameters' in event: |
| 340 continue |
| 341 for parameter in event['parameters']: |
| 342 if 'enum' in parameter and not '$ref' in parameter: |
| 343 SynthesizeEnumType(domain, event['name'], parameter) |
| 344 event_type = { |
| 345 'id': ToTitleCase(event['name']) + 'Params', |
| 346 'type': 'object', |
| 347 'description': 'Parameters for the %s event.' % ToTitleCase( |
| 348 event['name']), |
| 349 'properties': event['parameters'] |
| 350 } |
| 351 domain['types'].append(event_type) |
| 352 |
| 353 |
| 333 def Generate(jinja_env, output_dirname, json_api, class_name, file_types): | 354 def Generate(jinja_env, output_dirname, json_api, class_name, file_types): |
| 334 template_context = { | 355 template_context = { |
| 335 'api': json_api, | 356 'api': json_api, |
| 336 'join_arrays': JoinArrays, | 357 'join_arrays': JoinArrays, |
| 337 'resolve_type': ResolveType, | 358 'resolve_type': ResolveType, |
| 338 'type_definition': TypeDefinition, | 359 'type_definition': TypeDefinition, |
| 339 } | 360 } |
| 340 for file_type in file_types: | 361 for file_type in file_types: |
| 341 template = jinja_env.get_template('/%s_%s.template' % ( | 362 template = jinja_env.get_template('/%s_%s.template' % ( |
| 342 class_name, file_type)) | 363 class_name, file_type)) |
| (...skipping 15 matching lines...) Expand all Loading... |
| 358 domain_name = CamelCaseToHackerStyle(domain['domain']) | 379 domain_name = CamelCaseToHackerStyle(domain['domain']) |
| 359 output_file = '%s/%s.%s' % (output_dirname, domain_name, file_type) | 380 output_file = '%s/%s.%s' % (output_dirname, domain_name, file_type) |
| 360 with open(output_file, 'w') as f: | 381 with open(output_file, 'w') as f: |
| 361 f.write(template.render(template_context)) | 382 f.write(template.render(template_context)) |
| 362 | 383 |
| 363 | 384 |
| 364 if __name__ == '__main__': | 385 if __name__ == '__main__': |
| 365 json_api, output_dirname = ParseArguments(sys.argv[1:]) | 386 json_api, output_dirname = ParseArguments(sys.argv[1:]) |
| 366 jinja_env = InitializeJinjaEnv(output_dirname) | 387 jinja_env = InitializeJinjaEnv(output_dirname) |
| 367 SynthesizeCommandTypes(json_api) | 388 SynthesizeCommandTypes(json_api) |
| 389 SynthesizeEventTypes(json_api) |
| 368 PatchFullQualifiedRefs(json_api) | 390 PatchFullQualifiedRefs(json_api) |
| 369 CreateTypeDefinitions(json_api) | 391 CreateTypeDefinitions(json_api) |
| 370 Generate(jinja_env, output_dirname, json_api, 'types', ['cc', 'h']) | 392 Generate(jinja_env, output_dirname, json_api, 'types', ['cc', 'h']) |
| 371 Generate(jinja_env, output_dirname, json_api, 'type_conversions', ['h']) | 393 Generate(jinja_env, output_dirname, json_api, 'type_conversions', ['h']) |
| 372 GenerateDomains(jinja_env, output_dirname, json_api, 'domain', ['cc', 'h']) | 394 GenerateDomains(jinja_env, output_dirname, json_api, 'domain', ['cc', 'h']) |
| OLD | NEW |