Chromium Code Reviews| Index: tools/json_schema_compiler/compiler.py |
| diff --git a/tools/json_schema_compiler/compiler.py b/tools/json_schema_compiler/compiler.py |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..d53e961831d88c325e6003ce0bd9b438aca241f3 |
| --- /dev/null |
| +++ b/tools/json_schema_compiler/compiler.py |
| @@ -0,0 +1,94 @@ |
| +#!/usr/bin/env python |
| +# Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +# Use of this source code is governed by a BSD-style license that can be |
| +# found in the LICENSE file. |
| +"""Generator for C++ structs from api json files. |
| + |
| +The purpose of this tool is to remove the need for hand-written code that |
| +converts to and from base::Value types when receiving javascript api calls. |
| +Originally written for generating code for extension apis. Reference schemas |
| +are in chrome/common/extensions/api. |
| + |
| +Usage example: |
| + compiler.py --root /home/Work/src --namespace extensions windows.json |
| + tabs.json |
| + compiler.py --destdir gen --suffix _api --root /home/Work/src |
| + --namespace extensions windows.json tabs.json |
| +""" |
| +import model |
|
Yoyo Zhou
2012/01/19 02:19:40
Imports should be sorted.
calamity
2012/01/23 05:14:45
Done.
|
| +import os.path |
| +import json |
| +import cc_generator |
| +import h_generator |
| +import optparse |
| +import sys |
| + |
| +parser = optparse.OptionParser( |
| + description='Generates a C++ model of an API from JSON schema', |
| + usage='usage: %prog [option]... schema [referenced_schema]...') |
| +parser.add_option('-r', '--root', default='.', |
| + help='logical include root directory. Path to schema files from specified' |
| + 'dir will be the include path.') |
| +parser.add_option('-d', '--destdir', |
| + help='root directory to output generated files') |
| +parser.add_option('-n', '--namespace', default='generated_api_schemas', |
| + help='C++ namespace for generated files') |
| +parser.add_option('-s', '--suffix', default='_api', |
| + help='C++ namespace suffix for generated files') |
| + |
| +(opts, args) = parser.parse_args() |
|
Yoyo Zhou
2012/01/19 02:19:40
Looks like you want most of this file to be behind
calamity
2012/01/20 01:10:25
Done.
|
| +if not args: |
| + sys.exit(parser.get_usage()) |
| +dest_dir = opts.destdir |
| +root_namespace = opts.namespace |
| +filename_suffix = opts.suffix |
| + |
| +schema = os.path.normpath(args[0]) |
| +referenced_schemas = args[1:] |
| + |
| +api_model = model.Model() |
| + |
| +# Load type dependencies into the model. |
| +for referenced_schema_path in referenced_schemas: |
| + referenced_schema_path = os.path.normpath(referenced_schema_path) |
| + referenced_schema_file = open(referenced_schema_path, 'r') |
| + referenced_api_defs = json.loads(referenced_schema_file.read()) |
| + referenced_schema_file.close() |
| + |
| + for namespace in referenced_api_defs: |
| + api_model.add_namespace(namespace, |
| + os.path.relpath(referenced_schema_path, opts.root), filename_suffix) |
| + |
| +# Actually generate for source file. |
| +schema_file = open(schema, 'r') |
| +api_defs = json.loads(schema_file.read()) |
|
Nico
2012/01/18 17:30:12
with open(schema, 'r') as schema_file:
api_defs
calamity
2012/01/20 01:10:25
Done.
|
| +schema_file.close() |
| +for target_namespace in api_defs: |
| + # Gets the relative path from opts.root to the schema to correctly determine |
| + # the include path. |
| + relpath = os.path.relpath(schema, opts.root) |
| + namespace = api_model.add_namespace(target_namespace, relpath, |
| + filename_suffix) |
| + if not namespace: |
| + continue |
| + cc_generator = cc_generator.CCGenerator(namespace, api_model, root_namespace) |
| + cc_code = cc_generator.generate().render() |
| + h_generator = h_generator.HGenerator(namespace, api_model, root_namespace) |
| + h_code = h_generator.generate().render() |
| + if dest_dir: |
| + cc_file = open(os.path.join(dest_dir, namespace.source_file_dir, |
| + namespace.target_namespace + '.cc'), 'w') |
|
Yoyo Zhou
2012/01/19 02:19:40
Is this going to put experimental APIs into files
calamity
2012/01/20 01:10:25
Hmm. Good point. Refactored this whole thing a bit
|
| + cc_file.write(cc_code) |
| + cc_file.close() |
| + h_file = open(os.path.join(dest_dir, namespace.source_file_dir, |
| + namespace.target_namespace + '.h'), 'w') |
| + h_file.write(h_code) |
| + h_file.close() |
| + else: |
| + print '%s.h' % namespace.target_namespace |
| + print h_code |
| + print '%s.cc\n' % namespace.target_namespace |
| + print cc_code |