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

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

Issue 9600050: Add IDL capability to json_schema_compiler. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Patch Set 1 Created 8 years, 9 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 | Annotate | Revision Log
OLDNEW
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 from json_schema import LoadJSON 22 import idl_schema
23 import json_schema
23 import model 24 import model
24 import optparse 25 import optparse
25 import os.path 26 import os.path
26 import sys 27 import sys
27 28
28 if __name__ == '__main__': 29 if __name__ == '__main__':
29 parser = optparse.OptionParser( 30 parser = optparse.OptionParser(
30 description='Generates a C++ model of an API from JSON schema', 31 description='Generates a C++ model of an API from JSON schema',
31 usage='usage: %prog [option]... schema') 32 usage='usage: %prog [option]... schema')
32 parser.add_option('-r', '--root', default='.', 33 parser.add_option('-r', '--root', default='.',
33 help='logical include root directory. Path to schema files from specified' 34 help='logical include root directory. Path to schema files from specified'
34 'dir will be the include path.') 35 'dir will be the include path.')
35 parser.add_option('-d', '--destdir', 36 parser.add_option('-d', '--destdir',
36 help='root directory to output generated files.') 37 help='root directory to output generated files.')
37 parser.add_option('-n', '--namespace', default='generated_api_schemas', 38 parser.add_option('-n', '--namespace', default='generated_api_schemas',
38 help='C++ namespace for generated files. e.g extensions::api.') 39 help='C++ namespace for generated files. e.g extensions::api.')
39 40
40 (opts, args) = parser.parse_args() 41 (opts, args) = parser.parse_args()
41 if not args: 42 if not args:
42 sys.exit(parser.get_usage()) 43 sys.exit(parser.get_usage())
43 dest_dir = opts.destdir 44 dest_dir = opts.destdir
44 root_namespace = opts.namespace 45 root_namespace = opts.namespace
45 46
46 schema = os.path.normpath(args[0]) 47 schema = os.path.normpath(args[0])
47 48
48 api_model = model.Model() 49 api_model = model.Model()
49 50
50
51 # Actually generate for source file. 51 # Actually generate for source file.
52 api_defs = LoadJSON(schema) 52 schema_filename, schema_extension = os.path.splitext(schema)
53 if schema_extension == '.json':
54 api_defs = json_schema.Load(schema)
55 elif schema_extension == '.idl':
56 api_defs = idl_schema.Load(schema)
57 else:
58 sys.exit("Did not recognize file extension %s for schema %s" %
59 (schema_extension, schema))
53 60
54 for target_namespace in api_defs: 61 for target_namespace in api_defs:
55 referenced_schemas = target_namespace.get('dependencies', []) 62 referenced_schemas = target_namespace.get('dependencies', [])
56 # Load type dependencies into the model. 63 # Load type dependencies into the model.
64 # TODO(miket): do we need this in IDL?
57 for referenced_schema in referenced_schemas: 65 for referenced_schema in referenced_schemas:
58 referenced_schema_path = os.path.join( 66 referenced_schema_path = os.path.join(
59 os.path.dirname(schema), referenced_schema + '.json') 67 os.path.dirname(schema), referenced_schema + '.json')
60 referenced_api_defs = LoadJSON(referenced_schema_path) 68 referenced_api_defs = json_schema.Load(referenced_schema_path)
61 69
62 for namespace in referenced_api_defs: 70 for namespace in referenced_api_defs:
63 api_model.AddNamespace(namespace, 71 api_model.AddNamespace(namespace,
64 os.path.relpath(referenced_schema_path, opts.root)) 72 os.path.relpath(referenced_schema_path, opts.root))
65 73
66 # Gets the relative path from opts.root to the schema to correctly determine 74 # Gets the relative path from opts.root to the schema to correctly determine
67 # the include path. 75 # the include path.
68 relpath = os.path.relpath(schema, opts.root) 76 relpath = os.path.relpath(schema, opts.root)
69 namespace = api_model.AddNamespace(target_namespace, relpath) 77 namespace = api_model.AddNamespace(target_namespace, relpath)
70 if not namespace: 78 if not namespace:
(...skipping 24 matching lines...) Expand all
95 'w') as h_file: 103 'w') as h_file:
96 h_file.write(h_code) 104 h_file.write(h_code)
97 else: 105 else:
98 print '%s.h' % out_file 106 print '%s.h' % out_file
99 print 107 print
100 print h_code 108 print h_code
101 print 109 print
102 print '%s.cc' % out_file 110 print '%s.cc' % out_file
103 print 111 print
104 print cc_code 112 print cc_code
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698