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++ features from json files. | |
6 | |
7 Usage example: | |
8 features_compiler.py --destdir gen --root /home/Work/src _permissions.json | |
9 """ | |
10 | |
11 import optparse | |
12 import os | |
13 import sys | |
14 | |
15 import json_schema | |
16 from schema_loader import SchemaLoader | |
17 from features_cc_generator import CCGenerator | |
18 from features_h_generator import HGenerator | |
19 from model import ComplexFeature, CreateFeature | |
20 | |
21 | |
22 def _GenerateSchema(filename, root, destdir): | |
23 """Generates C++ features files from the json file |filename|. | |
24 """ | |
25 # Load in the feature permissions from the JSON file. | |
26 schema = os.path.normpath(filename) | |
27 schema_loader = SchemaLoader(os.path.dirname(os.path.relpath(schema, root))) | |
28 schema_filename, _ = os.path.splitext(schema) | |
29 feature_defs = schema_loader.LoadSchema(schema) | |
30 | |
31 # Generate a list of the features defined and a list of their models. | |
32 feature_list = [] | |
33 for feature_def, feature in feature_defs.iteritems(): | |
34 feature_list.append(CreateFeature(feature_def, feature)) | |
35 | |
36 source_file_dir, source_file_filename = os.path.split(schema) | |
37 relpath = os.path.relpath(os.path.normpath(source_file_dir), root) | |
38 full_path = os.path.join(relpath, schema) | |
39 | |
40 generators = [ | |
41 ('%s.cc' % schema_filename, CCGenerator()), | |
42 ('%s.h' % schema_filename, HGenerator()) | |
43 ] | |
44 | |
45 # Generate and output the code for all features. | |
46 output_code = [] | |
47 for filename, generator in generators: | |
48 code = generator.Generate(feature_list, full_path).Render() | |
49 if destdir: | |
50 with open(os.path.join(destdir, relpath, filename), 'w') as f: | |
51 f.write(code) | |
52 output_code += [filename, '', code, ''] | |
53 | |
54 return '\n'.join(output_code) | |
55 | |
56 | |
57 if __name__ == '__main__': | |
58 parser = optparse.OptionParser( | |
59 description='Generates a C++ features model from JSON schema', | |
60 usage='usage: %prog [option]... schema') | |
61 parser.add_option('-r', '--root', default='.', | |
62 help='logical include root directory. Path to schema files from ' | |
63 'specified dir will be the include path.') | |
64 parser.add_option('-d', '--destdir', | |
65 help='root directory to output generated files.') | |
not at google - send to devlin
2013/09/17 18:26:19
this also need a C++ namespace option similar to h
dhnishi (use Chromium)
2013/09/18 18:32:45
Done.
| |
66 (opts, filenames) = parser.parse_args() | |
67 | |
68 # Only one file is currently specified. | |
69 if len(filenames) != 1: | |
70 raise ValueError('One (and only one) file is required (for now).') | |
71 | |
72 result = _GenerateSchema(filenames[0], opts.root, opts.destdir) | |
73 if not opts.destdir: | |
74 print result | |
OLD | NEW |