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

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

Issue 12522004: Lazily load extension API schemas (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: take out dependencies Created 7 years, 9 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 #!/usr/bin/env python 1 #!/usr/bin/env python
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be 3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file. 4 # found in the LICENSE file.
5 """Generator for C++ structs from api json files. 5 """Generator for C++ structs from api json files.
6 6
7 The purpose of this tool is to remove the need for hand-written code that 7 The purpose of this tool is to remove the need for hand-written code that
8 converts to and from base::Value types when receiving javascript api calls. 8 converts to and from base::Value types when receiving javascript api calls.
9 Originally written for generating code for extension apis. Reference schemas 9 Originally written for generating code for extension apis. Reference schemas
10 are in chrome/common/extensions/api. 10 are in chrome/common/extensions/api.
(...skipping 30 matching lines...) Expand all
41 api_defs = idl_schema.Load(schema) 41 api_defs = idl_schema.Load(schema)
42 else: 42 else:
43 sys.exit('Did not recognize file extension %s for schema %s' % 43 sys.exit('Did not recognize file extension %s for schema %s' %
44 (schema_extension, schema)) 44 (schema_extension, schema))
45 if len(api_defs) != 1: 45 if len(api_defs) != 1:
46 sys.exit('File %s has multiple schemas. Files are only allowed to contain a' 46 sys.exit('File %s has multiple schemas. Files are only allowed to contain a'
47 ' single schema.' % schema) 47 ' single schema.' % schema)
48 48
49 return api_defs 49 return api_defs
50 50
51 class TypeNamespaceResolver(object):
not at google - send to devlin 2013/03/22 22:10:12 pull into separate file?
cduvall 2013/03/22 22:52:29 Done.
52 '''Resolves a type name into the namespace the type belongs to.
53 '''
54 def __init__(self, api_path):
55 self._api_path = api_path
56
57 def Resolve(self, full_name, default_namespace):
58 name_parts = full_name.rsplit('.', 1)
59 if len(name_parts) == 1:
60 if full_name not in default_namespace.types:
61 return None
not at google - send to devlin 2013/03/22 22:10:12 can this be an assertion?
cduvall 2013/03/22 22:52:29 Still returning None, to be handled in CppTypeGene
62 return default_namespace
63 namespace_name, type_name = name_parts
64 json_name = '%s.json' % namespace_name
65 idl_name = '%s.idl' % namespace_name
66 if os.path.exists(json_name):
67 real_name = json_name
68 elif os.path.exists(idl_name):
69 real_name = idl_name
70 else:
71 return None
not at google - send to devlin 2013/03/22 22:10:12 I think a structure like real_name = None for ext
cduvall 2013/03/22 22:52:29 Done.
72 namespace = Model().AddNamespace(_LoadSchema(real_name)[0],
73 os.path.join(self._api_path, real_name))
74 if type_name not in namespace.types:
75 return None
not at google - send to devlin 2013/03/22 22:10:12 can this be an assertion?
cduvall 2013/03/22 22:52:29 Same as above.
76 return namespace
77
51 def GenerateSchema(generator, 78 def GenerateSchema(generator,
52 filenames, 79 filenames,
53 root, 80 root,
54 destdir, 81 destdir,
55 root_namespace, 82 root_namespace,
56 dart_overrides_dir): 83 dart_overrides_dir):
57 # Merge the source files into a single list of schemas. 84 # Merge the source files into a single list of schemas.
58 api_defs = [] 85 api_defs = []
59 for filename in filenames: 86 for filename in filenames:
60 schema = os.path.normpath(filename) 87 schema = os.path.normpath(filename)
61 schema_filename, schema_extension = os.path.splitext(schema) 88 schema_filename, schema_extension = os.path.splitext(schema)
62 path, short_filename = os.path.split(schema_filename) 89 path, short_filename = os.path.split(schema_filename)
63 api_def = _LoadSchema(schema) 90 api_def = _LoadSchema(schema)
64 91
65 # If compiling the C++ model code, delete 'nocompile' nodes. 92 # If compiling the C++ model code, delete 'nocompile' nodes.
66 if generator == 'cpp': 93 if generator == 'cpp':
67 api_def = json_schema.DeleteNodes(api_def, 'nocompile') 94 api_def = json_schema.DeleteNodes(api_def, 'nocompile')
68 api_defs.extend(api_def) 95 api_defs.extend(api_def)
69 96
70 api_model = Model() 97 api_model = Model()
71 98 type_namespace_resolver = TypeNamespaceResolver(
72 # Load type dependencies into the model. 99 os.path.dirname(os.path.relpath(schema, root)))
73 #
74 # HACK(kalman): bundle mode doesn't work with dependencies, because not all
75 # schemas work in bundle mode.
76 #
77 # TODO(kalman): load dependencies lazily (get rid of the 'dependencies' list)
78 # and this problem will go away.
79 if generator != 'cpp-bundle':
80 for target_namespace in api_defs:
81 for referenced_schema in target_namespace.get('dependencies', []):
82 split_schema = referenced_schema.split(':', 1)
83 if len(split_schema) > 1:
84 if split_schema[0] != 'api':
85 continue
86 else:
87 referenced_schema = split_schema[1]
88
89 referenced_schema_path = os.path.join(
90 os.path.dirname(schema), '%s.json' % UnixName(referenced_schema))
91 referenced_api_defs = json_schema.Load(referenced_schema_path)
92
93 for namespace in referenced_api_defs:
94 api_model.AddNamespace(
95 namespace,
96 os.path.relpath(referenced_schema_path, root),
97 include_compiler_options=True)
98 100
99 # For single-schema compilation make sure that the first (i.e. only) schema 101 # For single-schema compilation make sure that the first (i.e. only) schema
100 # is the default one. 102 # is the default one.
101 default_namespace = None 103 default_namespace = None
102 104
103 # Load the actual namespaces into the model. 105 # Load the actual namespaces into the model.
104 for target_namespace, schema_filename in zip(api_defs, filenames): 106 for target_namespace, schema_filename in zip(api_defs, filenames):
105 relpath = os.path.relpath(os.path.normpath(schema_filename), root) 107 relpath = os.path.relpath(os.path.normpath(schema_filename), root)
106 namespace = api_model.AddNamespace(target_namespace, 108 namespace = api_model.AddNamespace(target_namespace,
107 relpath, 109 relpath,
(...skipping 10 matching lines...) Expand all
118 if namespace.unix_name != short_filename: 120 if namespace.unix_name != short_filename:
119 sys.exit("Filename %s is illegal. Name files using unix_hacker style." % 121 sys.exit("Filename %s is illegal. Name files using unix_hacker style." %
120 schema_filename) 122 schema_filename)
121 123
122 # The output filename must match the input filename for gyp to deal with it 124 # The output filename must match the input filename for gyp to deal with it
123 # properly. 125 # properly.
124 out_file = namespace.unix_name 126 out_file = namespace.unix_name
125 127
126 # Construct the type generator with all the namespaces in this model. 128 # Construct the type generator with all the namespaces in this model.
127 type_generator = CppTypeGenerator(api_model, 129 type_generator = CppTypeGenerator(api_model,
130 type_namespace_resolver,
128 default_namespace=default_namespace) 131 default_namespace=default_namespace)
129 132
130 if generator == 'cpp-bundle': 133 if generator == 'cpp-bundle':
131 cpp_bundle_generator = CppBundleGenerator(root, 134 cpp_bundle_generator = CppBundleGenerator(root,
132 api_model, 135 api_model,
133 api_defs, 136 api_defs,
134 type_generator, 137 type_generator,
135 root_namespace) 138 root_namespace)
136 generators = [ 139 generators = [
137 ('generated_api.cc', cpp_bundle_generator.api_cc_generator), 140 ('generated_api.cc', cpp_bundle_generator.api_cc_generator),
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
190 # Unless in bundle mode, only one file should be specified. 193 # Unless in bundle mode, only one file should be specified.
191 if opts.generator != 'cpp-bundle' and len(filenames) > 1: 194 if opts.generator != 'cpp-bundle' and len(filenames) > 1:
192 # TODO(sashab): Could also just use filenames[0] here and not complain. 195 # TODO(sashab): Could also just use filenames[0] here and not complain.
193 raise Exception( 196 raise Exception(
194 "Unless in bundle mode, only one file can be specified at a time.") 197 "Unless in bundle mode, only one file can be specified at a time.")
195 198
196 result = GenerateSchema(opts.generator, filenames, opts.root, opts.destdir, 199 result = GenerateSchema(opts.generator, filenames, opts.root, opts.destdir,
197 opts.namespace, opts.dart_overrides_dir) 200 opts.namespace, opts.dart_overrides_dir)
198 if not opts.destdir: 201 if not opts.destdir:
199 print result 202 print result
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698