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

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

Issue 2266573003: [Extensions] Trim some fat around what we use in generated schemas (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Update comment, return copy Created 4 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 | « no previous file | no next file » | 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
11 import copy
11 import json 12 import json
12 import os 13 import os
13 import re 14 import re
14 15
15 16
16 def _RemoveDescriptions(node): 17 def _RemoveKey(node, key, type_restriction):
17 """Returns a copy of |schema| with "description" fields removed. 18 if isinstance(node, dict):
19 if key in node and isinstance(node[key], type_restriction):
20 del node[key]
21 for value in node.values():
22 _RemoveKey(value, key, type_restriction)
23 elif isinstance(node, list):
24 for value in node:
25 _RemoveKey(value, key, type_restriction)
26
27 def _RemoveUnneededFields(schema):
28 """Returns a copy of |schema| with fields that aren't necessary at runtime
29 removed.
18 """ 30 """
19 if isinstance(node, dict): 31 # Return a copy so that we don't pollute the global api object, which may be
20 result = {} 32 # used elsewhere.
21 for key, value in node.items(): 33 ret = copy.deepcopy(schema)
Devlin 2016/08/22 17:08:58 Note: I changed this (back) to returning a copy, b
22 # Some schemas actually have properties called "description", so only 34 _RemoveKey(ret, "description", basestring)
23 # remove descriptions that have string values. 35 _RemoveKey(ret, "compiler_options", dict)
24 if key == 'description' and isinstance(value, basestring): 36 _RemoveKey(ret, "nodoc", bool)
25 continue 37 _RemoveKey(ret, "noinline_doc", bool)
26 result[key] = _RemoveDescriptions(value) 38 return ret
27 return result
28 if isinstance(node, list):
29 return [_RemoveDescriptions(v) for v in node]
30 return node
31 39
32 40
33 class CppBundleGenerator(object): 41 class CppBundleGenerator(object):
34 """This class contains methods to generate code based on multiple schemas. 42 """This class contains methods to generate code based on multiple schemas.
35 """ 43 """
36 44
37 def __init__(self, 45 def __init__(self,
38 root, 46 root,
39 model, 47 model,
40 api_defs, 48 api_defs,
(...skipping 235 matching lines...) Expand 10 before | Expand all | Expand 10 after
276 c.Append() 284 c.Append()
277 c.Append('#include "%s"' % (os.path.join(self._bundle._source_file_dir, 285 c.Append('#include "%s"' % (os.path.join(self._bundle._source_file_dir,
278 'generated_schemas.h'))) 286 'generated_schemas.h')))
279 c.Append() 287 c.Append()
280 c.Append('#include "base/lazy_instance.h"') 288 c.Append('#include "base/lazy_instance.h"')
281 c.Append() 289 c.Append()
282 c.Append('namespace {') 290 c.Append('namespace {')
283 for api in self._bundle._api_defs: 291 for api in self._bundle._api_defs:
284 namespace = self._bundle._model.namespaces[api.get('namespace')] 292 namespace = self._bundle._model.namespaces[api.get('namespace')]
285 # JSON parsing code expects lists of schemas, so dump a singleton list. 293 # JSON parsing code expects lists of schemas, so dump a singleton list.
286 json_content = json.dumps([_RemoveDescriptions(api)], 294 json_content = json.dumps([_RemoveUnneededFields(api)],
287 separators=(',', ':')) 295 separators=(',', ':'))
288 # Escape all double-quotes and backslashes. For this to output a valid 296 # Escape all double-quotes and backslashes. For this to output a valid
289 # JSON C string, we need to escape \ and ". Note that some schemas are 297 # JSON C string, we need to escape \ and ". Note that some schemas are
290 # too large to compile on windows. Split the JSON up into several 298 # too large to compile on windows. Split the JSON up into several
291 # strings, since apparently that helps. 299 # strings, since apparently that helps.
292 max_length = 8192 300 max_length = 8192
293 segments = [json_content[i:i + max_length].replace('\\', '\\\\') 301 segments = [json_content[i:i + max_length].replace('\\', '\\\\')
294 .replace('"', '\\"') 302 .replace('"', '\\"')
295 for i in xrange(0, len(json_content), max_length)] 303 for i in xrange(0, len(json_content), max_length)]
296 c.Append('const char %s[] = "%s";' % 304 c.Append('const char %s[] = "%s";' %
(...skipping 25 matching lines...) Expand all
322 c.Append() 330 c.Append()
323 c.Append('// static') 331 c.Append('// static')
324 c.Sblock('bool %s::IsGenerated(std::string name) {' % 332 c.Sblock('bool %s::IsGenerated(std::string name) {' %
325 self._bundle._GenerateBundleClass('GeneratedSchemas')) 333 self._bundle._GenerateBundleClass('GeneratedSchemas'))
326 c.Append('return g_lazy_instance.Get().schemas.count(name) > 0;') 334 c.Append('return g_lazy_instance.Get().schemas.count(name) > 0;')
327 c.Eblock('}') 335 c.Eblock('}')
328 c.Append() 336 c.Append()
329 c.Concat(cpp_util.CloseNamespace(self._bundle._cpp_namespace)) 337 c.Concat(cpp_util.CloseNamespace(self._bundle._cpp_namespace))
330 c.Append() 338 c.Append()
331 return c 339 return c
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698