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

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

Issue 15091002: Lazily load API schemas from resource files and convert all APIs to features (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: convert all APIs to features Created 7 years, 7 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 | Annotate | Revision Log
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 189 matching lines...) Expand 10 before | Expand all | Expand 10 after
200 return c 200 return c
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 <set>')
210 c.Append('#include <string>') 211 c.Append('#include <string>')
211 c.Append(); 212 c.Append();
212 c.Append('#include "base/strings/string_piece.h"') 213 c.Append('#include "base/strings/string_piece.h"')
213 c.Append() 214 c.Append()
214 c.Concat(cpp_util.OpenNamespace(self._bundle._cpp_namespace)) 215 c.Concat(cpp_util.OpenNamespace(self._bundle._cpp_namespace))
215 c.Append() 216 c.Append()
216 c.Append('class GeneratedSchemas {') 217 c.Append('class GeneratedSchemas {')
217 c.Sblock(' public:') 218 c.Sblock(' public:')
218 c.Append('// Puts all API schemas in |schemas|.') 219 c.Append('// Puts names for all the schemas in |schemas|.')
219 c.Append('static void Get(' 220 c.Append('static void GetNames(std::set<std::string>* names);')
220 'std::map<std::string, base::StringPiece>* schemas);') 221 c.Append('// Determines if schema named |name| is generated.')
222 c.Append('static bool IsGenerated(std::string name);')
223 c.Append('// Gets the API schema named |name|.')
224 c.Append('static base::StringPiece Get(const std::string& name);')
221 c.Eblock('};'); 225 c.Eblock('};');
222 c.Append() 226 c.Append()
223 c.Concat(cpp_util.CloseNamespace(self._bundle._cpp_namespace)) 227 c.Concat(cpp_util.CloseNamespace(self._bundle._cpp_namespace))
224 return self._bundle._GenerateHeader('generated_schemas', c) 228 return self._bundle._GenerateHeader('generated_schemas', c)
225 229
226 class _SchemasCCGenerator(object): 230 class _SchemasCCGenerator(object):
227 """Generates a code.Code object for the generated schemas .cc file""" 231 """Generates a code.Code object for the generated schemas .cc file"""
228 232
229 def __init__(self, cpp_bundle): 233 def __init__(self, cpp_bundle):
230 self._bundle = cpp_bundle 234 self._bundle = cpp_bundle
231 235
232 def Generate(self, namespace): 236 def Generate(self, namespace):
233 c = code.Code() 237 c = code.Code()
234 c.Append(cpp_util.CHROMIUM_LICENSE) 238 c.Append(cpp_util.CHROMIUM_LICENSE)
235 c.Append() 239 c.Append()
236 c.Append('#include "%s"' % (os.path.join(SOURCE_BASE_PATH, 240 c.Append('#include "%s"' % (os.path.join(SOURCE_BASE_PATH,
237 'generated_schemas.h'))) 241 'generated_schemas.h')))
238 c.Append() 242 c.Append()
239 c.Concat(cpp_util.OpenNamespace(self._bundle._cpp_namespace)) 243 c.Concat(cpp_util.OpenNamespace(self._bundle._cpp_namespace))
240 c.Append() 244 c.Append()
241 c.Append('// static') 245 c.Append('// static')
242 c.Sblock('void GeneratedSchemas::Get(' 246 c.Sblock('void GeneratedSchemas::GetNames(std::set<std::string>* names) {')
243 'std::map<std::string, base::StringPiece>* schemas) {') 247 for api in self._bundle._api_defs:
248 namespace = self._bundle._model.namespaces[api.get('namespace')]
249 c.Append('names->insert("%s");' % namespace.name)
250 c.Eblock('}')
251 c.Append()
252 c.Append('// static')
253 c.Sblock('bool GeneratedSchemas::IsGenerated(std::string name) {')
254 for api in self._bundle._api_defs:
255 namespace = self._bundle._model.namespaces[api.get('namespace')]
256 c.Append('if (name == "%s")' % namespace.name)
257 c.Append(' return true;')
258 c.Append('return false;')
259 c.Eblock('}')
260 c.Append()
261 c.Append('// static')
262 c.Sblock('base::StringPiece GeneratedSchemas::Get('
263 'const std::string& name) {')
not at google - send to devlin 2013/05/20 17:19:04 This is all a bit inefficient: 1 - GetNames return
cduvall 2013/05/22 03:19:56 I made a lazily initialized map of name -> schema
244 for api in self._bundle._api_defs: 264 for api in self._bundle._api_defs:
245 namespace = self._bundle._model.namespaces[api.get('namespace')] 265 namespace = self._bundle._model.namespaces[api.get('namespace')]
246 # JSON parsing code expects lists of schemas, so dump a singleton list. 266 # JSON parsing code expects lists of schemas, so dump a singleton list.
247 json_content = json.dumps([_RemoveDescriptions(api)], 267 json_content = json.dumps([_RemoveDescriptions(api)],
248 separators=(',', ':')) 268 separators=(',', ':'))
249 # Escape all double-quotes and backslashes. For this to output a valid 269 # Escape all double-quotes and backslashes. For this to output a valid
250 # JSON C string, we need to escape \ and ". 270 # JSON C string, we need to escape \ and ".
251 json_content = json_content.replace('\\', '\\\\').replace('"', '\\"') 271 json_content = json_content.replace('\\', '\\\\').replace('"', '\\"')
252 c.Append('(*schemas)["%s"] = "%s";' % (namespace.name, json_content)) 272 c.Sblock('if (name == "%s") {' % namespace.name)
273 c.Append('return "%s";' % json_content)
274 c.Eblock('}')
275 c.Append('return "";');
253 c.Eblock('}') 276 c.Eblock('}')
254 c.Append() 277 c.Append()
255 c.Concat(cpp_util.CloseNamespace(self._bundle._cpp_namespace)) 278 c.Concat(cpp_util.CloseNamespace(self._bundle._cpp_namespace))
256 c.Append() 279 c.Append()
257 return c 280 return c
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698