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++ permissions from permission 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 Feature | |
20 | |
21 | |
22 def _GenerateSchema(filename, root, destdir): | |
23 """Generates C++ feature permissions 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 multiple_schemas=True) | |
29 schema_filename, _ = os.path.splitext(schema) | |
30 feature_defs = schema_loader.LoadSchema(schema) | |
31 | |
32 # Generate a list of the features defined and a list of their models. | |
33 feature_list = [] | |
34 for feature_def, feature in feature_defs.iteritems(): | |
35 if isinstance(feature, dict): | |
36 feature_list.append(Feature(feature_def, feature, schema)) | |
not at google - send to devlin
2013/09/13 21:37:52
yeah - let Feature figure this out, since a list i
dhnishi (use Chromium)
2013/09/16 22:26:21
The model now handles it.
Not sure if this is pre
| |
37 else: | |
38 ## The dict may contain list of features, not a single feature. | |
not at google - send to devlin
2013/09/13 21:37:52
# not ##
dhnishi (use Chromium)
2013/09/16 22:26:21
Done.
| |
39 feature_list += [Feature(feature_def, single_def, schema) | |
40 for single_def in feature] | |
41 | |
42 source_file_dir, source_file_filename = os.path.split(schema) | |
43 relpath = os.path.relpath(os.path.normpath(source_file_dir), root) | |
44 full_path = os.path.join(relpath, schema) | |
45 | |
46 generators = [ | |
47 ('%s.cc' % schema_filename, CCGenerator()), | |
48 ('%s.h' % schema_filename, HGenerator()) | |
49 ] | |
50 | |
51 # Generate and output the code for all features. | |
52 output_code = [] | |
53 for filename, generator in generators: | |
54 code = generator.Generate(feature_list, full_path).Render() | |
55 if destdir: | |
56 with open(os.path.join(destdir, relpath, filename), 'w') as f: | |
57 f.write(code) | |
58 output_code += [filename, '', code, ''] | |
59 | |
60 return '\n'.join(output_code) | |
61 | |
62 | |
63 if __name__ == '__main__': | |
64 parser = optparse.OptionParser( | |
65 description='Generates a C++ features model from JSON schema', | |
66 usage='usage: %prog [option]... schema') | |
67 parser.add_option('-r', '--root', default='.', | |
68 help='logical include root directory. Path to schema files from ' | |
69 'specified dir will be the include path.') | |
70 parser.add_option('-d', '--destdir', | |
71 help='root directory to output generated files.') | |
72 (opts, filenames) = parser.parse_args() | |
73 | |
74 if not filenames: | |
75 raise Exception("No files were specified for the generator.") | |
not at google - send to devlin
2013/09/13 21:37:52
ValueError
dhnishi (use Chromium)
2013/09/16 22:26:21
Merged the two error checks as per the other comme
| |
76 | |
77 # Only one file is currently specified. | |
78 if len(filenames) > 1: | |
79 raise Exception("Only one file is allowed (for now).") | |
not at google - send to devlin
2013/09/13 21:37:52
ValueError.
though that said you can combine both
dhnishi (use Chromium)
2013/09/16 22:26:21
Done.
| |
80 | |
81 result = _GenerateSchema(filenames[0], opts.root, opts.destdir) | |
82 if not opts.destdir: | |
83 print result | |
OLD | NEW |