Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(407)

Unified Diff: tools/json_schema_compiler/compiler.py

Issue 9114036: Code generation for extensions api (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: more rework Created 8 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..8315e9b0b92a08f18398fdf650c6523f96c55686
--- /dev/null
+++ b/tools/json_schema_compiler/compiler.py
@@ -0,0 +1,80 @@
+# Copyright (c) 2012 The Chromium Authors. All rights reserved.
not at google - send to devlin 2012/01/17 05:42:32 #!/usr/bin/env python
calamity 2012/01/18 05:43:08 Done.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
not at google - send to devlin 2012/01/17 05:42:32 We should probably add a general of comment of wha
calamity 2012/01/18 05:43:08 Done.
+import model
+import os.path
+import json
+import cc_generator
+import h_generator
+import optparse
+import sys
+
+parser = optparse.OptionParser(
+ description='Generate C++ boilerplate code for extension apis from json',
not at google - send to devlin 2012/01/17 05:42:32 "Generates a C++ model of a JSON schema" or someth
calamity 2012/01/18 05:43:08 Done.
+ usage='usage: %prog [options] target referenced_jsons')
not at google - send to devlin 2012/01/17 05:42:32 I think a more unixy way of saying something like
calamity 2012/01/18 05:43:08 Done.
+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')
not at google - send to devlin 2012/01/17 05:42:32 Print usage instead of just "Error.." ?
calamity 2012/01/18 05:43:08 Done.
+dest_dir = opts.destdir
+root_namespace = opts.namespace
+filename_suffix = opts.suffix
+
+target_json = os.path.normpath(args[0])
not at google - send to devlin 2012/01/17 05:42:32 it's not actually target, that implies output. th
+referenced_jsons = args[1:]
+
+json_model = model.Model()
+
+# Load type dependencies into the model
not at google - send to devlin 2012/01/17 05:42:32 fullstop
calamity 2012/01/18 05:43:08 Done.
+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)
not at google - send to devlin 2012/01/17 05:42:32 pull this comment and the os.path.relpath(...) thi
calamity 2012/01/18 05:43:08 Include path is the #include path? The relative pa
calamity 2012/01/18 05:43:08 Well, it's the output path here, but the #include
not at google - send to devlin 2012/01/18 06:57:28 Cool, this can be 1 line now? Also, comment above
+ if not namespace:
+ continue
+ cc_generator = cc_generator.CCGenerator(namespace, json_model)
+ cc_code = cc_generator.generate().render()
+ h_generator = h_generator.HGenerator(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

Powered by Google App Engine
This is Rietveld 408576698