diff options
| author | danakj@chromium.org <danakj@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-11-15 00:54:38 +0000 |
|---|---|---|
| committer | danakj@chromium.org <danakj@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-11-15 00:54:38 +0000 |
| commit | 0c66c98b2d59bd69e38722792e4331646ffc96b3 (patch) | |
| tree | 64a5d352c8b61dacccf9955729527b8f334ddec5 /tools/json_to_struct/json_to_struct.py | |
| parent | aa212dba7c8d69986c797f6a383f79fcec09b248 (diff) | |
| download | chromium_src-0c66c98b2d59bd69e38722792e4331646ffc96b3.zip chromium_src-0c66c98b2d59bd69e38722792e4331646ffc96b3.tar.gz chromium_src-0c66c98b2d59bd69e38722792e4331646ffc96b3.tar.bz2 | |
Revert 167793 - Moving prepopulated search engines to a JSON file.
This CL also includes the python script required to convert the JSON file to a .cc/.h pair. The generated .cc/.h are not generated by the build process and must be committed to the repository.
BUG=159990
Review URL: https://chromiumcodereview.appspot.com/11377049
TBR=beaudoin@chromium.org
Compiler errors:
http://build.chromium.org/p/chromium.chromiumos/buildstatus?builder=Linux%20ChromiumOS%20Builder&number=28409
http://build.chromium.org/p/chromium.win/buildstatus?builder=Win%20Builder%20%28dbg%29&number=4778
Review URL: https://codereview.chromium.org/11293291
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@167796 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'tools/json_to_struct/json_to_struct.py')
| -rwxr-xr-x | tools/json_to_struct/json_to_struct.py | 199 |
1 files changed, 0 insertions, 199 deletions
diff --git a/tools/json_to_struct/json_to_struct.py b/tools/json_to_struct/json_to_struct.py deleted file mode 100755 index 8dfda44..0000000 --- a/tools/json_to_struct/json_to_struct.py +++ /dev/null @@ -1,199 +0,0 @@ -#!/usr/bin/env python -# 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. - -# Format for the JSON schema file: -# { -# "type_name": "DesiredCStructName", -# "headers": [ // Optional list of headers to be included by the .h. -# "path/to/header.h" -# ], -# "schema": [ // Fields of the generated structure. -# { -# "field": "my_enum_field", -# "type": "enum", // Either: int, string, string16, enum, array. -# "default": "RED", // Optional. Cannot be used for array. -# "ctype": "Color" // Only for enum, specify the C type. -# }, -# { -# "field": "my_int_array_field", // my_int_array_field_size will also -# "type": "array", // be generated. -# "contents": { -# "type": "int" // Either: int, string, string16, enum, array. -# } -# }, -# ... -# ] -# } -# -# Format for the JSON description file: -# { -# "int_variables": { // An optional list of constant int variables. -# "kDesiredConstantName": 45 -# }, -# "elements": { // All the elements for which to create static -# // initialization code in the .cc file. -# "my_const_variable": { -# "my_int_field": 10, -# "my_string_field": "foo bar", -# "my_enum_field": "BLACK", -# "my_int_array_field": [ 1, 2, 3, 5, 7 ], -# }, -# "my_other_const_variable": { -# ... -# } -# } -# } - -import json -from datetime import datetime -import os.path -import sys -import optparse -import copy -_script_path = os.path.realpath(__file__) - -sys.path.insert(0, os.path.normpath(_script_path + "/../../")) -try: - import json_comment_eater -finally: - sys.path.pop(0) - -import struct_generator -import element_generator - -HEAD = """// Copyright %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 DESCRIPTION IN -// %s -// %s -// DO NOT EDIT. - -""" - -def _GenerateHeaderGuard(h_filename): - """Generates the string used in #ifndef guarding the header file. - """ - return (('%s_' % h_filename) - .upper().replace(os.sep, '_').replace('/', '_').replace('.', '_')) - -def _GenerateH(fileroot, head, namespace, schema, description): - """Generates the .h file containing the definition of the structure specified - by the schema. - - Args: - fileroot: The filename and path of the file to create, without an extension. - head: The string to output as the header of the .h file. - namespace: A string corresponding to the C++ namespace to use. - schema: A dict containing the schema. See comment at the top of this file. - description: A dict containing the description. See comment at the top of - this file. - """ - - h_filename = fileroot + '.h' - with open(h_filename, 'w') as f: - f.write(head) - - f.write('#include <cstddef>\n') - f.write('\n') - - header_guard = _GenerateHeaderGuard(h_filename) - f.write('#ifndef %s\n' % header_guard) - f.write('#define %s\n' % header_guard) - f.write('\n') - - for header in schema.get('headers', []): - f.write('#include "%s"\n' % header) - 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 var_name, value in description.get('int_variables', []).items(): - f.write('extern const int %s;\n' % var_name) - f.write('\n') - - for element_name, element in description['elements'].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, description): - """Generates the .cc file containing the static initializers for the - of the elements specified in the description. - - Args: - fileroot: The filename and path of the file to create, without an extension. - head: The string to output as the header of the .cc file. - namespace: A string corresponding to the C++ namespace to use. - schema: A dict containing the schema. See comment at the top of this file. - description: A dict containing the description. See comment at the top of - this file. - """ - with open(fileroot + '.cc', 'w') as f: - f.write(head) - - 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'], description)) - - if namespace: - f.write('\n') - f.write('} // namespace %s\n' % namespace) - -def _Load(filename): - """Loads a JSON file int a Python object and return this object. - """ - # TODO(beaudoin): When moving to Python 2.7 use object_pairs_hook=OrderedDict. - with open(filename, 'r') as handle: - result = json.loads(json_comment_eater.Nom(handle.read())) - return result - -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 opts.schema: - parser.error('You must specify a --schema.') - - description_filename = os.path.normpath(args[0]) - root, ext = os.path.splitext(description_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) - description = _Load(description_filename) - - head = HEAD % (datetime.now().year, opts.schema, description_filename) - _GenerateH(output_root, head, opts.namespace, schema, description) - _GenerateCC(output_root, head, opts.namespace, schema, description) |
