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

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

Issue 232183002: Chromium side changes to add the ledger API. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: merge Created 6 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
« no previous file with comments | « extensions/common/permissions/api_permission.h ('k') | tools/metrics/histograms/histograms.xml » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 22 matching lines...) Expand all
33 # First is default. 33 # First is default.
34 GENERATORS = ['cpp', 'cpp-bundle', 'dart', 'ppapi'] 34 GENERATORS = ['cpp', 'cpp-bundle', 'dart', 'ppapi']
35 35
36 def GenerateSchema(generator, 36 def GenerateSchema(generator,
37 filenames, 37 filenames,
38 root, 38 root,
39 destdir, 39 destdir,
40 root_namespace, 40 root_namespace,
41 dart_overrides_dir, 41 dart_overrides_dir,
42 impl_dir): 42 impl_dir):
43 schema_loader = SchemaLoader(
44 os.path.dirname(os.path.relpath(os.path.normpath(filenames[0]), root)),
45 os.path.dirname(filenames[0]))
46 # Merge the source files into a single list of schemas. 43 # Merge the source files into a single list of schemas.
47 api_defs = [] 44 api_defs = []
48 for filename in filenames: 45 for filename in filenames:
49 schema = os.path.normpath(filename) 46 schema = os.path.normpath(filename)
47 schema_loader = SchemaLoader(
48 os.path.dirname(os.path.relpath(os.path.normpath(filename), root)),
49 os.path.dirname(filename))
50 api_def = schema_loader.LoadSchema(os.path.split(schema)[1]) 50 api_def = schema_loader.LoadSchema(os.path.split(schema)[1])
51 51
52 # If compiling the C++ model code, delete 'nocompile' nodes. 52 # If compiling the C++ model code, delete 'nocompile' nodes.
53 if generator == 'cpp': 53 if generator == 'cpp':
54 api_def = json_schema.DeleteNodes(api_def, 'nocompile') 54 api_def = json_schema.DeleteNodes(api_def, 'nocompile')
55 api_defs.extend(api_def) 55 api_defs.extend(api_def)
56 56
57 api_model = Model() 57 api_model = Model()
58 58
59 # For single-schema compilation make sure that the first (i.e. only) schema 59 # For single-schema compilation make sure that the first (i.e. only) schema
60 # is the default one. 60 # is the default one.
61 default_namespace = None 61 default_namespace = None
62 62
63 # If we have files from multiple source paths, we'll use the common parent
64 # path as the source directory.
65 src_path = None
66
63 # Load the actual namespaces into the model. 67 # Load the actual namespaces into the model.
64 for target_namespace, schema_filename in zip(api_defs, filenames): 68 for target_namespace, schema_filename in zip(api_defs, filenames):
65 relpath = os.path.relpath(os.path.normpath(schema_filename), root) 69 relpath = os.path.relpath(os.path.normpath(schema_filename), root)
66 namespace = api_model.AddNamespace(target_namespace, 70 namespace = api_model.AddNamespace(target_namespace,
67 relpath, 71 relpath,
68 include_compiler_options=True) 72 include_compiler_options=True)
73
69 if default_namespace is None: 74 if default_namespace is None:
70 default_namespace = namespace 75 default_namespace = namespace
71 76
77 if src_path is None:
78 src_path = namespace.source_file_dir
79 else:
80 src_path = os.path.commonprefix((src_path, namespace.source_file_dir))
81
72 path, filename = os.path.split(schema_filename) 82 path, filename = os.path.split(schema_filename)
73 short_filename, extension = os.path.splitext(filename) 83 short_filename, extension = os.path.splitext(filename)
74 84
75 # Construct the type generator with all the namespaces in this model. 85 # Construct the type generator with all the namespaces in this model.
76 type_generator = CppTypeGenerator(api_model, 86 type_generator = CppTypeGenerator(api_model,
77 schema_loader, 87 schema_loader,
78 default_namespace=default_namespace) 88 default_namespace=default_namespace)
79
80 if generator == 'cpp-bundle': 89 if generator == 'cpp-bundle':
81 cpp_bundle_generator = CppBundleGenerator(root, 90 cpp_bundle_generator = CppBundleGenerator(root,
82 api_model, 91 api_model,
83 api_defs, 92 api_defs,
84 type_generator, 93 type_generator,
85 root_namespace, 94 root_namespace,
86 namespace.source_file_dir, 95 src_path,
87 impl_dir) 96 impl_dir)
88 generators = [ 97 generators = [
89 ('generated_api.cc', cpp_bundle_generator.api_cc_generator), 98 ('generated_api.cc', cpp_bundle_generator.api_cc_generator),
90 ('generated_api.h', cpp_bundle_generator.api_h_generator), 99 ('generated_api.h', cpp_bundle_generator.api_h_generator),
91 ('generated_schemas.cc', cpp_bundle_generator.schemas_cc_generator), 100 ('generated_schemas.cc', cpp_bundle_generator.schemas_cc_generator),
92 ('generated_schemas.h', cpp_bundle_generator.schemas_h_generator) 101 ('generated_schemas.h', cpp_bundle_generator.schemas_h_generator)
93 ] 102 ]
94 elif generator == 'cpp': 103 elif generator == 'cpp':
95 cpp_generator = CppGenerator(type_generator, root_namespace) 104 cpp_generator = CppGenerator(type_generator, root_namespace)
96 generators = [ 105 generators = [
(...skipping 11 matching lines...) Expand all
108 (os.path.join('api', 'ppb_%s.idl' % namespace.unix_name), 117 (os.path.join('api', 'ppb_%s.idl' % namespace.unix_name),
109 generator.idl_generator), 118 generator.idl_generator),
110 ] 119 ]
111 else: 120 else:
112 raise Exception('Unrecognised generator %s' % generator) 121 raise Exception('Unrecognised generator %s' % generator)
113 122
114 output_code = [] 123 output_code = []
115 for filename, generator in generators: 124 for filename, generator in generators:
116 code = generator.Generate(namespace).Render() 125 code = generator.Generate(namespace).Render()
117 if destdir: 126 if destdir:
118 with open(os.path.join(destdir, namespace.source_file_dir, 127 output_dir = os.path.join(destdir, src_path)
119 filename), 'w') as f: 128 if not os.path.exists(output_dir):
129 os.makedirs(output_dir)
130 with open(os.path.join(output_dir, filename), 'w') as f:
120 f.write(code) 131 f.write(code)
121 output_code += [filename, '', code, ''] 132 output_code += [filename, '', code, '']
122 133
123 return '\n'.join(output_code) 134 return '\n'.join(output_code)
124 135
125 136
126 if __name__ == '__main__': 137 if __name__ == '__main__':
127 parser = optparse.OptionParser( 138 parser = optparse.OptionParser(
128 description='Generates a C++ model of an API from JSON schema', 139 description='Generates a C++ model of an API from JSON schema',
129 usage='usage: %prog [option]... schema') 140 usage='usage: %prog [option]... schema')
(...skipping 22 matching lines...) Expand all
152 if opts.generator != 'cpp-bundle' and len(filenames) > 1: 163 if opts.generator != 'cpp-bundle' and len(filenames) > 1:
153 # TODO(sashab): Could also just use filenames[0] here and not complain. 164 # TODO(sashab): Could also just use filenames[0] here and not complain.
154 raise Exception( 165 raise Exception(
155 "Unless in bundle mode, only one file can be specified at a time.") 166 "Unless in bundle mode, only one file can be specified at a time.")
156 167
157 result = GenerateSchema(opts.generator, filenames, opts.root, opts.destdir, 168 result = GenerateSchema(opts.generator, filenames, opts.root, opts.destdir,
158 opts.namespace, opts.dart_overrides_dir, 169 opts.namespace, opts.dart_overrides_dir,
159 opts.impl_dir) 170 opts.impl_dir)
160 if not opts.destdir: 171 if not opts.destdir:
161 print result 172 print result
OLDNEW
« no previous file with comments | « extensions/common/permissions/api_permission.h ('k') | tools/metrics/histograms/histograms.xml » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698