Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 #!/usr/bin/env python | |
| 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 | |
| 4 # found in the LICENSE file. | |
| 5 | |
| 6 # Format for the JSON schema file: | |
| 7 # { | |
| 8 # "type_name": "DesiredCStructName", | |
| 9 # "headers": [ // Optional list of headers to be included by the .h. | |
| 10 # "path/to/header.h" | |
| 11 # ], | |
| 12 # "schema": [ // Fields of the generated structure. | |
| 13 # { | |
| 14 # "field": "my_enum_field", | |
| 15 # "type": "enum", // Either: int, string, string16, enum, array. | |
| 16 # "default": "RED", // Optional. Cannot be used for array. | |
| 17 # "ctype": "Color" // Only for enum, specify the C type. | |
| 18 # }, | |
| 19 # { | |
| 20 # "field": "my_int_array_field", | |
| 21 # "type": "array", | |
| 22 # "contents": { | |
| 23 # "type": "int" // Either: int, string, string16, enum, array. | |
| 24 # } | |
| 25 # }, | |
| 26 # ... | |
| 27 # ] | |
| 28 # } | |
| 29 # | |
| 30 # Format for the JSON description file: | |
| 31 # { | |
| 32 # "int_variables": { // An optional list of constant int variables. | |
| 33 # "kDesiredConstantName": 45 | |
| 34 # }, | |
| 35 # "elements": { // All the elements for which to create static | |
| 36 # // initialization code in the .cc file. | |
| 37 # "my_const_variable": { | |
| 38 # "my_int_field": 10, | |
| 39 # "my_string_field": "foo bar", | |
| 40 # "my_enum_field": "BLACK", | |
| 41 # "my_int_array_field": [ 1, 2, 3, 5, 7 ], | |
| 42 # }, | |
| 43 # "my_other_const_variable": { | |
| 44 # ... | |
| 45 # } | |
| 46 # } | |
| 47 # } | |
| 48 | |
| 49 import json | |
| 50 from datetime import datetime | |
| 51 import os.path | |
| 52 import sys | |
| 53 import optparse | |
| 54 _script_path = os.path.realpath(__file__) | |
| 55 sys.path.insert(0, os.path.normpath(_script_path + "/../../")) | |
|
not at google - send to devlin
2012/11/12 18:38:13
I think it'd be polite to remove this from the pat
beaudoin
2012/11/13 18:44:26
Done.
| |
| 56 import json_comment_eater | |
| 57 import struct_generator | |
| 58 import element_generator | |
| 59 | |
| 60 HEAD = """// Copyright (c) %d The Chromium Authors. All rights reserved. | |
|
not at google - send to devlin
2012/11/12 18:38:13
I thought we weren't generating the (c) anymore?
beaudoin
2012/11/13 18:44:26
Done.
| |
| 61 // Use of this source code is governed by a BSD-style license that can be | |
| 62 // found in the LICENSE file. | |
| 63 | |
| 64 // GENERATED FROM THE SCHEMA DEFINITION AND DESCRIPTION IN | |
| 65 // %s | |
| 66 // %s | |
| 67 // DO NOT EDIT. | |
| 68 | |
| 69 """ | |
| 70 | |
| 71 def GenerateHeaderGuard(h_filename): | |
|
not at google - send to devlin
2012/11/12 18:38:13
Private functions should begin with an underscore,
beaudoin
2012/11/13 18:44:26
Done.
| |
| 72 """Generates the string used in #ifndef guarding the header file. | |
| 73 """ | |
| 74 return (('%s_' % h_filename) | |
| 75 .upper().replace(os.sep, '_').replace('/', '_').replace('.', '_')) | |
| 76 | |
| 77 def GenerateH(fileroot, head, namespace, schema, description): | |
| 78 """Generates the .h file containing the definition of the structure specified | |
| 79 by the schema. | |
| 80 | |
| 81 Args: | |
| 82 fileroot: The filename and path of the file to create, without an extension. | |
| 83 head: The string to output as the header of the .h file. | |
| 84 namespace: A string corresponding to the C++ namespace to use. | |
| 85 schema: A dict containing the schema. See comment at the top of this file. | |
| 86 description: A dict containing the description. See comment at the top of | |
| 87 this file. | |
| 88 """ | |
| 89 | |
| 90 h_filename = fileroot + '.h' | |
| 91 with open(h_filename, 'w') as f: | |
| 92 f.write(head) | |
| 93 header_guard = GenerateHeaderGuard(h_filename) | |
|
not at google - send to devlin
2012/11/12 18:38:13
You might find the Code class in tools/json_schema
beaudoin
2012/11/13 18:44:26
Yeah, as noted elsewhere, I thought about using it
not at google - send to devlin
2012/11/13 20:28:07
See previous comment. Why does it require that?
| |
| 94 f.write('#ifndef %s\n' % header_guard) | |
| 95 f.write('#define %s\n' % header_guard) | |
| 96 f.write('\n') | |
| 97 | |
| 98 try: | |
| 99 for header in schema['headers']: | |
| 100 f.write('#include "%s"\n' % header) | |
| 101 except KeyError: | |
| 102 pass | |
| 103 f.write('\n') | |
| 104 | |
| 105 if namespace: | |
| 106 f.write('namespace %s {\n' % namespace) | |
| 107 f.write('\n') | |
| 108 | |
| 109 f.write(struct_generator.GenerateStruct( | |
| 110 schema['type_name'], schema['schema'])) | |
| 111 f.write('\n') | |
| 112 | |
| 113 try: | |
| 114 for var_name, value in description['int_variables'].items(): | |
| 115 f.write('extern const int %s;\n' % var_name) | |
| 116 f.write('\n') | |
| 117 except KeyError: | |
| 118 pass | |
| 119 | |
| 120 for element_name, element in description['elements'].items(): | |
| 121 f.write('extern const %s %s;\n' % (schema['type_name'], element_name)) | |
| 122 | |
| 123 if namespace: | |
| 124 f.write('\n') | |
| 125 f.write('} // namespace %s\n' % namespace) | |
| 126 | |
| 127 f.write('\n') | |
| 128 f.write( '#endif // %s\n' % header_guard) | |
| 129 | |
| 130 def GenerateCC(fileroot, head, namespace, schema, description): | |
| 131 """Generates the .cc file containing the static initializers for the | |
| 132 of the elements specified in the description. | |
| 133 | |
| 134 Args: | |
| 135 fileroot: The filename and path of the file to create, without an extension. | |
| 136 head: The string to output as the header of the .cc file. | |
| 137 namespace: A string corresponding to the C++ namespace to use. | |
| 138 schema: A dict containing the schema. See comment at the top of this file. | |
| 139 description: A dict containing the description. See comment at the top of | |
| 140 this file. | |
| 141 """ | |
| 142 with open(fileroot + '.cc', 'w') as f: | |
| 143 f.write(head) | |
| 144 | |
| 145 f.write('#include <stdio.h>\n') | |
| 146 f.write('\n') | |
| 147 f.write('#include "%s"\n' % (fileroot + '.h')) | |
| 148 f.write('\n') | |
| 149 | |
| 150 if namespace: | |
| 151 f.write('namespace %s {\n' % namespace) | |
| 152 f.write('\n') | |
| 153 | |
| 154 f.write(element_generator.GenerateElements(schema['type_name'], | |
| 155 schema['schema'], description)) | |
| 156 | |
| 157 if namespace: | |
| 158 f.write('\n') | |
| 159 f.write('} // namespace %s\n' % namespace) | |
| 160 | |
| 161 def Load(filename): | |
| 162 """Loads a JSON file int a Python object and return this object. | |
| 163 """ | |
| 164 with open(filename, 'r') as handle: | |
| 165 result = json.loads(json_comment_eater.Nom(handle.read())) | |
| 166 return result | |
| 167 | |
| 168 if __name__ == '__main__': | |
| 169 parser = optparse.OptionParser( | |
| 170 description='Generates an C++ array of struct from a JSON description.', | |
| 171 usage='usage: %prog [option] -s schema description') | |
| 172 parser.add_option('-d', '--destdir', | |
| 173 help='root directory to output generated files.') | |
| 174 parser.add_option('-n', '--namespace', | |
| 175 help='C++ namespace for generated files. e.g search_providers.') | |
| 176 parser.add_option('-s', '--schema', help='path to the schema file, ' | |
| 177 'mandatory.') | |
| 178 (opts, args) = parser.parse_args() | |
| 179 | |
| 180 if not args: | |
| 181 sys.exit(0) # This is OK as a no-op | |
|
not at google - send to devlin
2012/11/12 18:38:13
why? when can this happen?
beaudoin
2012/11/13 18:44:26
Good question. Cut-and-pasted from the json_schema
| |
| 182 if not opts.schema: | |
| 183 parser.error('You must specify a --schema.') | |
| 184 | |
| 185 description_filename = os.path.normpath(args[0]) | |
| 186 root, ext = os.path.splitext(description_filename) | |
| 187 shortroot = os.path.split(root)[1] | |
| 188 if opts.destdir: | |
| 189 output_root = os.path.join(os.path.normpath(opts.destdir), shortroot) | |
| 190 else: | |
| 191 output_root = shortroot | |
| 192 | |
| 193 schema = Load(opts.schema) | |
| 194 description = Load(description_filename) | |
| 195 | |
| 196 head = HEAD % (datetime.now().year, opts.schema, description_filename) | |
| 197 GenerateH(output_root, head, opts.namespace, schema, description) | |
|
not at google - send to devlin
2012/11/12 18:38:13
Passing head into these methods seems unnecessary,
beaudoin
2012/11/13 18:44:26
That GenerateCopyright function would need the opt
not at google - send to devlin
2012/11/13 20:28:07
My apologies, I didn't see that.
Different commen
| |
| 198 GenerateCC(output_root, head, opts.namespace, schema, description) | |
| OLD | NEW |