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

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

Issue 9114036: Code generation for extensions api (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: more rework Created 8 years, 11 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
OLDNEW
(Empty)
1 # Copyright (c) 2012 The Chromium Authors. All rights reserved.
not at google - send to devlin 2012/01/17 05:42:32 #!/usr/bin/env python
calamity 2012/01/18 05:43:08 Done.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
4
not at google - send to devlin 2012/01/17 05:42:32 We should probably add a general of comment of wha
calamity 2012/01/18 05:43:08 Done.
5 import model
6 import os.path
7 import json
8 import cc_generator
9 import h_generator
10 import optparse
11 import sys
12
13 parser = optparse.OptionParser(
14 description='Generate C++ boilerplate code for extension apis from json',
not at google - send to devlin 2012/01/17 05:42:32 "Generates a C++ model of a JSON schema" or someth
calamity 2012/01/18 05:43:08 Done.
15 usage='usage: %prog [options] target referenced_jsons')
not at google - send to devlin 2012/01/17 05:42:32 I think a more unixy way of saying something like
calamity 2012/01/18 05:43:08 Done.
16 parser.add_option('-r', '--root', default='.',
17 help='logical include root directory. Path to json files from specified'
18 'dir will be the include path.')
19 parser.add_option('-d', '--destdir',
20 help='root directory to output generated files')
21 parser.add_option('-n', '--namespace',
22 help='C++ namespace for generated files')
23 parser.add_option('-s', '--suffix', default='_api',
24 help='C++ namespace suffix for generated files')
25
26 (opts, args) = parser.parse_args()
27 if not args:
28 sys.exit('Error: No input json file')
not at google - send to devlin 2012/01/17 05:42:32 Print usage instead of just "Error.." ?
calamity 2012/01/18 05:43:08 Done.
29 dest_dir = opts.destdir
30 root_namespace = opts.namespace
31 filename_suffix = opts.suffix
32
33 target_json = os.path.normpath(args[0])
not at google - send to devlin 2012/01/17 05:42:32 it's not actually target, that implies output. th
34 referenced_jsons = args[1:]
35
36 json_model = model.Model()
37
38 # Load type dependencies into the model
not at google - send to devlin 2012/01/17 05:42:32 fullstop
calamity 2012/01/18 05:43:08 Done.
39 for json_path in referenced_jsons:
40 json_path = os.path.normpath(json_path)
41 api_file = open(json_path, 'r')
42 api_defs = json.loads(api_file.read())
43 api_file.close()
44
45 for namespace in api_defs:
46 json_model.add_namespace(namespace, root_namespace,
47 os.path.relpath(json_path, opts.root), filename_suffix)
48
49 # Actually generate for target file.
50 target_api_file = open(target_json, 'r')
51 target_api_defs = json.loads(target_api_file.read())
52 target_api_file.close()
53 for target_namespace in target_api_defs:
54 namespace = json_model.add_namespace(target_namespace, root_namespace,
55 # Gets the relative path from opts.root to the target_json to correctly
56 # determine the include path
57 os.path.relpath(target_json, opts.root), filename_suffix)
not at google - send to devlin 2012/01/17 05:42:32 pull this comment and the os.path.relpath(...) thi
calamity 2012/01/18 05:43:08 Include path is the #include path? The relative pa
calamity 2012/01/18 05:43:08 Well, it's the output path here, but the #include
not at google - send to devlin 2012/01/18 06:57:28 Cool, this can be 1 line now? Also, comment above
58 if not namespace:
59 continue
60 cc_generator = cc_generator.CCGenerator(namespace, json_model)
61 cc_code = cc_generator.generate().render()
62 h_generator = h_generator.HGenerator(namespace, json_model)
63 h_code = h_generator.generate().render()
64 if dest_dir:
65 cc_file = open(os.path.join(dest_dir, namespace.parent_dir,
66 namespace.filename + '.cc'), 'w')
67 cc_file.write(cc_code)
68 cc_file.close()
69 h_file = open(os.path.join(dest_dir, namespace.parent_dir,
70 namespace.filename + '.h'), 'w')
71 h_file.write(h_code)
72 h_file.close()
73 else:
74 print '%s.h' % namespace.filename
75 print
76 print h_code
77 print
78 print '%s.cc\n' % namespace.filename
79 print
80 print cc_code
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698