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

Side by Side Diff: tools/json_to_struct/json_to_struct.py

Issue 1209743002: Generate a static struct from fieldtrial_testing_config (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@codegen_nested_structs
Patch Set: add another unittest Created 5 years, 5 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 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 165 matching lines...) Expand 10 before | Expand all | Expand 10 after
176 f.write('} // namespace %s\n' % namespace) 176 f.write('} // namespace %s\n' % namespace)
177 177
178 def _Load(filename): 178 def _Load(filename):
179 """Loads a JSON file int a Python object and return this object. 179 """Loads a JSON file int a Python object and return this object.
180 """ 180 """
181 # TODO(beaudoin): When moving to Python 2.7 use object_pairs_hook=OrderedDict. 181 # TODO(beaudoin): When moving to Python 2.7 use object_pairs_hook=OrderedDict.
182 with open(filename, 'r') as handle: 182 with open(filename, 'r') as handle:
183 result = json.loads(json_comment_eater.Nom(handle.read())) 183 result = json.loads(json_comment_eater.Nom(handle.read()))
184 return result 184 return result
185 185
186 def GenerateStruct(basepath, output_root, namespace, schema, description,
187 description_filename, schema_filename):
188 """Generates a C++ struct from a JSON description.
189
190 Args:
191 basepath: The base directory in which files are generated.
192 output_root: The filename and path, relative to basepath, of the file to
193 create, without an extension.
194 namespace: A string corresponding to the C++ namespace to use.
195 schema: A dict containing the schema. See comment at the top of this file.
196 description: A dict containing the description. See comment at the top of
197 this file.
198 description_filename: The description filename. This is added to the
199 header of the outputted files.
200 schema_filename: The schema filename. This is added to the header of the
201 outputted files.
202 """
203 head = HEAD % (datetime.now().year, schema_filename, description_filename)
204 _GenerateH(basepath, output_root, head, namespace, schema, description)
205 _GenerateCC(basepath, output_root, head, namespace, schema, description)
206
186 if __name__ == '__main__': 207 if __name__ == '__main__':
187 parser = optparse.OptionParser( 208 parser = optparse.OptionParser(
188 description='Generates an C++ array of struct from a JSON description.', 209 description='Generates an C++ array of struct from a JSON description.',
189 usage='usage: %prog [option] -s schema description') 210 usage='usage: %prog [option] -s schema description')
190 parser.add_option('-b', '--destbase', 211 parser.add_option('-b', '--destbase',
191 help='base directory of generated files.') 212 help='base directory of generated files.')
192 parser.add_option('-d', '--destdir', 213 parser.add_option('-d', '--destdir',
193 help='directory to output generated files, relative to destbase.') 214 help='directory to output generated files, relative to destbase.')
194 parser.add_option('-n', '--namespace', 215 parser.add_option('-n', '--namespace',
195 help='C++ namespace for generated files. e.g search_providers.') 216 help='C++ namespace for generated files. e.g search_providers.')
196 parser.add_option('-s', '--schema', help='path to the schema file, ' 217 parser.add_option('-s', '--schema', help='path to the schema file, '
197 'mandatory.') 218 'mandatory.')
219 parser.add_option('-o', '--output', help='output filename, ')
198 (opts, args) = parser.parse_args() 220 (opts, args) = parser.parse_args()
199 221
200 if not opts.schema: 222 if not opts.schema:
201 parser.error('You must specify a --schema.') 223 parser.error('You must specify a --schema.')
202 224
203 description_filename = os.path.normpath(args[0]) 225 description_filename = os.path.normpath(args[0])
204 root, ext = os.path.splitext(description_filename) 226 root, ext = os.path.splitext(description_filename)
205 shortroot = os.path.split(root)[1] 227 shortroot = opts.output if opts.output else os.path.split(root)[1]
206 if opts.destdir: 228 if opts.destdir:
207 output_root = os.path.join(os.path.normpath(opts.destdir), shortroot) 229 output_root = os.path.join(os.path.normpath(opts.destdir), shortroot)
208 else: 230 else:
209 output_root = shortroot 231 output_root = shortroot
210 232
211 if opts.destbase: 233 if opts.destbase:
212 basepath = os.path.normpath(opts.destbase) 234 basepath = os.path.normpath(opts.destbase)
213 else: 235 else:
214 basepath = '' 236 basepath = ''
215 237
216 schema = _Load(opts.schema) 238 schema = _Load(opts.schema)
217 description = _Load(description_filename) 239 description = _Load(description_filename)
218 240 GenerateStruct(basepath, output_root, opts.namespace, schema, description,
219 head = HEAD % (datetime.now().year, opts.schema, description_filename) 241 description_filename, opts.schema)
220 _GenerateH(basepath, output_root, head, opts.namespace, schema, description)
221 _GenerateCC(basepath, output_root, head, opts.namespace, schema, description)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698