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

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: 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
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 18 matching lines...) Expand all
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 class CppBundleGenerator(object): 34 class CppBundleGenerator(object):
35 """This class contains methods to generate code based on multiple schemas. 35 """This class contains methods to generate code based on multiple schemas.
36 """ 36 """
37 37
38 def __init__(self, root, model, api_defs, cpp_type_generator, cpp_namespace): 38 def __init__(self, root, model, api_defs, cpp_type_generator, cpp_namespace):
39 self._root = root; 39 self._root = root
40 self._model = model 40 self._model = model
41 self._api_defs = api_defs 41 self._api_defs = api_defs
42 self._cpp_type_generator = cpp_type_generator 42 self._cpp_type_generator = cpp_type_generator
43 self._cpp_namespace = cpp_namespace 43 self._cpp_namespace = cpp_namespace
44 44
45 self.api_cc_generator = _APICCGenerator(self) 45 self.api_cc_generator = _APICCGenerator(self)
46 self.api_h_generator = _APIHGenerator(self) 46 self.api_h_generator = _APIHGenerator(self)
47 self.schemas_cc_generator = _SchemasCCGenerator(self) 47 self.schemas_cc_generator = _SchemasCCGenerator(self)
48 self.schemas_h_generator = _SchemasHGenerator(self) 48 self.schemas_h_generator = _SchemasHGenerator(self)
49 49
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after
140 c.Append('#include "base/basictypes.h"') 140 c.Append('#include "base/basictypes.h"')
141 c.Append() 141 c.Append()
142 c.Append("class ExtensionFunctionRegistry;") 142 c.Append("class ExtensionFunctionRegistry;")
143 c.Append() 143 c.Append()
144 c.Concat(cpp_util.OpenNamespace(self._bundle._cpp_namespace)) 144 c.Concat(cpp_util.OpenNamespace(self._bundle._cpp_namespace))
145 c.Append() 145 c.Append()
146 c.Append('class GeneratedFunctionRegistry {') 146 c.Append('class GeneratedFunctionRegistry {')
147 c.Sblock(' public:') 147 c.Sblock(' public:')
148 c.Append('static void RegisterAll(' 148 c.Append('static void RegisterAll('
149 'ExtensionFunctionRegistry* registry);') 149 'ExtensionFunctionRegistry* registry);')
150 c.Eblock('};'); 150 c.Eblock('};')
151 c.Append() 151 c.Append()
152 c.Concat(cpp_util.CloseNamespace(self._bundle._cpp_namespace)) 152 c.Concat(cpp_util.CloseNamespace(self._bundle._cpp_namespace))
153 return self._bundle._GenerateHeader('generated_api', c) 153 return self._bundle._GenerateHeader('generated_api', c)
154 154
155 class _APICCGenerator(object): 155 class _APICCGenerator(object):
156 """Generates a code.Code object for the generated API .cc file""" 156 """Generates a code.Code object for the generated API .cc file"""
157 157
158 def __init__(self, cpp_bundle): 158 def __init__(self, cpp_bundle):
159 self._bundle = cpp_bundle 159 self._bundle = cpp_bundle
160 160
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
201 201
202 class _SchemasHGenerator(object): 202 class _SchemasHGenerator(object):
203 """Generates a code.Code object for the generated schemas .h file""" 203 """Generates a code.Code object for the generated schemas .h file"""
204 def __init__(self, cpp_bundle): 204 def __init__(self, cpp_bundle):
205 self._bundle = cpp_bundle 205 self._bundle = cpp_bundle
206 206
207 def Generate(self, namespace): 207 def Generate(self, namespace):
208 c = code.Code() 208 c = code.Code()
209 c.Append('#include <map>') 209 c.Append('#include <map>')
210 c.Append('#include <string>') 210 c.Append('#include <string>')
211 c.Append(); 211 c.Append()
212 c.Append('#include "base/strings/string_piece.h"') 212 c.Append('#include "base/strings/string_piece.h"')
213 c.Append() 213 c.Append()
214 c.Concat(cpp_util.OpenNamespace(self._bundle._cpp_namespace)) 214 c.Concat(cpp_util.OpenNamespace(self._bundle._cpp_namespace))
215 c.Append() 215 c.Append()
216 c.Append('class GeneratedSchemas {') 216 c.Append('class GeneratedSchemas {')
217 c.Sblock(' public:') 217 c.Sblock(' public:')
218 c.Append('// Determines if schema named |name| is generated.') 218 c.Append('// Determines if schema named |name| is generated.')
219 c.Append('static bool IsGenerated(std::string name);') 219 c.Append('static bool IsGenerated(std::string name);')
220 c.Append() 220 c.Append()
221 c.Append('// Gets the API schema named |name|.') 221 c.Append('// Gets the API schema named |name|.')
222 c.Append('static base::StringPiece Get(const std::string& name);') 222 c.Append('static base::StringPiece Get(const std::string& name);')
223 c.Eblock('};'); 223 c.Eblock('};')
224 c.Append() 224 c.Append()
225 c.Concat(cpp_util.CloseNamespace(self._bundle._cpp_namespace)) 225 c.Concat(cpp_util.CloseNamespace(self._bundle._cpp_namespace))
226 return self._bundle._GenerateHeader('generated_schemas', c) 226 return self._bundle._GenerateHeader('generated_schemas', c)
227 227
228 def _FormatNameAsConstant(name): 228 def _FormatNameAsConstant(name):
229 """Formats a name to be a C++ constant of the form kConstantName""" 229 """Formats a name to be a C++ constant of the form kConstantName"""
230 name = '%s%s' % (name[0].upper(), name[1:]) 230 name = '%s%s' % (name[0].upper(), name[1:])
231 return 'k%s' % re.sub('_[a-z]', 231 return 'k%s' % re.sub('_[a-z]',
232 lambda m: m.group(0)[1].upper(), 232 lambda m: m.group(0)[1].upper(),
233 name.replace('.', '_')) 233 name.replace('.', '_'))
(...skipping 26 matching lines...) Expand all
260 (_FormatNameAsConstant(namespace.name), json_content)) 260 (_FormatNameAsConstant(namespace.name), json_content))
261 c.Append('}') 261 c.Append('}')
262 c.Concat(cpp_util.OpenNamespace(self._bundle._cpp_namespace)) 262 c.Concat(cpp_util.OpenNamespace(self._bundle._cpp_namespace))
263 c.Append() 263 c.Append()
264 c.Sblock('struct Static {') 264 c.Sblock('struct Static {')
265 c.Sblock('Static() {') 265 c.Sblock('Static() {')
266 for api in self._bundle._api_defs: 266 for api in self._bundle._api_defs:
267 namespace = self._bundle._model.namespaces[api.get('namespace')] 267 namespace = self._bundle._model.namespaces[api.get('namespace')]
268 c.Append('schemas["%s"] = %s;' % (namespace.name, 268 c.Append('schemas["%s"] = %s;' % (namespace.name,
269 _FormatNameAsConstant(namespace.name))) 269 _FormatNameAsConstant(namespace.name)))
270 c.Eblock('}'); 270 c.Eblock('}')
271 c.Append() 271 c.Append()
272 c.Append('std::map<std::string, const char*> schemas;') 272 c.Append('std::map<std::string, const char*> schemas;')
273 c.Eblock('};'); 273 c.Eblock('};')
274 c.Append() 274 c.Append()
275 c.Append('base::LazyInstance<Static> g_lazy_instance;') 275 c.Append('base::LazyInstance<Static> g_lazy_instance;')
276 c.Append() 276 c.Append()
277 c.Append('// static') 277 c.Append('// static')
278 c.Sblock('base::StringPiece GeneratedSchemas::Get(' 278 c.Sblock('base::StringPiece GeneratedSchemas::Get('
279 'const std::string& name) {') 279 'const std::string& name) {')
280 c.Append('return IsGenerated(name) ? ' 280 c.Append('return IsGenerated(name) ? '
281 'g_lazy_instance.Get().schemas[name] : "";') 281 'g_lazy_instance.Get().schemas[name] : "";')
282 c.Eblock('}') 282 c.Eblock('}')
283 c.Append() 283 c.Append()
284 c.Append('// static') 284 c.Append('// static')
285 c.Sblock('bool GeneratedSchemas::IsGenerated(std::string name) {') 285 c.Sblock('bool GeneratedSchemas::IsGenerated(std::string name) {')
286 c.Append('return g_lazy_instance.Get().schemas.count(name) > 0;') 286 c.Append('return g_lazy_instance.Get().schemas.count(name) > 0;')
287 c.Eblock('}') 287 c.Eblock('}')
288 c.Append() 288 c.Append()
289 c.Concat(cpp_util.CloseNamespace(self._bundle._cpp_namespace)) 289 c.Concat(cpp_util.CloseNamespace(self._bundle._cpp_namespace))
290 c.Append() 290 c.Append()
291 return c 291 return c
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698