Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 json | 11 import json |
| 12 import os | 12 import os |
| 13 import re | 13 import re |
| 14 | 14 |
| 15 | 15 |
| 16 def _RemoveDescriptions(node): | 16 def _RemoveKey(node, key, type_restriction): |
| 17 if isinstance(node, dict): | |
| 18 if key in node and isinstance(node[key], type_restriction): | |
| 19 del node[key] | |
| 20 for value in node.values(): | |
| 21 _RemoveKey(value, key, type_restriction) | |
| 22 elif isinstance(node, list): | |
| 23 for value in node: | |
| 24 _RemoveKey(value, key, type_restriction) | |
| 25 | |
| 26 def _RemoveUnneededFields(node): | |
| 17 """Returns a copy of |schema| with "description" fields removed. | 27 """Returns a copy of |schema| with "description" fields removed. |
|
Devlin
2016/08/20 00:53:50
Whoops - will update this comment.
lazyboy
2016/08/20 01:34:31
Acknowledged.
| |
| 18 """ | 28 """ |
| 19 if isinstance(node, dict): | 29 _RemoveKey(node, "description", basestring) |
| 20 result = {} | 30 _RemoveKey(node, "compiler_options", dict) |
| 21 for key, value in node.items(): | 31 _RemoveKey(node, "nodoc", bool) |
| 22 # Some schemas actually have properties called "description", so only | 32 _RemoveKey(node, "noinline_doc", bool) |
| 23 # remove descriptions that have string values. | |
| 24 if key == 'description' and isinstance(value, basestring): | |
| 25 continue | |
| 26 result[key] = _RemoveDescriptions(value) | |
| 27 return result | |
| 28 if isinstance(node, list): | |
| 29 return [_RemoveDescriptions(v) for v in node] | |
| 30 return node | 33 return node |
| 31 | 34 |
| 32 | 35 |
| 33 class CppBundleGenerator(object): | 36 class CppBundleGenerator(object): |
| 34 """This class contains methods to generate code based on multiple schemas. | 37 """This class contains methods to generate code based on multiple schemas. |
| 35 """ | 38 """ |
| 36 | 39 |
| 37 def __init__(self, | 40 def __init__(self, |
| 38 root, | 41 root, |
| 39 model, | 42 model, |
| (...skipping 236 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 276 c.Append() | 279 c.Append() |
| 277 c.Append('#include "%s"' % (os.path.join(self._bundle._source_file_dir, | 280 c.Append('#include "%s"' % (os.path.join(self._bundle._source_file_dir, |
| 278 'generated_schemas.h'))) | 281 'generated_schemas.h'))) |
| 279 c.Append() | 282 c.Append() |
| 280 c.Append('#include "base/lazy_instance.h"') | 283 c.Append('#include "base/lazy_instance.h"') |
| 281 c.Append() | 284 c.Append() |
| 282 c.Append('namespace {') | 285 c.Append('namespace {') |
| 283 for api in self._bundle._api_defs: | 286 for api in self._bundle._api_defs: |
| 284 namespace = self._bundle._model.namespaces[api.get('namespace')] | 287 namespace = self._bundle._model.namespaces[api.get('namespace')] |
| 285 # JSON parsing code expects lists of schemas, so dump a singleton list. | 288 # JSON parsing code expects lists of schemas, so dump a singleton list. |
| 286 json_content = json.dumps([_RemoveDescriptions(api)], | 289 json_content = json.dumps([_RemoveUnneededFields(api)], |
| 287 separators=(',', ':')) | 290 separators=(',', ':')) |
| 288 # Escape all double-quotes and backslashes. For this to output a valid | 291 # 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 | 292 # 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 | 293 # too large to compile on windows. Split the JSON up into several |
| 291 # strings, since apparently that helps. | 294 # strings, since apparently that helps. |
| 292 max_length = 8192 | 295 max_length = 8192 |
| 293 segments = [json_content[i:i + max_length].replace('\\', '\\\\') | 296 segments = [json_content[i:i + max_length].replace('\\', '\\\\') |
| 294 .replace('"', '\\"') | 297 .replace('"', '\\"') |
| 295 for i in xrange(0, len(json_content), max_length)] | 298 for i in xrange(0, len(json_content), max_length)] |
| 296 c.Append('const char %s[] = "%s";' % | 299 c.Append('const char %s[] = "%s";' % |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 322 c.Append() | 325 c.Append() |
| 323 c.Append('// static') | 326 c.Append('// static') |
| 324 c.Sblock('bool %s::IsGenerated(std::string name) {' % | 327 c.Sblock('bool %s::IsGenerated(std::string name) {' % |
| 325 self._bundle._GenerateBundleClass('GeneratedSchemas')) | 328 self._bundle._GenerateBundleClass('GeneratedSchemas')) |
| 326 c.Append('return g_lazy_instance.Get().schemas.count(name) > 0;') | 329 c.Append('return g_lazy_instance.Get().schemas.count(name) > 0;') |
| 327 c.Eblock('}') | 330 c.Eblock('}') |
| 328 c.Append() | 331 c.Append() |
| 329 c.Concat(cpp_util.CloseNamespace(self._bundle._cpp_namespace)) | 332 c.Concat(cpp_util.CloseNamespace(self._bundle._cpp_namespace)) |
| 330 c.Append() | 333 c.Append() |
| 331 return c | 334 return c |
| OLD | NEW |