[Eraser strings] Remove unused Supervised User infobar and corresponding strings
[chromium-blink-merge.git] / tools / variations / fieldtrial_to_struct.py
blobb19dc554938570aa9e3aaac013a189cbf09d9e43
1 #!/usr/bin/env python
2 # Copyright 2015 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.
6 import json
7 import os.path
8 import sys
9 import optparse
10 _script_path = os.path.realpath(__file__)
12 sys.path.insert(0, os.path.normpath(_script_path + "/../../json_comment_eater"))
13 try:
14 import json_comment_eater
15 finally:
16 sys.path.pop(0)
18 sys.path.insert(0, os.path.normpath(_script_path + "/../../json_to_struct"))
19 try:
20 import json_to_struct
21 finally:
22 sys.path.pop(0)
24 def _Load(filename):
25 """Loads a JSON file into a Python object and return this object.
26 """
27 with open(filename, 'r') as handle:
28 result = json.loads(json_comment_eater.Nom(handle.read()))
29 return result
31 def _LoadFieldTrialConfig(filename):
32 """Loads a field trial config JSON and converts it into a format that can be
33 used by json_to_struct.
34 """
35 return _FieldTrialConfigToDescription(_Load(filename))
37 def _FieldTrialConfigToDescription(config):
38 element = {'groups': []}
39 for study in sorted(config.keys()):
40 group_data = config[study][0]
41 group = {
42 'study': study,
43 'group_name': group_data['group_name']
45 params_data = group_data.get('params')
46 if (params_data):
47 params = []
48 for param in sorted(params_data.keys()):
49 params.append({'key': param, 'value': params_data[param]})
50 group['params'] = params
51 element['groups'].append(group)
52 return {'elements': {'kFieldTrialConfig': element}}
54 def main(arguments):
55 parser = optparse.OptionParser(
56 description='Generates an C++ array of struct from a JSON description.',
57 usage='usage: %prog [option] -s schema description')
58 parser.add_option('-b', '--destbase',
59 help='base directory of generated files.')
60 parser.add_option('-d', '--destdir',
61 help='directory to output generated files, relative to destbase.')
62 parser.add_option('-n', '--namespace',
63 help='C++ namespace for generated files. e.g search_providers.')
64 parser.add_option('-s', '--schema', help='path to the schema file, '
65 'mandatory.')
66 parser.add_option('-o', '--output', help='output filename, '
67 'mandatory.')
68 parser.add_option('-y', '--year',
69 help='year to put in the copy-right.')
70 (opts, args) = parser.parse_args(args=arguments)
72 if not opts.schema:
73 parser.error('You must specify a --schema.')
75 description_filename = os.path.normpath(args[0])
76 shortroot = opts.output
77 if opts.destdir:
78 output_root = os.path.join(os.path.normpath(opts.destdir), shortroot)
79 else:
80 output_root = shortroot
82 if opts.destbase:
83 basepath = os.path.normpath(opts.destbase)
84 else:
85 basepath = ''
87 schema = _Load(opts.schema)
88 description = _LoadFieldTrialConfig(description_filename)
89 json_to_struct.GenerateStruct(
90 basepath, output_root, opts.namespace, schema, description,
91 os.path.split(description_filename)[1], os.path.split(opts.schema)[1],
92 opts.year)
94 if __name__ == '__main__':
95 main(sys.argv[1:])