| Index: tools/json_to_struct/json_to_struct.py
|
| diff --git a/tools/json_to_struct/json_to_struct.py b/tools/json_to_struct/json_to_struct.py
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..c659203f1e6018b1d08484bc83a9e6aac868d501
|
| --- /dev/null
|
| +++ b/tools/json_to_struct/json_to_struct.py
|
| @@ -0,0 +1,120 @@
|
| +# Copyright 2012 The Chromium Authors. All rights reserved.
|
| +# Use of this source code is governed by a BSD-style license that can be
|
| +# found in the LICENSE file.
|
| +
|
| +import json
|
| +from datetime import datetime
|
| +import os.path
|
| +import sys
|
| +import optparse
|
| +_script_path = os.path.realpath(__file__)
|
| +sys.path.insert(0, os.path.normpath(_script_path + "/../../"))
|
| +import json_comment_eater
|
| +import struct_generator
|
| +import element_generator
|
| +
|
| +HEAD = """// Copyright (c) %d The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +// GENERATED FROM THE SCHEMA DEFINITION AND CONTENT IN
|
| +// %s
|
| +// %s
|
| +// DO NOT EDIT.
|
| +
|
| +"""
|
| +
|
| +def GenerateHeaderGuard(h_filename):
|
| + return (('%s_' % h_filename)
|
| + .upper().replace(os.sep, '_').replace('/', '_').replace('.', '_'))
|
| +
|
| +def GenerateH(fileroot, head, namespace, schema, content):
|
| + h_filename = fileroot + '.h'
|
| + with open(h_filename, 'w') as f:
|
| + f.write(head)
|
| + header_guard = GenerateHeaderGuard(h_filename)
|
| + f.write('#ifndef %s\n' % header_guard)
|
| + f.write('#define %s\n' % header_guard)
|
| + f.write('\n')
|
| +
|
| + try:
|
| + for header in schema['headers']:
|
| + f.write('#include "%s"\n' % header)
|
| + except KeyError:
|
| + pass
|
| + f.write('\n')
|
| +
|
| + if namespace:
|
| + f.write('namespace %s {\n' % namespace)
|
| + f.write('\n')
|
| +
|
| + f.write(struct_generator.GenerateStruct(
|
| + schema['type_name'], schema['schema']))
|
| + f.write('\n')
|
| +
|
| + for element_name, element in content.items():
|
| + f.write('extern const %s %s;\n' % (schema['type_name'], element_name))
|
| +
|
| + if namespace:
|
| + f.write('\n')
|
| + f.write('} // namespace %s\n' % namespace)
|
| +
|
| + f.write('\n')
|
| + f.write( '#endif // %s\n' % header_guard)
|
| +
|
| +def GenerateCC(fileroot, head, namespace, schema, content):
|
| + with open(fileroot + '.cc', 'w') as f:
|
| + f.write(head)
|
| +
|
| + f.write('#include <stdio.h>\n')
|
| + f.write('\n')
|
| + f.write('#include "%s"\n' % (fileroot + '.h'))
|
| + f.write('\n')
|
| +
|
| + if namespace:
|
| + f.write('namespace %s {\n' % namespace)
|
| + f.write('\n')
|
| +
|
| + f.write(element_generator.GenerateElements(schema['type_name'],
|
| + schema['schema'], content))
|
| +
|
| + if namespace:
|
| + f.write('\n')
|
| + f.write('} // namespace %s\n' % namespace)
|
| +
|
| +def Load(filename):
|
| + with open(filename, 'r') as handle:
|
| + schema = json.loads(json_comment_eater.Nom(handle.read()))
|
| + return schema
|
| +
|
| +if __name__ == '__main__':
|
| + parser = optparse.OptionParser(
|
| + description='Generates an C++ array of struct from a JSON description.',
|
| + usage='usage: %prog [option] -s schema description')
|
| + parser.add_option('-d', '--destdir',
|
| + help='root directory to output generated files.')
|
| + parser.add_option('-n', '--namespace',
|
| + help='C++ namespace for generated files. e.g search_providers.')
|
| + parser.add_option('-s', '--schema', help='path to the schema file, '
|
| + 'mandatory.')
|
| + (opts, args) = parser.parse_args()
|
| +
|
| + if not args:
|
| + sys.exit(0) # This is OK as a no-op
|
| + if not opts.schema:
|
| + parser.error('You must specify a --schema.')
|
| +
|
| + content_filename = os.path.normpath(args[0])
|
| + root, ext = os.path.splitext(content_filename)
|
| + shortroot = os.path.split(root)[1]
|
| + if opts.destdir:
|
| + output_root = os.path.join(os.path.normpath(opts.destdir), shortroot)
|
| + else:
|
| + output_root = shortroot
|
| +
|
| + schema = Load(opts.schema)
|
| + content = Load(content_filename)
|
| +
|
| + head = HEAD % (datetime.now().year, opts.schema, content_filename)
|
| + GenerateH(output_root, head, opts.namespace, schema, content)
|
| + GenerateCC(output_root, head, opts.namespace, schema, content)
|
|
|