OLD | NEW |
---|---|
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright 2012 The Chromium Authors. All rights reserved. | 2 # Copyright 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 | 5 |
6 # Format for the JSON schema file: | 6 # Format for the JSON schema file: |
7 # { | 7 # { |
8 # "type_name": "DesiredCStructName", | 8 # "type_name": "DesiredCStructName", |
9 # "headers": [ // Optional list of headers to be included by the .h. | 9 # "headers": [ // Optional list of headers to be included by the .h. |
10 # "path/to/header.h" | 10 # "path/to/header.h" |
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
166 f.write('} // namespace %s\n' % namespace) | 166 f.write('} // namespace %s\n' % namespace) |
167 | 167 |
168 def _Load(filename): | 168 def _Load(filename): |
169 """Loads a JSON file int a Python object and return this object. | 169 """Loads a JSON file int a Python object and return this object. |
170 """ | 170 """ |
171 # TODO(beaudoin): When moving to Python 2.7 use object_pairs_hook=OrderedDict. | 171 # TODO(beaudoin): When moving to Python 2.7 use object_pairs_hook=OrderedDict. |
172 with open(filename, 'r') as handle: | 172 with open(filename, 'r') as handle: |
173 result = json.loads(json_comment_eater.Nom(handle.read())) | 173 result = json.loads(json_comment_eater.Nom(handle.read())) |
174 return result | 174 return result |
175 | 175 |
176 def GenerateStruct(basepath, output_root, namespace, schema, description, | |
177 description_filename, schema_filename): | |
Alexei Svitkine (slow)
2015/07/08 16:54:54
Nit: Align
danduong
2015/07/09 00:12:54
Done.
| |
178 head = HEAD % (datetime.now().year, schema_filename, description_filename) | |
Alexei Svitkine (slow)
2015/07/08 16:54:53
Add a comment about what this function does and do
danduong
2015/07/09 00:12:54
Done.
| |
179 _GenerateH(basepath, output_root, head, namespace, schema, description) | |
180 _GenerateCC(basepath, output_root, head, namespace, schema, description) | |
181 | |
176 if __name__ == '__main__': | 182 if __name__ == '__main__': |
177 parser = optparse.OptionParser( | 183 parser = optparse.OptionParser( |
178 description='Generates an C++ array of struct from a JSON description.', | 184 description='Generates an C++ array of struct from a JSON description.', |
179 usage='usage: %prog [option] -s schema description') | 185 usage='usage: %prog [option] -s schema description') |
180 parser.add_option('-b', '--destbase', | 186 parser.add_option('-b', '--destbase', |
181 help='base directory of generated files.') | 187 help='base directory of generated files.') |
182 parser.add_option('-d', '--destdir', | 188 parser.add_option('-d', '--destdir', |
183 help='directory to output generated files, relative to destbase.') | 189 help='directory to output generated files, relative to destbase.') |
184 parser.add_option('-n', '--namespace', | 190 parser.add_option('-n', '--namespace', |
185 help='C++ namespace for generated files. e.g search_providers.') | 191 help='C++ namespace for generated files. e.g search_providers.') |
186 parser.add_option('-s', '--schema', help='path to the schema file, ' | 192 parser.add_option('-s', '--schema', help='path to the schema file, ' |
187 'mandatory.') | 193 'mandatory.') |
194 parser.add_option('-o', '--output', help='output filename, ') | |
188 (opts, args) = parser.parse_args() | 195 (opts, args) = parser.parse_args() |
189 | 196 |
190 if not opts.schema: | 197 if not opts.schema: |
191 parser.error('You must specify a --schema.') | 198 parser.error('You must specify a --schema.') |
192 | 199 |
193 description_filename = os.path.normpath(args[0]) | 200 description_filename = os.path.normpath(args[0]) |
194 root, ext = os.path.splitext(description_filename) | 201 root, ext = os.path.splitext(description_filename) |
195 shortroot = os.path.split(root)[1] | 202 shortroot = opts.output if opts.output else os.path.split(root)[1] |
196 if opts.destdir: | 203 if opts.destdir: |
197 output_root = os.path.join(os.path.normpath(opts.destdir), shortroot) | 204 output_root = os.path.join(os.path.normpath(opts.destdir), shortroot) |
198 else: | 205 else: |
199 output_root = shortroot | 206 output_root = shortroot |
200 | 207 |
201 if opts.destbase: | 208 if opts.destbase: |
202 basepath = os.path.normpath(opts.destbase) | 209 basepath = os.path.normpath(opts.destbase) |
203 else: | 210 else: |
204 basepath = '' | 211 basepath = '' |
205 | 212 |
206 schema = _Load(opts.schema) | 213 schema = _Load(opts.schema) |
207 description = _Load(description_filename) | 214 description = _Load(description_filename) |
208 | 215 GenerateStruct(basepath, output_root, opts.namespace, schema, description, |
209 head = HEAD % (datetime.now().year, opts.schema, description_filename) | 216 description_filename, opts.schema) |
210 _GenerateH(basepath, output_root, head, opts.namespace, schema, description) | |
211 _GenerateCC(basepath, output_root, head, opts.namespace, schema, description) | |
OLD | NEW |