Chromium Code Reviews| Index: tools/json_schema_compiler/features_compiler.py |
| diff --git a/tools/json_schema_compiler/features_compiler.py b/tools/json_schema_compiler/features_compiler.py |
| new file mode 100755 |
| index 0000000000000000000000000000000000000000..5afbfbfa28bc531c13d52cf666950bc641d0029a |
| --- /dev/null |
| +++ b/tools/json_schema_compiler/features_compiler.py |
| @@ -0,0 +1,83 @@ |
| +#!/usr/bin/env python |
| +# Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +# Use of this source code is governed by a BSD-style license that can be |
| +# found in the LICENSE file. |
| +"""Generator for C++ permissions from permission json files. |
| + |
| +Usage example: |
| + features_compiler.py --destdir gen --root /home/Work/src _permissions.json |
| +""" |
| + |
| +import optparse |
| +import os |
| +import sys |
| + |
| +import json_schema |
| +from schema_loader import SchemaLoader |
| +from features_cc_generator import CCGenerator |
| +from features_h_generator import HGenerator |
| +from model import Feature |
| + |
| + |
| +def _GenerateSchema(filename, root, destdir): |
| + """Generates C++ feature permissions files from the json file |filename|. |
| + """ |
| + # Load in the feature permissions from the JSON file. |
| + schema = os.path.normpath(filename) |
| + schema_loader = SchemaLoader(os.path.dirname(os.path.relpath(schema, root)), |
| + multiple_schemas=True) |
| + schema_filename, _ = os.path.splitext(schema) |
| + feature_defs = schema_loader.LoadSchema(schema) |
| + |
| + # Generate a list of the features defined and a list of their models. |
| + feature_list = [] |
| + for feature_def, feature in feature_defs.iteritems(): |
| + if isinstance(feature, dict): |
| + 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
|
| + else: |
| + ## 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.
|
| + feature_list += [Feature(feature_def, single_def, schema) |
| + for single_def in feature] |
| + |
| + source_file_dir, source_file_filename = os.path.split(schema) |
| + relpath = os.path.relpath(os.path.normpath(source_file_dir), root) |
| + full_path = os.path.join(relpath, schema) |
| + |
| + generators = [ |
| + ('%s.cc' % schema_filename, CCGenerator()), |
| + ('%s.h' % schema_filename, HGenerator()) |
| + ] |
| + |
| + # Generate and output the code for all features. |
| + output_code = [] |
| + for filename, generator in generators: |
| + code = generator.Generate(feature_list, full_path).Render() |
| + if destdir: |
| + with open(os.path.join(destdir, relpath, filename), 'w') as f: |
| + f.write(code) |
| + output_code += [filename, '', code, ''] |
| + |
| + return '\n'.join(output_code) |
| + |
| + |
| +if __name__ == '__main__': |
| + parser = optparse.OptionParser( |
| + description='Generates a C++ features model from JSON schema', |
| + usage='usage: %prog [option]... schema') |
| + parser.add_option('-r', '--root', default='.', |
| + help='logical include root directory. Path to schema files from ' |
| + 'specified dir will be the include path.') |
| + parser.add_option('-d', '--destdir', |
| + help='root directory to output generated files.') |
| + (opts, filenames) = parser.parse_args() |
| + |
| + if not filenames: |
| + 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
|
| + |
| + # Only one file is currently specified. |
| + if len(filenames) > 1: |
| + 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.
|
| + |
| + result = _GenerateSchema(filenames[0], opts.root, opts.destdir) |
| + if not opts.destdir: |
| + print result |