OLD | NEW |
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. |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
123 output_code += [filename, '', code, ''] | 123 output_code += [filename, '', code, ''] |
124 | 124 |
125 return '\n'.join(output_code) | 125 return '\n'.join(output_code) |
126 | 126 |
127 if __name__ == '__main__': | 127 if __name__ == '__main__': |
128 parser = optparse.OptionParser( | 128 parser = optparse.OptionParser( |
129 description='Generates a C++ model of an API from JSON schema', | 129 description='Generates a C++ model of an API from JSON schema', |
130 usage='usage: %prog [option]... schema') | 130 usage='usage: %prog [option]... schema') |
131 parser.add_option('-r', '--root', default='.', | 131 parser.add_option('-r', '--root', default='.', |
132 help='logical include root directory. Path to schema files from specified' | 132 help='logical include root directory. Path to schema files from specified' |
133 'dir will be the include path.') | 133 ' dir will be the include path.') |
134 parser.add_option('-d', '--destdir', | 134 parser.add_option('-d', '--destdir', |
135 help='root directory to output generated files.') | 135 help='root directory to output generated files.') |
136 parser.add_option('-n', '--namespace', default='generated_api_schemas', | 136 parser.add_option('-n', '--namespace', default='generated_api_schemas', |
137 help='C++ namespace for generated files. e.g extensions::api.') | 137 help='C++ namespace for generated files. e.g extensions::api.') |
138 parser.add_option('-g', '--generator', default=GENERATORS[0], | 138 parser.add_option('-g', '--generator', default=GENERATORS[0], |
139 choices=GENERATORS, | 139 choices=GENERATORS, |
140 help='The generator to use to build the output code. Supported values are' | 140 help='The generator to use to build the output code. Supported values are' |
141 ' %s' % GENERATORS) | 141 ' %s' % GENERATORS) |
142 parser.add_option('-D', '--dart-overrides-dir', dest='dart_overrides_dir', | 142 parser.add_option('-D', '--dart-overrides-dir', dest='dart_overrides_dir', |
143 help='Adds custom dart from files in the given directory (Dart only).') | 143 help='Adds custom dart from files in the given directory (Dart only).') |
144 | 144 |
145 (opts, filenames) = parser.parse_args() | 145 (opts, filenames) = parser.parse_args() |
146 | 146 |
147 if not filenames: | 147 if not filenames: |
148 sys.exit(0) # This is OK as a no-op | 148 sys.exit(0) # This is OK as a no-op |
149 | 149 |
150 # Unless in bundle mode, only one file should be specified. | 150 # Unless in bundle mode, only one file should be specified. |
151 if opts.generator != 'cpp-bundle' and len(filenames) > 1: | 151 if opts.generator != 'cpp-bundle' and len(filenames) > 1: |
152 # TODO(sashab): Could also just use filenames[0] here and not complain. | 152 # TODO(sashab): Could also just use filenames[0] here and not complain. |
153 raise Exception( | 153 raise Exception( |
154 "Unless in bundle mode, only one file can be specified at a time.") | 154 "Unless in bundle mode, only one file can be specified at a time.") |
155 | 155 |
156 result = GenerateSchema(opts.generator, filenames, opts.root, opts.destdir, | 156 result = GenerateSchema(opts.generator, filenames, opts.root, opts.destdir, |
157 opts.namespace, opts.dart_overrides_dir) | 157 opts.namespace, opts.dart_overrides_dir) |
158 if not opts.destdir: | 158 if not opts.destdir: |
159 print result | 159 print result |
OLD | NEW |