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 import model | |
Yoyo Zhou
2012/01/19 02:19:40
Imports should be sorted.
calamity
2012/01/23 05:14:45
Done.
| |
19 import os.path | |
20 import json | |
21 import cc_generator | |
22 import h_generator | |
23 import optparse | |
24 import sys | |
25 | |
26 parser = optparse.OptionParser( | |
27 description='Generates a C++ model of an API from JSON schema', | |
28 usage='usage: %prog [option]... schema [referenced_schema]...') | |
29 parser.add_option('-r', '--root', default='.', | |
30 help='logical include root directory. Path to schema files from specified' | |
31 'dir will be the include path.') | |
32 parser.add_option('-d', '--destdir', | |
33 help='root directory to output generated files') | |
34 parser.add_option('-n', '--namespace', default='generated_api_schemas', | |
35 help='C++ namespace for generated files') | |
36 parser.add_option('-s', '--suffix', default='_api', | |
37 help='C++ namespace suffix for generated files') | |
38 | |
39 (opts, args) = parser.parse_args() | |
Yoyo Zhou
2012/01/19 02:19:40
Looks like you want most of this file to be behind
calamity
2012/01/20 01:10:25
Done.
| |
40 if not args: | |
41 sys.exit(parser.get_usage()) | |
42 dest_dir = opts.destdir | |
43 root_namespace = opts.namespace | |
44 filename_suffix = opts.suffix | |
45 | |
46 schema = os.path.normpath(args[0]) | |
47 referenced_schemas = args[1:] | |
48 | |
49 api_model = model.Model() | |
50 | |
51 # Load type dependencies into the model. | |
52 for referenced_schema_path in referenced_schemas: | |
53 referenced_schema_path = os.path.normpath(referenced_schema_path) | |
54 referenced_schema_file = open(referenced_schema_path, 'r') | |
55 referenced_api_defs = json.loads(referenced_schema_file.read()) | |
56 referenced_schema_file.close() | |
57 | |
58 for namespace in referenced_api_defs: | |
59 api_model.add_namespace(namespace, | |
60 os.path.relpath(referenced_schema_path, opts.root), filename_suffix) | |
61 | |
62 # Actually generate for source file. | |
63 schema_file = open(schema, 'r') | |
64 api_defs = json.loads(schema_file.read()) | |
Nico
2012/01/18 17:30:12
with open(schema, 'r') as schema_file:
api_defs
calamity
2012/01/20 01:10:25
Done.
| |
65 schema_file.close() | |
66 for target_namespace in api_defs: | |
67 # Gets the relative path from opts.root to the schema to correctly determine | |
68 # the include path. | |
69 relpath = os.path.relpath(schema, opts.root) | |
70 namespace = api_model.add_namespace(target_namespace, relpath, | |
71 filename_suffix) | |
72 if not namespace: | |
73 continue | |
74 cc_generator = cc_generator.CCGenerator(namespace, api_model, root_namespace) | |
75 cc_code = cc_generator.generate().render() | |
76 h_generator = h_generator.HGenerator(namespace, api_model, root_namespace) | |
77 h_code = h_generator.generate().render() | |
78 if dest_dir: | |
79 cc_file = open(os.path.join(dest_dir, namespace.source_file_dir, | |
80 namespace.target_namespace + '.cc'), 'w') | |
Yoyo Zhou
2012/01/19 02:19:40
Is this going to put experimental APIs into files
calamity
2012/01/20 01:10:25
Hmm. Good point. Refactored this whole thing a bit
| |
81 cc_file.write(cc_code) | |
82 cc_file.close() | |
83 h_file = open(os.path.join(dest_dir, namespace.source_file_dir, | |
84 namespace.target_namespace + '.h'), 'w') | |
85 h_file.write(h_code) | |
86 h_file.close() | |
87 else: | |
88 print '%s.h' % namespace.target_namespace | |
89 print | |
90 print h_code | |
91 print | |
92 print '%s.cc\n' % namespace.target_namespace | |
93 print | |
94 print cc_code | |
OLD | NEW |