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

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: 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 for element_name, element in content.items():
56 f.write('extern const %s %s;\n' % (schema['type_name'], element_name))
57
58 if namespace:
59 f.write('\n')
60 f.write('} // namespace %s\n' % namespace)
61
62 f.write('\n')
63 f.write( '#endif // %s\n' % header_guard)
64
65 def GenerateCC(fileroot, head, namespace, schema, content):
66 with open(fileroot + '.cc', 'w') as f:
67 f.write(head)
68
69 f.write('#include <stdio.h>\n')
70 f.write('\n')
71 f.write('#include "%s"\n' % (fileroot + '.h'))
72 f.write('\n')
73
74 if namespace:
75 f.write('namespace %s {\n' % namespace)
76 f.write('\n')
77
78 f.write(element_generator.GenerateElements(schema['type_name'],
79 schema['schema'], content))
80
81 if namespace:
82 f.write('\n')
83 f.write('} // namespace %s\n' % namespace)
84
85 def Load(filename):
86 with open(filename, 'r') as handle:
87 schema = json.loads(json_comment_eater.Nom(handle.read()))
88 return schema
89
90 if __name__ == '__main__':
91 parser = optparse.OptionParser(
92 description='Generates an C++ array of struct from a JSON description.',
93 usage='usage: %prog [option] -s schema description')
94 parser.add_option('-d', '--destdir',
95 help='root directory to output generated files.')
96 parser.add_option('-n', '--namespace',
97 help='C++ namespace for generated files. e.g search_providers.')
98 parser.add_option('-s', '--schema', help='path to the schema file, '
99 'mandatory.')
100 (opts, args) = parser.parse_args()
101
102 if not args:
103 sys.exit(0) # This is OK as a no-op
104 if not opts.schema:
105 parser.error('You must specify a --schema.')
106
107 content_filename = os.path.normpath(args[0])
108 root, ext = os.path.splitext(content_filename)
109 shortroot = os.path.split(root)[1]
110 if opts.destdir:
111 output_root = os.path.join(os.path.normpath(opts.destdir), shortroot)
112 else:
113 output_root = shortroot
114
115 schema = Load(opts.schema)
116 content = Load(content_filename)
117
118 head = HEAD % (datetime.now().year, opts.schema, content_filename)
119 GenerateH(output_root, head, opts.namespace, schema, content)
120 GenerateCC(output_root, head, opts.namespace, schema, content)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698