Index: tools/json_schema_compiler/compiler.py |
diff --git a/tools/json_schema_compiler/compiler.py b/tools/json_schema_compiler/compiler.py |
index a270756bf31bd127fa8d4a7203d44fad1b1dc94a..561d837655a1a870f59fc4c8634e8a6bb5873246 100755 |
--- a/tools/json_schema_compiler/compiler.py |
+++ b/tools/json_schema_compiler/compiler.py |
@@ -16,13 +16,14 @@ Usage example: |
--namespace extensions windows.json tabs.json |
""" |
-import cc_generator |
+from cpp_generator import CppGenerator |
not at google - send to devlin
2013/02/02 00:45:48
I like this idiom. Let's make all the imports whic
sashab
2013/02/04 05:09:27
Done.
|
import cpp_type_generator |
import h_generator |
+from dart_generator import DartGenerator |
import idl_schema |
import json_schema |
import model |
-import schema_bundle_generator |
+from cpp_bundle_generator import CppBundleGenerator |
import optparse |
import os.path |
@@ -36,27 +37,63 @@ def load_schema(schema): |
elif schema_extension == '.idl': |
api_defs = idl_schema.Load(schema) |
else: |
- sys.exit("Did not recognize file extension %s for schema %s" % |
+ sys.exit('Did not recognize file extension %s for schema %s' % |
(schema_extension, schema)) |
if len(api_defs) != 1: |
- sys.exit("File %s has multiple schemas. Files are only allowed to contain a" |
- " single schema." % schema) |
+ sys.exit('File %s has multiple schemas. Files are only allowed to contain a' |
+ ' single schema.' % 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) |
- path, short_filename = os.path.split(schema_filename) |
- api_defs = json_schema.DeleteNocompileNodes(load_schema(schema)) |
+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.''') |
+ parser.add_option('-l', '--lang', default='c++', |
+ help='''The language to generate the output in.''') |
not at google - send to devlin
2013/02/02 00:45:48
Ok, one last complaint: --bundle and --lang are se
sashab
2013/02/04 05:09:27
Done. Not sure what nbd means, but I updated the .
not at google - send to devlin
2013/02/04 17:21:06
nbd = no big deal
|
+ parser.add_option('-D', '--dart-overrides-dir', dest='dart_overrides_dir', |
+ help='Adds custom dart from files in the given directory (Dart only).') |
+ |
+ (opts, filenames) = parser.parse_args() |
+ |
+ if not filenames: |
+ sys.exit(0) # This is OK as a no-op |
+ |
+ # Unless in bundle mode, only one file should be specified. |
+ if not opts.bundle and len(filenames) > 1: |
+ # TODO(sashab): Could also just use filenames[0] here and not complain. |
+ raise Exception( |
+ "Unless in bundle mode, only one file can be specified at a time.") |
+ |
+ # 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) |
+ path, short_filename = os.path.split(schema_filename) |
+ api_def = load_schema(schema) |
+ |
+ # If in a c++-related mode, delete 'nocompile' nodes. |
+ if opts.bundle or opts.lang == 'c++': |
+ api_def = json_schema.DeleteNodes(api_def, 'nocompile') |
+ api_defs.extend(api_def) |
api_model = model.Model() |
+ # Load type dependencies into the model. |
for target_namespace in api_defs: |
- referenced_schemas = target_namespace.get('dependencies', []) |
- # Load type dependencies into the model. |
# TODO(miket): do we need this in IDL? |
not at google - send to devlin
2013/02/02 00:45:48
I think this comment might be out of date now.
sashab
2013/02/04 05:09:27
What does it mean? *Is* it needed in IDL?
I'm pre
not at google - send to devlin
2013/02/04 17:21:06
I more mean that this code executes independently
sashab
2013/02/04 23:12:45
Done.
sashab
2013/02/04 23:12:45
Done.
|
- for referenced_schema in referenced_schemas: |
+ for referenced_schema in target_namespace.get('dependencies', []): |
split_schema = referenced_schema.split(':', 1) |
if len(split_schema) > 1: |
if split_schema[0] != 'api': |
@@ -73,66 +110,9 @@ def handle_single_schema(filename, dest_dir, root, root_namespace): |
namespace, |
os.path.relpath(referenced_schema_path, opts.root)) |
- # 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.AddNamespace(target_namespace, |
- relpath, |
- include_compiler_options=True) |
- if not namespace: |
- continue |
- |
- if short_filename != namespace.unix_name: |
- sys.exit("Filename %s is illegal. Name files using unix_hacker style." % |
- filename) |
- |
- # The output filename must match the input filename for gyp to deal with it |
- # properly. |
- out_file = namespace.unix_name |
- type_generator = cpp_type_generator.CppTypeGenerator( |
- root_namespace, namespace, namespace.unix_name) |
- for referenced_namespace in api_model.namespaces.values(): |
- if referenced_namespace == namespace: |
- continue |
- type_generator.AddNamespace( |
- referenced_namespace, |
- referenced_namespace.unix_name) |
- |
- h_code = (h_generator.HGenerator(namespace, type_generator) |
- .Generate().Render()) |
- cc_code = (cc_generator.CCGenerator(namespace, type_generator) |
- .Generate().Render()) |
- |
- if dest_dir: |
- with open( |
- os.path.join(dest_dir, namespace.source_file_dir, out_file + '.cc'), |
- 'w') as cc_file: |
- cc_file.write(cc_code) |
- with open( |
- os.path.join(dest_dir, namespace.source_file_dir, out_file + '.h'), |
- 'w') as h_file: |
- h_file.write(h_code) |
- else: |
- print '%s.h' % out_file |
- print h_code |
- 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) |
- |
+ # Load the actual namespaces into the model. |
for target_namespace, schema_filename in zip(api_defs, filenames): |
+ relpath = os.path.relpath(os.path.normpath(schema_filename), opts.root) |
namespace = api_model.AddNamespace(target_namespace, |
relpath, |
include_compiler_options=True) |
@@ -146,62 +126,49 @@ def handle_bundle_schema(filenames, dest_dir, root, root_namespace): |
sys.exit("Filename %s is illegal. Name files using unix_hacker style." % |
schema_filename) |
- type_generator = cpp_type_generator.CppTypeGenerator(root_namespace) |
+ # The output filename must match the input filename for gyp to deal with it |
+ # properly. |
+ out_file = namespace.unix_name |
+ |
+ # Construct the type generator with all the namespaces in this model. |
+ type_generator = cpp_type_generator.CppTypeGenerator(opts.namespace) |
for referenced_namespace in api_model.namespaces.values(): |
type_generator.AddNamespace( |
referenced_namespace, |
referenced_namespace.unix_name) |
- generator = schema_bundle_generator.SchemaBundleGenerator( |
- root, api_model, api_defs, type_generator) |
- api_h_code = generator.GenerateAPIHeader().Render() |
- schemas_h_code = generator.GenerateSchemasHeader().Render() |
- schemas_cc_code = generator.GenerateSchemasCC().Render() |
- |
- if dest_dir: |
- basedir = os.path.join(dest_dir, 'chrome/common/extensions/api') |
- with open(os.path.join(basedir, 'generated_api.h'), 'w') as h_file: |
- h_file.write(api_h_code) |
- with open(os.path.join(basedir, 'generated_schemas.h'), 'w') as h_file: |
- h_file.write(schemas_h_code) |
- with open(os.path.join(basedir, 'generated_schemas.cc'), 'w') as cc_file: |
- cc_file.write(schemas_cc_code) |
- else: |
- print 'generated_api.h' |
- print api_h_code |
- print 'generated_schemas.h' |
- print schemas_h_code |
- print 'generated_schemas.cc' |
- print schemas_cc_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) |
+ # Generate the code for each namespace. |
+ for namespace in api_model.namespaces.values(): |
+ if opts.bundle: |
+ cpp_bundle_generator = CppBundleGenerator(opts.root, api_model, api_defs, |
+ type_generator) |
+ generators = [ |
+ ('generated_api.h', cpp_bundle_generator.api_h_generator), |
+ ('generated_schemas.cc', cpp_bundle_generator.schemas_cc_generator), |
+ ('generated_schemas.h', cpp_bundle_generator.schemas_h_generator) |
+ ] |
+ elif opts.lang == 'c++': |
+ cpp_generator = CppGenerator(type_generator) |
+ generators = [ |
+ ('%s.h' % namespace.unix_name, cpp_generator.h_generator), |
+ ('%s.cc' % namespace.unix_name, cpp_generator.cc_generator) |
+ ] |
+ elif opts.lang == 'dart': |
+ generators = [ |
+ ('%s.dart' % namespace.unix_name, DartGenerator( |
+ opts.dart_overrides_dir)) |
+ ] |
+ else: |
not at google - send to devlin
2013/02/02 00:45:48
I am so happy right now! Factored perfection!
sashab
2013/02/04 05:09:27
=D
|
+ raise Exception('Unrecognised language %s' % opts.lang) |
+ |
+ for filename, generator in generators: |
+ code = generator.Generate(namespace).Render() |
+ if opts.destdir: |
+ with open(os.path.join(opts.destdir, namespace.source_file_dir, |
+ filename)) as f: |
+ f.write(code) |
+ else: |
+ print filename |
+ print code |