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

Unified Diff: tools/json_schema_compiler/compiler.py

Issue 12218151: Added unit tests for the Dart Chrome.* API wrappers. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Remove print statement if outputting to file. Created 7 years, 10 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
« no previous file with comments | « no previous file | tools/json_schema_compiler/dart_generator_test.py » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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..cc3aa72ecc96070e5e599eac393a2bbf8d485937 100755
--- a/tools/json_schema_compiler/compiler.py
+++ b/tools/json_schema_compiler/compiler.py
@@ -32,7 +32,7 @@ import sys
# First is default.
GENERATORS = ['cpp', 'cpp-bundle', 'dart']
-def load_schema(schema):
+def _LoadSchema(schema):
schema_filename, schema_extension = os.path.splitext(schema)
if schema_extension == '.json':
@@ -48,45 +48,22 @@ 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 GenerateSchema(generator,
+ filenames,
+ root,
+ destdir,
+ root_namespace,
+ dart_overrides_dir):
# 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)
+ api_def = _LoadSchema(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,73 @@ 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
- print code
- print
+ 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.")
+
+ result = GenerateSchema(opts.generator, filenames, opts.root, opts.destdir,
+ opts.namespace, opts.dart_overrides_dir)
+ if not opts.destdir:
+ print result
« no previous file with comments | « no previous file | tools/json_schema_compiler/dart_generator_test.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698