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

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

Issue 9114036: Code generation for extensions api (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: windows path fix Created 8 years, 11 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
OLDNEW
(Empty)
1 #!/usr/bin/env python
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
4 # found in the LICENSE file.
5 """Generator for C++ structs from api json files.
6
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.
9 Originally written for generating code for extension apis. Reference schemas
10 are in chrome/common/extensions/api.
11
12 Usage example:
13 compiler.py --root /home/Work/src --namespace extensions windows.json
14 tabs.json
15 compiler.py --destdir gen --suffix _api --root /home/Work/src
16 --namespace extensions windows.json tabs.json
17 """
18
19 import cpp_util
20 import cc_generator
21 import cpp_type_generator
22 import h_generator
23 import json
24 import model
25 import optparse
26 import os.path
27 import sys
28
29 if __name__ == '__main__':
30 parser = optparse.OptionParser(
31 description='Generates a C++ model of an API from JSON schema',
32 usage='usage: %prog [option]... schema [referenced_schema]...')
33 parser.add_option('-r', '--root', default='.',
34 help='logical include root directory. Path to schema files from specified'
35 'dir will be the include path.')
36 parser.add_option('-d', '--destdir',
37 help='root directory to output generated files.')
38 parser.add_option('-n', '--namespace', default='generated_api_schemas',
39 help='C++ namespace for generated files. e.g extensions::api.')
40 parser.add_option('-s', '--suffix', default='',
41 help='Filename and C++ namespace suffix for generated files.')
42
43 (opts, args) = parser.parse_args()
44 if not args:
45 sys.exit(parser.get_usage())
46 dest_dir = opts.destdir
47 root_namespace = opts.namespace
48 filename_suffix = opts.suffix
49
50 schema = os.path.normpath(args[0])
51 referenced_schemas = args[1:]
52
53 api_model = model.Model()
54
55 # Load type dependencies into the model.
56 for referenced_schema_path in referenced_schemas:
57 with open(referenced_schema_path, 'r') as referenced_schema_file:
58 referenced_api_defs = json.loads(referenced_schema_file.read())
59
60 for namespace in referenced_api_defs:
61 api_model.AddNamespace(namespace,
62 os.path.relpath(referenced_schema_path, opts.root))
63
64 # Actually generate for source file.
65 with open(schema, 'r') as schema_file:
66 api_defs = json.loads(schema_file.read())
67
68 for target_namespace in api_defs:
69 # Gets the relative path from opts.root to the schema to correctly determine
70 # the include path.
71 relpath = os.path.relpath(schema, opts.root)
72 namespace = api_model.AddNamespace(target_namespace, relpath)
73 if not namespace:
74 continue
75
76 out_file = cpp_util.CppName(namespace.name).lower() + filename_suffix
77 type_generator = cpp_type_generator.CppTypeGenerator(root_namespace,
78 namespace, out_file)
79 for referenced_namespace in api_model.namespaces.values():
80 type_generator.AddNamespace(referenced_namespace,
81 cpp_util.CppName(referenced_namespace.name).lower() + filename_suffix)
82 cc_generator = cc_generator.CCGenerator(namespace, type_generator)
83 cc_code = cc_generator.Generate().Render()
84 h_generator = h_generator.HGenerator(namespace, type_generator)
85 h_code = h_generator.Generate().Render()
86
87 if dest_dir:
88 with open(os.path.join(dest_dir, namespace.source_file_dir,
89 out_file + '.cc'), 'w') as cc_file:
90 cc_file.write(cc_code)
91 with open(os.path.join(dest_dir, namespace.source_file_dir,
92 out_file + '.h'), 'w') as h_file:
93 h_file.write(h_code)
94 else:
95 print '%s.h' % out_file
96 print
97 print h_code
98 print
99 print '%s.cc' % out_file
100 print
101 print cc_code
OLDNEW
« no previous file with comments | « tools/json_schema_compiler/code_test.py ('k') | tools/json_schema_compiler/cpp_type_generator.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698