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 from cpp_generator import CppGenerator | 19 from cpp_generator import CppGenerator |
20 from cpp_type_generator import CppTypeGenerator | 20 from cpp_type_generator import CppTypeGenerator |
21 from dart_generator import DartGenerator | 21 from dart_generator import DartGenerator |
22 from cpp_bundle_generator import CppBundleGenerator | 22 from cpp_bundle_generator import CppBundleGenerator |
23 from model import Model | 23 from model import Model |
not at google - send to devlin
2013/02/16 02:50:02
from model import Mode, UnixName
SanjoyPal
2013/02/19 19:22:02
Done.
| |
24 from model import UnixName | |
24 import idl_schema | 25 import idl_schema |
25 import json_schema | 26 import json_schema |
26 | 27 |
27 import optparse | 28 import optparse |
28 import os.path | 29 import os.path |
29 import sys | 30 import sys |
30 | 31 |
31 # Names of supported code generators, as specified on the command-line. | 32 # Names of supported code generators, as specified on the command-line. |
32 # First is default. | 33 # First is default. |
33 GENERATORS = ['cpp', 'cpp-bundle', 'dart'] | 34 GENERATORS = ['cpp', 'cpp-bundle', 'dart'] |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
103 for target_namespace in api_defs: | 104 for target_namespace in api_defs: |
104 for referenced_schema in target_namespace.get('dependencies', []): | 105 for referenced_schema in target_namespace.get('dependencies', []): |
105 split_schema = referenced_schema.split(':', 1) | 106 split_schema = referenced_schema.split(':', 1) |
106 if len(split_schema) > 1: | 107 if len(split_schema) > 1: |
107 if split_schema[0] != 'api': | 108 if split_schema[0] != 'api': |
108 continue | 109 continue |
109 else: | 110 else: |
110 referenced_schema = split_schema[1] | 111 referenced_schema = split_schema[1] |
111 | 112 |
112 referenced_schema_path = os.path.join( | 113 referenced_schema_path = os.path.join( |
113 os.path.dirname(schema), referenced_schema + '.json') | 114 os.path.dirname(schema), UnixName(referenced_schema) + '.json') |
not at google - send to devlin
2013/02/16 02:50:02
cleanup: '%s.json' % UnixName(referenced_schema)
SanjoyPal
2013/02/19 19:22:02
Done.
| |
114 referenced_api_defs = json_schema.Load(referenced_schema_path) | 115 referenced_api_defs = json_schema.Load(referenced_schema_path) |
115 | 116 |
116 for namespace in referenced_api_defs: | 117 for namespace in referenced_api_defs: |
117 api_model.AddNamespace( | 118 api_model.AddNamespace( |
118 namespace, | 119 namespace, |
119 os.path.relpath(referenced_schema_path, opts.root), | 120 os.path.relpath(referenced_schema_path, opts.root), |
120 include_compiler_options=True) | 121 include_compiler_options=True) |
121 | 122 |
122 # For single-schema compilation make sure that the first (i.e. only) schema | 123 # For single-schema compilation make sure that the first (i.e. only) schema |
123 # is the default one. | 124 # is the default one. |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
180 code = generator.Generate(namespace).Render() | 181 code = generator.Generate(namespace).Render() |
181 if opts.destdir: | 182 if opts.destdir: |
182 with open(os.path.join(opts.destdir, namespace.source_file_dir, | 183 with open(os.path.join(opts.destdir, namespace.source_file_dir, |
183 filename), 'w') as f: | 184 filename), 'w') as f: |
184 f.write(code) | 185 f.write(code) |
185 else: | 186 else: |
186 print filename | 187 print filename |
187 print | 188 print |
188 print code | 189 print code |
189 print | 190 print |
OLD | NEW |