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 |
| index f782f724f1d4e091c0341166b79ab4f443c1e22c..d3ab10a02df304bfc8ebd4c8f6c6cec954b366ad 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,64 @@ if __name__ == '__main__': |
| print '%s.cc' % out_file |
| 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) |
| + |
| + h_bundle_code = (h_bundle_generator.HBundleGenerator(api_model, |
| + type_generator) |
| + .Generate().Render()) |
|
asargent_no_longer_on_chrome
2012/03/14 22:46:07
nit: the indentation looks a little weird here. Ev
miket_OOO
2012/03/14 22:55:16
Done.
|
| + |
| + 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 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) |