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

Side by Side 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 unified diff | Download patch
OLDNEW
(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(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.
23 # Load in the feature permissions from the JSON file.
24 schema_loader = SchemaLoader(os.path.dirname(os.path.relpath(
25 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
26 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
27 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.
28 feature_defs = schema_loader.LoadSchema(schema)
29
30 # Generate a list of the features defined and a list of their models.
31 feature_list = []
32 for feature_def, feature_dict in feature_defs.iteritems():
33 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
34 feature_list.append(Feature(feature_def, feature_dict, schema))
35 else:
36 ## The dict may contain list of features, not a single feature.
37 for single_def in feature_dict:
38 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.
39
40 source_file_dir, source_file_filename = os.path.split(schema)
41 relpath = os.path.relpath(os.path.normpath(source_file_dir), root)
42 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.
43
44 generators = [
45 ('%s.cc' % schema_filename, CCGenerator()),
46 ('%s.h' % schema_filename, HGenerator())
47 ]
48
49 # Generate and output the code for all features.
50 output_code = []
51 for filename, generator in generators:
52 code = generator.Generate(feature_list, full_path).Render()
53 if destdir:
54 with open(os.path.join(destdir, relpath,
55 filename), 'w') as f:
56 f.write(code)
57 output_code += [filename, '', code, '']
58
59 return '\n'.join(output_code)
60
61
62 if __name__ == '__main__':
63 parser = optparse.OptionParser(
64 description='Generates a C++ features model from JSON schema',
65 usage='usage: %prog [option]... schema')
66 parser.add_option('-r', '--root', default='.',
67 help='logical include root directory. Path to schema files from specified'
68 'dir will be the include path.')
69 parser.add_option('-d', '--destdir',
70 help='root directory to output generated files.')
71 (opts, filenames) = parser.parse_args()
72
73 if not filenames:
74 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
75
76 # Only one file is currently specified.
77 if len(filenames) > 1:
78 raise Exception(
79 "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.
80
81 result = GenerateSchema(filenames, opts.root, opts.destdir)
82 if not opts.destdir:
83 print result
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698