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

Unified Diff: tools/json_schema_compiler/features_compiler.py

Issue 23594008: Initial code generation for features. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: . Created 7 years, 3 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 side-by-side diff with in-line comments
Download patch
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..1a0b6f72a125818e150c66baa419c7216e3e09d5
--- /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(filenames, root, destdir):
not at google - send to devlin 2013/09/12 16:20:43 Private, so _GenerateSchema. Add a '''Docstring.
dhnishi (use Chromium) 2013/09/13 17:36:42 Done.
+ # Load in the feature permissions from the JSON file.
+ schema_loader = SchemaLoader(os.path.dirname(os.path.relpath(
+ os.path.normpath(filenames[0]), root)), True)
not at google - send to devlin 2013/09/12 16:20:43 what is True here? make it a named parameter if an
dhnishi (use Chromium) 2013/09/13 17:36:42 The SchemaLoader by default doesn't allow multiple
+ schema = os.path.normpath(filenames[0])
not at google - send to devlin 2013/09/12 16:20:43 pass schema into this function rather than filenam
dhnishi (use Chromium) 2013/09/13 17:36:42 If the path is coming in an oddball way with unnec
not at google - send to devlin 2013/09/13 21:37:51 Yeah - though I mean more like do things actually
+ schema_filename, schema_extension = os.path.splitext(schema)
not at google - send to devlin 2013/09/12 16:20:43 schema_extension isn't used, so substitute _
dhnishi (use Chromium) 2013/09/13 17:36:42 Done.
+ 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_dict in feature_defs.iteritems():
+ if type(feature_dict) is not list:
not at google - send to devlin 2013/09/12 16:20:43 a bit odd to be comparing a type of a variable cal
dhnishi (use Chromium) 2013/09/13 17:36:42 I'm having difficulty thinking of a good name for
+ feature_list.append(Feature(feature_def, feature_dict, schema))
+ else:
+ ## The dict may contain list of features, not a single feature.
+ for single_def in feature_dict:
+ feature_list.append(Feature(feature_def, single_def, schema))
not at google - send to devlin 2013/09/12 16:20:43 here: feature_list += [Feature(feature_def, singl
dhnishi (use Chromium) 2013/09/13 17:36:42 That looks cleaner. Done.
+
+ source_file_dir, source_file_filename = os.path.split(schema)
+ relpath = os.path.relpath(os.path.normpath(source_file_dir), root)
+ full_path = relpath + "/%s" % schema
not at google - send to devlin 2013/09/12 16:20:43 os.path.join(relpath, schema)
dhnishi (use Chromium) 2013/09/13 17:36:42 Done.
+
+ 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:
+ sys.exit(0) # This is OK as a no-op
not at google - send to devlin 2013/09/12 16:20:43 I don't think this make sense here
dhnishi (use Chromium) 2013/09/13 17:36:42 I replaced it with a raise Exception, since it doe
+
+ # 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/12 16:20:43 single line
dhnishi (use Chromium) 2013/09/13 17:36:42 Done.
+
+ result = GenerateSchema(filenames, opts.root, opts.destdir)
+ if not opts.destdir:
+ print result

Powered by Google App Engine
This is Rietveld 408576698