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

Side by Side Diff: tools/json_schema_compiler/compiler.py

Issue 23549025: Clean up JSON Schema Compiler. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Removed enum test and added blank lines. Created 7 years, 3 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 unified diff | Download patch
OLDNEW
1 #!/usr/bin/env python 1 #!/usr/bin/env python
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be 3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file. 4 # found in the LICENSE file.
5 """Generator for C++ structs from api json files. 5 """Generator for C++ structs from api json files.
6 6
7 The purpose of this tool is to remove the need for hand-written code that 7 The purpose of this tool is to remove the need for hand-written code that
8 converts to and from base::Value types when receiving javascript api calls. 8 converts to and from base::Value types when receiving javascript api calls.
9 Originally written for generating code for extension apis. Reference schemas 9 Originally written for generating code for extension apis. Reference schemas
10 are in chrome/common/extensions/api. 10 are in chrome/common/extensions/api.
11 11
12 Usage example: 12 Usage example:
13 compiler.py --root /home/Work/src --namespace extensions windows.json 13 compiler.py --root /home/Work/src --namespace extensions windows.json
14 tabs.json 14 tabs.json
15 compiler.py --destdir gen --root /home/Work/src 15 compiler.py --destdir gen --root /home/Work/src
16 --namespace extensions windows.json tabs.json 16 --namespace extensions windows.json tabs.json
17 """ 17 """
18 18
19 import optparse 19 import optparse
20 import os 20 import os
21 import sys 21 import sys
22 22
23 from cpp_bundle_generator import CppBundleGenerator 23 from cpp_bundle_generator import CppBundleGenerator
24 from cpp_generator import CppGenerator 24 from cpp_generator import CppGenerator
25 from cpp_type_generator import CppTypeGenerator 25 from cpp_type_generator import CppTypeGenerator
26 from dart_generator import DartGenerator 26 from dart_generator import DartGenerator
27 import json_schema 27 import json_schema
28 from model import Model, UnixName 28 from model import Model
29 from schema_loader import SchemaLoader 29 from schema_loader import SchemaLoader
30 30
31 # Names of supported code generators, as specified on the command-line. 31 # Names of supported code generators, as specified on the command-line.
32 # First is default. 32 # First is default.
33 GENERATORS = ['cpp', 'cpp-bundle', 'dart'] 33 GENERATORS = ['cpp', 'cpp-bundle', 'dart']
34 34
35 def GenerateSchema(generator, 35 def GenerateSchema(generator,
36 filenames, 36 filenames,
37 root, 37 root,
38 destdir, 38 destdir,
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
71 path, filename = os.path.split(schema_filename) 71 path, filename = os.path.split(schema_filename)
72 short_filename, extension = os.path.splitext(filename) 72 short_filename, extension = os.path.splitext(filename)
73 73
74 # Filenames are checked against the unix_names of the namespaces they 74 # Filenames are checked against the unix_names of the namespaces they
75 # generate because the gyp uses the names of the JSON files to generate 75 # generate because the gyp uses the names of the JSON files to generate
76 # the names of the .cc and .h files. We want these to be using unix_names. 76 # the names of the .cc and .h files. We want these to be using unix_names.
77 if namespace.unix_name != short_filename: 77 if namespace.unix_name != short_filename:
78 sys.exit("Filename %s is illegal. Name files using unix_hacker style." % 78 sys.exit("Filename %s is illegal. Name files using unix_hacker style." %
79 schema_filename) 79 schema_filename)
80 80
81 # The output filename must match the input filename for gyp to deal with it
82 # properly.
83 out_file = namespace.unix_name
84
85 # Construct the type generator with all the namespaces in this model. 81 # Construct the type generator with all the namespaces in this model.
86 type_generator = CppTypeGenerator(api_model, 82 type_generator = CppTypeGenerator(api_model,
87 schema_loader, 83 schema_loader,
88 default_namespace=default_namespace) 84 default_namespace=default_namespace)
89 85
90 if generator == 'cpp-bundle': 86 if generator == 'cpp-bundle':
91 cpp_bundle_generator = CppBundleGenerator(root, 87 cpp_bundle_generator = CppBundleGenerator(root,
92 api_model, 88 api_model,
93 api_defs, 89 api_defs,
94 type_generator, 90 type_generator,
(...skipping 22 matching lines...) Expand all
117 for filename, generator in generators: 113 for filename, generator in generators:
118 code = generator.Generate(namespace).Render() 114 code = generator.Generate(namespace).Render()
119 if destdir: 115 if destdir:
120 with open(os.path.join(destdir, namespace.source_file_dir, 116 with open(os.path.join(destdir, namespace.source_file_dir,
121 filename), 'w') as f: 117 filename), 'w') as f:
122 f.write(code) 118 f.write(code)
123 output_code += [filename, '', code, ''] 119 output_code += [filename, '', code, '']
124 120
125 return '\n'.join(output_code) 121 return '\n'.join(output_code)
126 122
123
127 if __name__ == '__main__': 124 if __name__ == '__main__':
128 parser = optparse.OptionParser( 125 parser = optparse.OptionParser(
129 description='Generates a C++ model of an API from JSON schema', 126 description='Generates a C++ model of an API from JSON schema',
130 usage='usage: %prog [option]... schema') 127 usage='usage: %prog [option]... schema')
131 parser.add_option('-r', '--root', default='.', 128 parser.add_option('-r', '--root', default='.',
132 help='logical include root directory. Path to schema files from specified' 129 help='logical include root directory. Path to schema files from specified'
133 ' dir will be the include path.') 130 ' dir will be the include path.')
134 parser.add_option('-d', '--destdir', 131 parser.add_option('-d', '--destdir',
135 help='root directory to output generated files.') 132 help='root directory to output generated files.')
136 parser.add_option('-n', '--namespace', default='generated_api_schemas', 133 parser.add_option('-n', '--namespace', default='generated_api_schemas',
(...skipping 13 matching lines...) Expand all
150 # Unless in bundle mode, only one file should be specified. 147 # Unless in bundle mode, only one file should be specified.
151 if opts.generator != 'cpp-bundle' and len(filenames) > 1: 148 if opts.generator != 'cpp-bundle' and len(filenames) > 1:
152 # TODO(sashab): Could also just use filenames[0] here and not complain. 149 # TODO(sashab): Could also just use filenames[0] here and not complain.
153 raise Exception( 150 raise Exception(
154 "Unless in bundle mode, only one file can be specified at a time.") 151 "Unless in bundle mode, only one file can be specified at a time.")
155 152
156 result = GenerateSchema(opts.generator, filenames, opts.root, opts.destdir, 153 result = GenerateSchema(opts.generator, filenames, opts.root, opts.destdir,
157 opts.namespace, opts.dart_overrides_dir) 154 opts.namespace, opts.dart_overrides_dir)
158 if not opts.destdir: 155 if not opts.destdir:
159 print result 156 print result
OLDNEW
« no previous file with comments | « tools/json_schema_compiler/cc_generator.py ('k') | tools/json_schema_compiler/cpp_bundle_generator.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698