Index: tools/json_schema_compiler/compiler.py |
diff --git a/tools/json_schema_compiler/compiler.py b/tools/json_schema_compiler/compiler.py |
index 272719a81f7f4ea33dde94a45b9052d064ace001..a36186b5856a9923d92571fed9865071bafabd68 100755 |
--- a/tools/json_schema_compiler/compiler.py |
+++ b/tools/json_schema_compiler/compiler.py |
@@ -48,35 +48,12 @@ def load_schema(schema): |
return api_defs |
-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('-g', '--generator', default=GENERATORS[0], |
- choices=GENERATORS, |
- help='The generator to use to build the output code. Supported values are' |
- ' %s' % GENERATORS) |
- 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 opts.generator != 'cpp-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.") |
- |
+def generate_schema(generator, |
not at google - send to devlin
2013/02/20 03:02:33
GenerateSchema
sashab
2013/02/20 03:26:19
Done.
|
+ filenames, |
+ root, |
+ destdir, |
+ root_namespace, |
+ dart_overrides_dir): |
# Merge the source files into a single list of schemas. |
api_defs = [] |
for filename in filenames: |
@@ -86,7 +63,7 @@ if __name__ == '__main__': |
api_def = load_schema(schema) |
# If compiling the C++ model code, delete 'nocompile' nodes. |
- if opts.generator == 'cpp': |
+ if generator == 'cpp': |
api_def = json_schema.DeleteNodes(api_def, 'nocompile') |
api_defs.extend(api_def) |
@@ -99,7 +76,7 @@ if __name__ == '__main__': |
# |
# TODO(kalman): load dependencies lazily (get rid of the 'dependencies' list) |
# and this problem will go away. |
- if opts.generator != 'cpp-bundle': |
+ if generator != 'cpp-bundle': |
for target_namespace in api_defs: |
for referenced_schema in target_namespace.get('dependencies', []): |
split_schema = referenced_schema.split(':', 1) |
@@ -116,7 +93,7 @@ if __name__ == '__main__': |
for namespace in referenced_api_defs: |
api_model.AddNamespace( |
namespace, |
- os.path.relpath(referenced_schema_path, opts.root), |
+ os.path.relpath(referenced_schema_path, root), |
include_compiler_options=True) |
# For single-schema compilation make sure that the first (i.e. only) schema |
@@ -125,7 +102,7 @@ if __name__ == '__main__': |
# 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) |
+ relpath = os.path.relpath(os.path.normpath(schema_filename), root) |
namespace = api_model.AddNamespace(target_namespace, |
relpath, |
include_compiler_options=True) |
@@ -150,40 +127,71 @@ if __name__ == '__main__': |
type_generator = CppTypeGenerator(api_model, |
default_namespace=default_namespace) |
- if opts.generator == 'cpp-bundle': |
- cpp_bundle_generator = CppBundleGenerator(opts.root, |
+ if generator == 'cpp-bundle': |
+ cpp_bundle_generator = CppBundleGenerator(root, |
api_model, |
api_defs, |
type_generator, |
- opts.namespace) |
+ root_namespace) |
generators = [ |
('generated_api.cc', cpp_bundle_generator.api_cc_generator), |
('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.generator == 'cpp': |
- cpp_generator = CppGenerator(type_generator, opts.namespace) |
+ elif generator == 'cpp': |
+ cpp_generator = CppGenerator(type_generator, root_namespace) |
generators = [ |
('%s.h' % namespace.unix_name, cpp_generator.h_generator), |
('%s.cc' % namespace.unix_name, cpp_generator.cc_generator) |
] |
- elif opts.generator == 'dart': |
+ elif generator == 'dart': |
generators = [ |
('%s.dart' % namespace.unix_name, DartGenerator( |
- opts.dart_overrides_dir)) |
+ dart_overrides_dir)) |
] |
else: |
- raise Exception('Unrecognised generator %s' % opts.generator) |
+ raise Exception('Unrecognised generator %s' % generator) |
+ output_code = [] |
for filename, generator in generators: |
code = generator.Generate(namespace).Render() |
- if opts.destdir: |
- with open(os.path.join(opts.destdir, namespace.source_file_dir, |
+ if destdir: |
+ with open(os.path.join(destdir, namespace.source_file_dir, |
filename), 'w') as f: |
f.write(code) |
- else: |
- print filename |
- print code |
+ output_code += [filename, '', code, ''] |
+ |
+ return '\n'.join(output_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('-g', '--generator', default=GENERATORS[0], |
+ choices=GENERATORS, |
+ help='The generator to use to build the output code. Supported values are' |
+ ' %s' % GENERATORS) |
+ 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 opts.generator != 'cpp-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.") |
+ |
+ print generate_schema(opts.generator, filenames, opts.root, opts.destdir, |
not at google - send to devlin
2013/02/20 03:02:33
nit: print(foo) not print foo.
sashab
2013/02/20 03:26:19
Done. Also fixed logic a bit.
|
+ opts.namespace, opts.dart_overrides_dir) |