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

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

Issue 2266113002: [Extensions] Prefix extension apis in the generation step (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: testfix 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 | « extensions/common/extension_api.cc ('k') | 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 collections
11 import copy 12 import copy
12 import json 13 import json
13 import os 14 import os
14 import re 15 import re
15 16
16 17
17 def _RemoveKey(node, key, type_restriction): 18 def _RemoveKey(node, key, type_restriction):
18 if isinstance(node, dict): 19 if isinstance(node, dict):
19 if key in node and isinstance(node[key], type_restriction): 20 if key in node and isinstance(node[key], type_restriction):
20 del node[key] 21 del node[key]
21 for value in node.values(): 22 for value in node.values():
22 _RemoveKey(value, key, type_restriction) 23 _RemoveKey(value, key, type_restriction)
23 elif isinstance(node, list): 24 elif isinstance(node, list):
24 for value in node: 25 for value in node:
25 _RemoveKey(value, key, type_restriction) 26 _RemoveKey(value, key, type_restriction)
26 27
27 def _RemoveUnneededFields(schema): 28 def _RemoveUnneededFields(schema):
28 """Returns a copy of |schema| with fields that aren't necessary at runtime 29 """Returns a copy of |schema| with fields that aren't necessary at runtime
29 removed. 30 removed.
30 """ 31 """
31 # Return a copy so that we don't pollute the global api object, which may be 32 # Return a copy so that we don't pollute the global api object, which may be
32 # used elsewhere. 33 # used elsewhere.
33 ret = copy.deepcopy(schema) 34 ret = copy.deepcopy(schema)
34 _RemoveKey(ret, "description", basestring) 35 _RemoveKey(ret, "description", basestring)
35 _RemoveKey(ret, "compiler_options", dict) 36 _RemoveKey(ret, "compiler_options", dict)
36 _RemoveKey(ret, "nodoc", bool) 37 _RemoveKey(ret, "nodoc", bool)
37 _RemoveKey(ret, "noinline_doc", bool) 38 _RemoveKey(ret, "noinline_doc", bool)
38 return ret 39 return ret
39 40
41 def _PrefixSchemaWithNamespace(schema):
lazyboy 2016/08/23 17:43:54 Add description, re: types and refs become fully q
Devlin 2016/08/23 22:02:43 Done.
42 assert isinstance(schema, dict), "Schema is unexpected type"
43 namespace = schema['namespace']
44 def prefix(obj, key, force_presence):
lazyboy 2016/08/23 17:43:55 nit: s/force_presence/mandatory
Devlin 2016/08/23 22:02:43 Done.
45 if not key in obj:
46 assert not force_presence, (
47 'Required key "%s" is not present in object.' % key)
48 return
49 if obj[key].find('.') == -1:
lazyboy 2016/08/23 17:43:54 assert obj[key] is string?
Devlin 2016/08/23 22:02:43 Done.
50 obj[key] = '%s.%s' % (namespace, obj[key])
51
52 if 'types' in schema:
53 for t in schema['types']:
lazyboy 2016/08/23 17:43:54 assert that schema['types'] is a list.
Devlin 2016/08/23 22:02:43 Done.
54 assert isinstance(t, dict), "Type entry is unexpected type"
55 prefix(t, 'id', True)
56 prefix(t, 'customBindings', False)
57
58 def prefix_refs(val):
59 if type(val) is list:
60 for sub_val in val:
61 prefix_refs(sub_val)
62 elif type(val) is dict or type(val) is collections.OrderedDict:
63 prefix(val, '$ref', False)
64 for key, sub_val in val.items():
65 prefix_refs(sub_val)
66 prefix_refs(schema)
67 return schema
68
40 69
41 class CppBundleGenerator(object): 70 class CppBundleGenerator(object):
42 """This class contains methods to generate code based on multiple schemas. 71 """This class contains methods to generate code based on multiple schemas.
43 """ 72 """
44 73
45 def __init__(self, 74 def __init__(self,
46 root, 75 root,
47 model, 76 model,
48 api_defs, 77 api_defs,
49 cpp_type_generator, 78 cpp_type_generator,
(...skipping 234 matching lines...) Expand 10 before | Expand all | Expand 10 after
284 c.Append() 313 c.Append()
285 c.Append('#include "%s"' % (os.path.join(self._bundle._source_file_dir, 314 c.Append('#include "%s"' % (os.path.join(self._bundle._source_file_dir,
286 'generated_schemas.h'))) 315 'generated_schemas.h')))
287 c.Append() 316 c.Append()
288 c.Append('#include "base/lazy_instance.h"') 317 c.Append('#include "base/lazy_instance.h"')
289 c.Append() 318 c.Append()
290 c.Append('namespace {') 319 c.Append('namespace {')
291 for api in self._bundle._api_defs: 320 for api in self._bundle._api_defs:
292 namespace = self._bundle._model.namespaces[api.get('namespace')] 321 namespace = self._bundle._model.namespaces[api.get('namespace')]
293 # JSON parsing code expects lists of schemas, so dump a singleton list. 322 # JSON parsing code expects lists of schemas, so dump a singleton list.
294 json_content = json.dumps([_RemoveUnneededFields(api)], 323 json_content = json.dumps([_PrefixSchemaWithNamespace(
324 _RemoveUnneededFields(api))],
295 separators=(',', ':')) 325 separators=(',', ':'))
296 # Escape all double-quotes and backslashes. For this to output a valid 326 # Escape all double-quotes and backslashes. For this to output a valid
297 # JSON C string, we need to escape \ and ". Note that some schemas are 327 # JSON C string, we need to escape \ and ". Note that some schemas are
298 # too large to compile on windows. Split the JSON up into several 328 # too large to compile on windows. Split the JSON up into several
299 # strings, since apparently that helps. 329 # strings, since apparently that helps.
300 max_length = 8192 330 max_length = 8192
301 segments = [json_content[i:i + max_length].replace('\\', '\\\\') 331 segments = [json_content[i:i + max_length].replace('\\', '\\\\')
302 .replace('"', '\\"') 332 .replace('"', '\\"')
303 for i in xrange(0, len(json_content), max_length)] 333 for i in xrange(0, len(json_content), max_length)]
304 c.Append('const char %s[] = "%s";' % 334 c.Append('const char %s[] = "%s";' %
(...skipping 25 matching lines...) Expand all
330 c.Append() 360 c.Append()
331 c.Append('// static') 361 c.Append('// static')
332 c.Sblock('bool %s::IsGenerated(std::string name) {' % 362 c.Sblock('bool %s::IsGenerated(std::string name) {' %
333 self._bundle._GenerateBundleClass('GeneratedSchemas')) 363 self._bundle._GenerateBundleClass('GeneratedSchemas'))
334 c.Append('return g_lazy_instance.Get().schemas.count(name) > 0;') 364 c.Append('return g_lazy_instance.Get().schemas.count(name) > 0;')
335 c.Eblock('}') 365 c.Eblock('}')
336 c.Append() 366 c.Append()
337 c.Concat(cpp_util.CloseNamespace(self._bundle._cpp_namespace)) 367 c.Concat(cpp_util.CloseNamespace(self._bundle._cpp_namespace))
338 c.Append() 368 c.Append()
339 return c 369 return c
OLDNEW
« no previous file with comments | « extensions/common/extension_api.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698