summaryrefslogtreecommitdiffstats
path: root/tools/json_schema_compiler/compiler.py
diff options
context:
space:
mode:
authormiket@chromium.org <miket@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-03-16 14:33:28 +0000
committermiket@chromium.org <miket@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-03-16 14:33:28 +0000
commit1cf2bc43dbea4c2888c634776adc4752f2d80a00 (patch)
tree60ce58bca53f8b056b669b62fe781c7f34fb6e49 /tools/json_schema_compiler/compiler.py
parent49c3b2764815f3db3b35c59415e099b80c5afca9 (diff)
downloadchromium_src-1cf2bc43dbea4c2888c634776adc4752f2d80a00.zip
chromium_src-1cf2bc43dbea4c2888c634776adc4752f2d80a00.tar.gz
chromium_src-1cf2bc43dbea4c2888c634776adc4752f2d80a00.tar.bz2
Refactor extension_function_dispatcher to extract ExtensionFunctionRegistry. This allows us to generate an additional code block that takes an ExtensionFunctionRegistry and asks it to register generated API functions.
Then switch DnsResolve over to get registered this way. Along the way, notice that DNSResolve is capitalized using an untenable style. Fix that. BUG=none (essential plumbing work) TEST=same as before Review URL: http://codereview.chromium.org/9666059 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@127159 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'tools/json_schema_compiler/compiler.py')
-rw-r--r--tools/json_schema_compiler/compiler.py95
1 files changed, 72 insertions, 23 deletions
diff --git a/tools/json_schema_compiler/compiler.py b/tools/json_schema_compiler/compiler.py
index f782f72..bd9805d 100644
--- a/tools/json_schema_compiler/compiler.py
+++ b/tools/json_schema_compiler/compiler.py
@@ -18,6 +18,7 @@ Usage example:
import cc_generator
import cpp_type_generator
+import h_bundle_generator
import h_generator
import idl_schema
import json_schema
@@ -26,30 +27,9 @@ import optparse
import os.path
import sys
-if __name__ == '__main__':
- parser = optparse.OptionParser(
- description='Generates a C++ model of an API from JSON schema',
- usage='usage: %prog [option]... 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. e.g extensions::api.')
-
- (opts, args) = parser.parse_args()
- if not args:
- sys.exit(parser.get_usage())
- dest_dir = opts.destdir
- root_namespace = opts.namespace
-
- schema = os.path.normpath(args[0])
-
- api_model = model.Model()
-
- # Actually generate for source file.
+def load_schema(schema):
schema_filename, schema_extension = os.path.splitext(schema)
+
if schema_extension == '.json':
api_defs = json_schema.Load(schema)
elif schema_extension == '.idl':
@@ -58,6 +38,15 @@ if __name__ == '__main__':
sys.exit("Did not recognize file extension %s for schema %s" %
(schema_extension, schema))
+ return api_defs
+
+def handle_single_schema(filename, dest_dir, root, root_namespace):
+ schema = os.path.normpath(filename)
+ schema_filename, schema_extension = os.path.splitext(schema)
+ api_defs = load_schema(schema)
+
+ api_model = model.Model()
+
for target_namespace in api_defs:
referenced_schemas = target_namespace.get('dependencies', [])
# Load type dependencies into the model.
@@ -110,3 +99,63 @@ if __name__ == '__main__':
print '%s.cc' % out_file
print
print cc_code
+
+def handle_bundle_schema(filenames, dest_dir, root, root_namespace):
+ # Merge the source files into a single list of schemas.
+ api_defs = []
+ for filename in filenames:
+ schema = os.path.normpath(filename)
+ schema_filename, schema_extension = os.path.splitext(schema)
+ api_defs.extend(load_schema(schema))
+
+ api_model = model.Model()
+ relpath = os.path.relpath(os.path.normpath(filenames[0]), root)
+ for target_namespace in api_defs:
+ api_model.AddNamespace(target_namespace, relpath)
+
+ type_generator = cpp_type_generator.CppTypeGenerator(root_namespace)
+ for referenced_namespace in api_model.namespaces.values():
+ type_generator.AddNamespace(
+ referenced_namespace,
+ referenced_namespace.unix_name)
+
+ generator = h_bundle_generator.HBundleGenerator(api_model, type_generator)
+ h_bundle_code = generator.Generate().Render()
+
+ out_file = 'generated_api'
+ if dest_dir:
+ with open(
+ os.path.join(dest_dir, 'chrome/common/extensions/api/generated_api.h'),
+ 'w') as h_file:
+ h_file.write(h_bundle_code)
+ else:
+ print '%s.h' % out_file
+ print
+ print h_bundle_code
+
+if __name__ == '__main__':
+ parser = optparse.OptionParser(
+ description='Generates a C++ model of an API from JSON schema',
+ usage='usage: %prog [option]... 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. e.g extensions::api.')
+ parser.add_option('-b', '--bundle', action="store_true", help=
+'''if supplied, causes compiler to generate bundle files for the given set of
+source files.''')
+
+ (opts, args) = parser.parse_args()
+
+ if not args:
+ sys.exit(0) # This is OK as a no-op
+ dest_dir = opts.destdir
+ root_namespace = opts.namespace
+
+ if opts.bundle:
+ handle_bundle_schema(args, dest_dir, opts.root, root_namespace)
+ else:
+ handle_single_schema(args[0], dest_dir, opts.root, root_namespace)