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

Side by Side Diff: tools/json_schema_compiler/cpp_bundle_generator.py

Issue 23549025: Clean up JSON Schema Compiler. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Removed enum test and added blank lines. Created 7 years, 3 months 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
« no previous file with comments | « tools/json_schema_compiler/compiler.py ('k') | tools/json_schema_compiler/cpp_generator.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be 2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file. 3 # found in the LICENSE file.
4 4
5 import code 5 import code
6 import cpp_util 6 import cpp_util
7 from model import Platforms 7 from model import Platforms
8 from schema_util import CapitalizeFirstLetter 8 from schema_util import CapitalizeFirstLetter
9 from schema_util import JsFunctionNameToClassName 9 from schema_util import JsFunctionNameToClassName
10 10
(...skipping 13 matching lines...) Expand all
24 # Some schemas actually have properties called "description", so only 24 # Some schemas actually have properties called "description", so only
25 # remove descriptions that have string values. 25 # remove descriptions that have string values.
26 if key == 'description' and isinstance(value, basestring): 26 if key == 'description' and isinstance(value, basestring):
27 continue 27 continue
28 result[key] = _RemoveDescriptions(value) 28 result[key] = _RemoveDescriptions(value)
29 return result 29 return result
30 if isinstance(node, list): 30 if isinstance(node, list):
31 return [_RemoveDescriptions(v) for v in node] 31 return [_RemoveDescriptions(v) for v in node]
32 return node 32 return node
33 33
34
34 class CppBundleGenerator(object): 35 class CppBundleGenerator(object):
35 """This class contains methods to generate code based on multiple schemas. 36 """This class contains methods to generate code based on multiple schemas.
36 """ 37 """
37 38
38 def __init__(self, root, model, api_defs, cpp_type_generator, cpp_namespace): 39 def __init__(self, root, model, api_defs, cpp_type_generator, cpp_namespace):
39 self._root = root; 40 self._root = root
40 self._model = model 41 self._model = model
41 self._api_defs = api_defs 42 self._api_defs = api_defs
42 self._cpp_type_generator = cpp_type_generator 43 self._cpp_type_generator = cpp_type_generator
43 self._cpp_namespace = cpp_namespace 44 self._cpp_namespace = cpp_namespace
44 45
45 self.api_cc_generator = _APICCGenerator(self) 46 self.api_cc_generator = _APICCGenerator(self)
46 self.api_h_generator = _APIHGenerator(self) 47 self.api_h_generator = _APIHGenerator(self)
47 self.schemas_cc_generator = _SchemasCCGenerator(self) 48 self.schemas_cc_generator = _SchemasCCGenerator(self)
48 self.schemas_h_generator = _SchemasHGenerator(self) 49 self.schemas_h_generator = _SchemasHGenerator(self)
49 50
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
120 namespace_types_name = JsFunctionNameToClassName( 121 namespace_types_name = JsFunctionNameToClassName(
121 namespace.name, type_.name) 122 namespace.name, type_.name)
122 c.Concat(self._GenerateRegisterFunctions(namespace_types_name, 123 c.Concat(self._GenerateRegisterFunctions(namespace_types_name,
123 function)) 124 function))
124 125
125 if namespace_ifdefs is not None: 126 if namespace_ifdefs is not None:
126 c.Append("#endif // %s" % namespace_ifdefs, indent_level=0) 127 c.Append("#endif // %s" % namespace_ifdefs, indent_level=0)
127 c.Eblock("}") 128 c.Eblock("}")
128 return c 129 return c
129 130
131
130 class _APIHGenerator(object): 132 class _APIHGenerator(object):
131 """Generates the header for API registration / declaration""" 133 """Generates the header for API registration / declaration"""
132 def __init__(self, cpp_bundle): 134 def __init__(self, cpp_bundle):
133 self._bundle = cpp_bundle 135 self._bundle = cpp_bundle
134 136
135 def Generate(self, namespace): 137 def Generate(self, namespace):
136 c = code.Code() 138 c = code.Code()
137 139
138 c.Append('#include <string>') 140 c.Append('#include <string>')
139 c.Append() 141 c.Append()
140 c.Append('#include "base/basictypes.h"') 142 c.Append('#include "base/basictypes.h"')
141 c.Append() 143 c.Append()
142 c.Append("class ExtensionFunctionRegistry;") 144 c.Append("class ExtensionFunctionRegistry;")
143 c.Append() 145 c.Append()
144 c.Concat(cpp_util.OpenNamespace(self._bundle._cpp_namespace)) 146 c.Concat(cpp_util.OpenNamespace(self._bundle._cpp_namespace))
145 c.Append() 147 c.Append()
146 c.Append('class GeneratedFunctionRegistry {') 148 c.Append('class GeneratedFunctionRegistry {')
147 c.Sblock(' public:') 149 c.Sblock(' public:')
148 c.Append('static void RegisterAll(' 150 c.Append('static void RegisterAll('
149 'ExtensionFunctionRegistry* registry);') 151 'ExtensionFunctionRegistry* registry);')
150 c.Eblock('};'); 152 c.Eblock('};')
151 c.Append() 153 c.Append()
152 c.Concat(cpp_util.CloseNamespace(self._bundle._cpp_namespace)) 154 c.Concat(cpp_util.CloseNamespace(self._bundle._cpp_namespace))
153 return self._bundle._GenerateHeader('generated_api', c) 155 return self._bundle._GenerateHeader('generated_api', c)
154 156
157
155 class _APICCGenerator(object): 158 class _APICCGenerator(object):
156 """Generates a code.Code object for the generated API .cc file""" 159 """Generates a code.Code object for the generated API .cc file"""
157 160
158 def __init__(self, cpp_bundle): 161 def __init__(self, cpp_bundle):
159 self._bundle = cpp_bundle 162 self._bundle = cpp_bundle
160 163
161 def Generate(self, namespace): 164 def Generate(self, namespace):
162 c = code.Code() 165 c = code.Code()
163 c.Append(cpp_util.CHROMIUM_LICENSE) 166 c.Append(cpp_util.CHROMIUM_LICENSE)
164 c.Append() 167 c.Append()
(...skipping 27 matching lines...) Expand all
192 '"chrome/browser/extensions/extension_function_registry.h"') 195 '"chrome/browser/extensions/extension_function_registry.h"')
193 c.Append() 196 c.Append()
194 c.Concat(cpp_util.OpenNamespace(self._bundle._cpp_namespace)) 197 c.Concat(cpp_util.OpenNamespace(self._bundle._cpp_namespace))
195 c.Append() 198 c.Append()
196 c.Concat(self._bundle._GenerateFunctionRegistryRegisterAll()) 199 c.Concat(self._bundle._GenerateFunctionRegistryRegisterAll())
197 c.Append() 200 c.Append()
198 c.Concat(cpp_util.CloseNamespace(self._bundle._cpp_namespace)) 201 c.Concat(cpp_util.CloseNamespace(self._bundle._cpp_namespace))
199 c.Append() 202 c.Append()
200 return c 203 return c
201 204
205
202 class _SchemasHGenerator(object): 206 class _SchemasHGenerator(object):
203 """Generates a code.Code object for the generated schemas .h file""" 207 """Generates a code.Code object for the generated schemas .h file"""
204 def __init__(self, cpp_bundle): 208 def __init__(self, cpp_bundle):
205 self._bundle = cpp_bundle 209 self._bundle = cpp_bundle
206 210
207 def Generate(self, namespace): 211 def Generate(self, namespace):
208 c = code.Code() 212 c = code.Code()
209 c.Append('#include <map>') 213 c.Append('#include <map>')
210 c.Append('#include <string>') 214 c.Append('#include <string>')
211 c.Append(); 215 c.Append()
212 c.Append('#include "base/strings/string_piece.h"') 216 c.Append('#include "base/strings/string_piece.h"')
213 c.Append() 217 c.Append()
214 c.Concat(cpp_util.OpenNamespace(self._bundle._cpp_namespace)) 218 c.Concat(cpp_util.OpenNamespace(self._bundle._cpp_namespace))
215 c.Append() 219 c.Append()
216 c.Append('class GeneratedSchemas {') 220 c.Append('class GeneratedSchemas {')
217 c.Sblock(' public:') 221 c.Sblock(' public:')
218 c.Append('// Determines if schema named |name| is generated.') 222 c.Append('// Determines if schema named |name| is generated.')
219 c.Append('static bool IsGenerated(std::string name);') 223 c.Append('static bool IsGenerated(std::string name);')
220 c.Append() 224 c.Append()
221 c.Append('// Gets the API schema named |name|.') 225 c.Append('// Gets the API schema named |name|.')
222 c.Append('static base::StringPiece Get(const std::string& name);') 226 c.Append('static base::StringPiece Get(const std::string& name);')
223 c.Eblock('};'); 227 c.Eblock('};')
224 c.Append() 228 c.Append()
225 c.Concat(cpp_util.CloseNamespace(self._bundle._cpp_namespace)) 229 c.Concat(cpp_util.CloseNamespace(self._bundle._cpp_namespace))
226 return self._bundle._GenerateHeader('generated_schemas', c) 230 return self._bundle._GenerateHeader('generated_schemas', c)
227 231
232
228 def _FormatNameAsConstant(name): 233 def _FormatNameAsConstant(name):
229 """Formats a name to be a C++ constant of the form kConstantName""" 234 """Formats a name to be a C++ constant of the form kConstantName"""
230 name = '%s%s' % (name[0].upper(), name[1:]) 235 name = '%s%s' % (name[0].upper(), name[1:])
231 return 'k%s' % re.sub('_[a-z]', 236 return 'k%s' % re.sub('_[a-z]',
232 lambda m: m.group(0)[1].upper(), 237 lambda m: m.group(0)[1].upper(),
233 name.replace('.', '_')) 238 name.replace('.', '_'))
234 239
240
235 class _SchemasCCGenerator(object): 241 class _SchemasCCGenerator(object):
236 """Generates a code.Code object for the generated schemas .cc file""" 242 """Generates a code.Code object for the generated schemas .cc file"""
237 243
238 def __init__(self, cpp_bundle): 244 def __init__(self, cpp_bundle):
239 self._bundle = cpp_bundle 245 self._bundle = cpp_bundle
240 246
241 def Generate(self, namespace): 247 def Generate(self, namespace):
242 c = code.Code() 248 c = code.Code()
243 c.Append(cpp_util.CHROMIUM_LICENSE) 249 c.Append(cpp_util.CHROMIUM_LICENSE)
244 c.Append() 250 c.Append()
(...skipping 15 matching lines...) Expand all
260 (_FormatNameAsConstant(namespace.name), json_content)) 266 (_FormatNameAsConstant(namespace.name), json_content))
261 c.Append('}') 267 c.Append('}')
262 c.Concat(cpp_util.OpenNamespace(self._bundle._cpp_namespace)) 268 c.Concat(cpp_util.OpenNamespace(self._bundle._cpp_namespace))
263 c.Append() 269 c.Append()
264 c.Sblock('struct Static {') 270 c.Sblock('struct Static {')
265 c.Sblock('Static() {') 271 c.Sblock('Static() {')
266 for api in self._bundle._api_defs: 272 for api in self._bundle._api_defs:
267 namespace = self._bundle._model.namespaces[api.get('namespace')] 273 namespace = self._bundle._model.namespaces[api.get('namespace')]
268 c.Append('schemas["%s"] = %s;' % (namespace.name, 274 c.Append('schemas["%s"] = %s;' % (namespace.name,
269 _FormatNameAsConstant(namespace.name))) 275 _FormatNameAsConstant(namespace.name)))
270 c.Eblock('}'); 276 c.Eblock('}')
271 c.Append() 277 c.Append()
272 c.Append('std::map<std::string, const char*> schemas;') 278 c.Append('std::map<std::string, const char*> schemas;')
273 c.Eblock('};'); 279 c.Eblock('};')
274 c.Append() 280 c.Append()
275 c.Append('base::LazyInstance<Static> g_lazy_instance;') 281 c.Append('base::LazyInstance<Static> g_lazy_instance;')
276 c.Append() 282 c.Append()
277 c.Append('// static') 283 c.Append('// static')
278 c.Sblock('base::StringPiece GeneratedSchemas::Get(' 284 c.Sblock('base::StringPiece GeneratedSchemas::Get('
279 'const std::string& name) {') 285 'const std::string& name) {')
280 c.Append('return IsGenerated(name) ? ' 286 c.Append('return IsGenerated(name) ? '
281 'g_lazy_instance.Get().schemas[name] : "";') 287 'g_lazy_instance.Get().schemas[name] : "";')
282 c.Eblock('}') 288 c.Eblock('}')
283 c.Append() 289 c.Append()
284 c.Append('// static') 290 c.Append('// static')
285 c.Sblock('bool GeneratedSchemas::IsGenerated(std::string name) {') 291 c.Sblock('bool GeneratedSchemas::IsGenerated(std::string name) {')
286 c.Append('return g_lazy_instance.Get().schemas.count(name) > 0;') 292 c.Append('return g_lazy_instance.Get().schemas.count(name) > 0;')
287 c.Eblock('}') 293 c.Eblock('}')
288 c.Append() 294 c.Append()
289 c.Concat(cpp_util.CloseNamespace(self._bundle._cpp_namespace)) 295 c.Concat(cpp_util.CloseNamespace(self._bundle._cpp_namespace))
290 c.Append() 296 c.Append()
291 return c 297 return c
OLDNEW
« no previous file with comments | « tools/json_schema_compiler/compiler.py ('k') | tools/json_schema_compiler/cpp_generator.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698