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..e8dd91bae56db0444c53390107fe76c691450dd6
|
--- /dev/null
|
+++ b/tools/json_schema_compiler/compiler.py
|
@@ -0,0 +1,80 @@
|
+# Copyright (c) 2011 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.
|
+
|
+import model
|
+import os.path
|
+import json
|
+import generate_cc
|
+import generate_h
|
+import optparse
|
+import sys
|
+
|
+parser = optparse.OptionParser(
|
+ description='Generate C++ boilerplate code for extension apis from json',
|
+ usage='usage: %prog [options] target referenced_jsons')
|
+parser.add_option('-r', '--root', default='.',
|
+ help='logical include root directory. Path to json 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',
|
+ 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()
|
+if not args:
|
+ sys.exit('Error: No input json file')
|
+dest_dir = opts.destdir
|
+root_namespace = opts.namespace
|
+filename_suffix = opts.suffix
|
+
|
+target_json = os.path.normpath(args[0])
|
+referenced_jsons = args[1:]
|
+
|
+json_model = model.Model()
|
+
|
+# Load type dependencies into the model
|
+for json_path in referenced_jsons:
|
+ json_path = os.path.normpath(json_path)
|
+ api_file = open(json_path, 'r')
|
+ api_defs = json.loads(api_file.read())
|
+ api_file.close()
|
+
|
+ for namespace in api_defs:
|
+ json_model.add_namespace(namespace, root_namespace,
|
+ os.path.relpath(json_path, opts.root), filename_suffix)
|
+
|
+# Actually generate for target file.
|
+target_api_file = open(target_json, 'r')
|
+target_api_defs = json.loads(target_api_file.read())
|
+target_api_file.close()
|
+for target_namespace in target_api_defs:
|
+ namespace = json_model.add_namespace(target_namespace, root_namespace,
|
+ # Gets the relative path from opts.root to the target_json to correctly
|
+ # determine the include path
|
+ os.path.relpath(target_json, opts.root), filename_suffix)
|
+ if not namespace:
|
+ continue
|
+ cc_generator = generate_cc.CC_Generator(namespace, json_model)
|
+ cc_code = cc_generator.generate().render()
|
+ h_generator = generate_h.H_Generator(namespace, json_model)
|
+ h_code = h_generator.generate().render()
|
+ if dest_dir:
|
+ cc_file = open(os.path.join(dest_dir, namespace.parent_dir,
|
+ namespace.filename + '.cc'), 'w')
|
+ cc_file.write(cc_code)
|
+ cc_file.close()
|
+ h_file = open(os.path.join(dest_dir, namespace.parent_dir,
|
+ namespace.filename + '.h'), 'w')
|
+ h_file.write(h_code)
|
+ h_file.close()
|
+ else:
|
+ print '%s.h' % namespace.filename
|
+ print
|
+ print h_code
|
+ print
|
+ print '%s.cc\n' % namespace.filename
|
+ print
|
+ print cc_code
|
|