OLD | NEW |
---|---|
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. |
11 | 11 |
12 Usage example: | 12 Usage example: |
13 compiler.py --root /home/Work/src --namespace extensions windows.json | 13 compiler.py --root /home/Work/src --namespace extensions windows.json |
14 tabs.json | 14 tabs.json |
15 compiler.py --destdir gen --root /home/Work/src | 15 compiler.py --destdir gen --root /home/Work/src |
16 --namespace extensions windows.json tabs.json | 16 --namespace extensions windows.json tabs.json |
17 """ | 17 """ |
18 | 18 |
19 import cc_generator | 19 import cc_generator |
20 import cpp_type_generator | 20 import cpp_type_generator |
21 import h_generator | 21 import h_generator |
22 import json | 22 import json |
23 import model | 23 import model |
24 import optparse | 24 import optparse |
25 import os.path | 25 import os.path |
26 import sys | 26 import sys |
27 | 27 |
28 # We need to get json_minify from the third_party directory. | |
29 # This is similar to what is done in chrome/common/extensions/docs/build.py | |
30 third_party_path = os.path.dirname(os.path.realpath(__file__)) + \ | |
31 '/../../third_party/' | |
not at google - send to devlin
2012/03/02 02:15:08
Won't this cause problems on Windows? os.path.join
| |
32 if third_party_path not in sys.path: | |
33 sys.path.insert(0, third_party_path) | |
34 import json_minify as minify | |
35 | |
28 if __name__ == '__main__': | 36 if __name__ == '__main__': |
29 parser = optparse.OptionParser( | 37 parser = optparse.OptionParser( |
30 description='Generates a C++ model of an API from JSON schema', | 38 description='Generates a C++ model of an API from JSON schema', |
31 usage='usage: %prog [option]... schema') | 39 usage='usage: %prog [option]... schema') |
32 parser.add_option('-r', '--root', default='.', | 40 parser.add_option('-r', '--root', default='.', |
33 help='logical include root directory. Path to schema files from specified' | 41 help='logical include root directory. Path to schema files from specified' |
34 'dir will be the include path.') | 42 'dir will be the include path.') |
35 parser.add_option('-d', '--destdir', | 43 parser.add_option('-d', '--destdir', |
36 help='root directory to output generated files.') | 44 help='root directory to output generated files.') |
37 parser.add_option('-n', '--namespace', default='generated_api_schemas', | 45 parser.add_option('-n', '--namespace', default='generated_api_schemas', |
38 help='C++ namespace for generated files. e.g extensions::api.') | 46 help='C++ namespace for generated files. e.g extensions::api.') |
39 | 47 |
40 (opts, args) = parser.parse_args() | 48 (opts, args) = parser.parse_args() |
41 if not args: | 49 if not args: |
42 sys.exit(parser.get_usage()) | 50 sys.exit(parser.get_usage()) |
43 dest_dir = opts.destdir | 51 dest_dir = opts.destdir |
44 root_namespace = opts.namespace | 52 root_namespace = opts.namespace |
45 | 53 |
46 schema = os.path.normpath(args[0]) | 54 schema = os.path.normpath(args[0]) |
47 | 55 |
48 api_model = model.Model() | 56 api_model = model.Model() |
49 | 57 |
50 | 58 |
51 # Actually generate for source file. | 59 # Actually generate for source file. |
52 with open(schema, 'r') as schema_file: | 60 with open(schema, 'r') as schema_file: |
53 api_defs = json.loads(schema_file.read()) | 61 api_defs = json.loads(minify.json_minify(schema_file.read())) |
54 | 62 |
55 for target_namespace in api_defs: | 63 for target_namespace in api_defs: |
56 referenced_schemas = target_namespace.get('dependencies', []) | 64 referenced_schemas = target_namespace.get('dependencies', []) |
57 # Load type dependencies into the model. | 65 # Load type dependencies into the model. |
58 for referenced_schema in referenced_schemas: | 66 for referenced_schema in referenced_schemas: |
59 referenced_schema_path = os.path.join( | 67 referenced_schema_path = os.path.join( |
60 os.path.dirname(schema), referenced_schema + '.json') | 68 os.path.dirname(schema), referenced_schema + '.json') |
61 with open(referenced_schema_path, 'r') as referenced_schema_file: | 69 with open(referenced_schema_path, 'r') as referenced_schema_file: |
62 referenced_api_defs = json.loads(referenced_schema_file.read()) | 70 referenced_api_defs = json.loads(minify.json_minify( |
71 referenced_schema_file.read())) | |
63 | 72 |
64 for namespace in referenced_api_defs: | 73 for namespace in referenced_api_defs: |
65 api_model.AddNamespace(namespace, | 74 api_model.AddNamespace(namespace, |
66 os.path.relpath(referenced_schema_path, opts.root)) | 75 os.path.relpath(referenced_schema_path, opts.root)) |
67 | 76 |
68 # Gets the relative path from opts.root to the schema to correctly determine | 77 # Gets the relative path from opts.root to the schema to correctly determine |
69 # the include path. | 78 # the include path. |
70 relpath = os.path.relpath(schema, opts.root) | 79 relpath = os.path.relpath(schema, opts.root) |
71 namespace = api_model.AddNamespace(target_namespace, relpath) | 80 namespace = api_model.AddNamespace(target_namespace, relpath) |
72 if not namespace: | 81 if not namespace: |
(...skipping 24 matching lines...) Expand all Loading... | |
97 'w') as h_file: | 106 'w') as h_file: |
98 h_file.write(h_code) | 107 h_file.write(h_code) |
99 else: | 108 else: |
100 print '%s.h' % out_file | 109 print '%s.h' % out_file |
101 print | 110 print |
102 print h_code | 111 print h_code |
103 print | 112 print |
104 print '%s.cc' % out_file | 113 print '%s.cc' % out_file |
105 print | 114 print |
106 print cc_code | 115 print cc_code |
OLD | NEW |