OLD | NEW |
---|---|
(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', | |
Yoyo Zhou
2012/01/23 23:16:42
nit: descriptions should end with period.
calamity
2012/01/24 22:57:22
Done.
| |
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 | |
74 out_file = cpp_util.cppName(namespace.name).lower() + filename_suffix | |
75 if not namespace: | |
76 continue | |
Yoyo Zhou
2012/01/23 23:16:42
This looks like it should be above the out_file =
calamity
2012/01/24 22:57:22
Done.
| |
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, api_model, | |
83 type_generator) | |
84 cc_code = cc_generator.generate().render() | |
85 h_generator = h_generator.HGenerator(namespace, api_model, | |
86 type_generator) | |
87 h_code = h_generator.generate().render() | |
88 | |
89 if dest_dir: | |
90 with open(os.path.join(dest_dir, namespace.source_file_dir, | |
91 out_file + '.cc'), 'w') as cc_file: | |
92 cc_file.write(cc_code) | |
93 with open(os.path.join(dest_dir, namespace.source_file_dir, | |
94 out_file + '.h'), 'w') as h_file: | |
95 h_file.write(h_code) | |
96 else: | |
97 print '%s.h' % out_file | |
98 print | |
99 print h_code | |
100 print | |
101 print '%s.cc\n' % out_file | |
Yoyo Zhou
2012/01/23 23:16:42
nit: extra \n
calamity
2012/01/24 22:57:22
Done.
| |
102 print | |
103 print cc_code | |
OLD | NEW |