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

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

Issue 11377049: Moving prepopulated search engines to a JSON file. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Answered review comments. Created 8 years, 1 month 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
(Empty)
1 # Copyright 2012 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
4
5 import json
6 from datetime import datetime
7 import os.path
8 import sys
9 import optparse
10 _script_path = os.path.realpath(__file__)
11 sys.path.insert(0, os.path.normpath(_script_path + "/../../"))
12 import json_comment_eater
13 import struct_generator
14 import element_generator
15
16 HEAD = """// Copyright (c) %d The Chromium Authors. All rights reserved.
17 // Use of this source code is governed by a BSD-style license that can be
18 // found in the LICENSE file.
19
20 // GENERATED FROM THE SCHEMA DEFINITION AND CONTENT IN
21 // %s
22 // %s
23 // DO NOT EDIT.
24
25 """
26
27 def GenerateHeaderGuard(h_filename):
28 return (('%s_' % h_filename)
29 .upper().replace(os.sep, '_').replace('/', '_').replace('.', '_'))
30
31 def GenerateH(fileroot, head, namespace, schema, content):
32 h_filename = fileroot + '.h'
33 with open(h_filename, 'w') as f:
34 f.write(head)
35 header_guard = GenerateHeaderGuard(h_filename)
36 f.write('#ifndef %s\n' % header_guard)
37 f.write('#define %s\n' % header_guard)
38 f.write('\n')
39
40 try:
41 for header in schema['headers']:
42 f.write('#include "%s"\n' % header)
43 except KeyError:
44 pass
45 f.write('\n')
46
47 if namespace:
48 f.write('namespace %s {\n' % namespace)
49 f.write('\n')
50
51 f.write(struct_generator.GenerateStruct(
52 schema['type_name'], schema['schema']))
53 f.write('\n')
54
55 f.write('extern const int kCurrentDataVersion;')
56 f.write('\n')
57
58 for element_name, element in content['engines'].items():
59 f.write('extern const %s %s;\n' % (schema['type_name'], element_name))
60
61 if namespace:
62 f.write('\n')
63 f.write('} // namespace %s\n' % namespace)
64
65 f.write('\n')
66 f.write( '#endif // %s\n' % header_guard)
67
68 def GenerateCC(fileroot, head, namespace, schema, content):
69 with open(fileroot + '.cc', 'w') as f:
70 f.write(head)
71
72 f.write('#include <stdio.h>\n')
73 f.write('\n')
74 f.write('#include "%s"\n' % (fileroot + '.h'))
75 f.write('\n')
76
77 if namespace:
78 f.write('namespace %s {\n' % namespace)
79 f.write('\n')
80
81 f.write(element_generator.GenerateElements(schema['type_name'],
82 schema['schema'], content))
83
84 if namespace:
85 f.write('\n')
86 f.write('} // namespace %s\n' % namespace)
87
88 def Load(filename):
89 with open(filename, 'r') as handle:
90 schema = json.loads(json_comment_eater.Nom(handle.read()))
91 return schema
92
93 if __name__ == '__main__':
94 parser = optparse.OptionParser(
95 description='Generates an C++ array of struct from a JSON description.',
96 usage='usage: %prog [option] -s schema description')
97 parser.add_option('-d', '--destdir',
98 help='root directory to output generated files.')
99 parser.add_option('-n', '--namespace',
100 help='C++ namespace for generated files. e.g search_providers.')
101 parser.add_option('-s', '--schema', help='path to the schema file, '
102 'mandatory.')
103 (opts, args) = parser.parse_args()
104
105 if not args:
106 sys.exit(0) # This is OK as a no-op
107 if not opts.schema:
108 parser.error('You must specify a --schema.')
109
110 content_filename = os.path.normpath(args[0])
111 root, ext = os.path.splitext(content_filename)
112 shortroot = os.path.split(root)[1]
113 if opts.destdir:
114 output_root = os.path.join(os.path.normpath(opts.destdir), shortroot)
115 else:
116 output_root = shortroot
117
118 schema = Load(opts.schema)
119 content = Load(content_filename)
120
121 head = HEAD % (datetime.now().year, opts.schema, content_filename)
122 GenerateH(output_root, head, opts.namespace, schema, content)
123 GenerateCC(output_root, head, opts.namespace, schema, content)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698