OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 import json | 6 import json |
7 import os.path | 7 import os.path |
8 import sys | 8 import sys |
9 import optparse | 9 import optparse |
10 _script_path = os.path.realpath(__file__) | 10 _script_path = os.path.realpath(__file__) |
(...skipping 16 matching lines...) Expand all Loading... |
27 with open(filename, 'r') as handle: | 27 with open(filename, 'r') as handle: |
28 result = json.loads(json_comment_eater.Nom(handle.read())) | 28 result = json.loads(json_comment_eater.Nom(handle.read())) |
29 return result | 29 return result |
30 | 30 |
31 def _LoadFieldTrialConfig(filename): | 31 def _LoadFieldTrialConfig(filename): |
32 """Loads a field trial config JSON and converts it into a format that can be | 32 """Loads a field trial config JSON and converts it into a format that can be |
33 used by json_to_struct. | 33 used by json_to_struct. |
34 """ | 34 """ |
35 return _FieldTrialConfigToDescription(_Load(filename)) | 35 return _FieldTrialConfigToDescription(_Load(filename)) |
36 | 36 |
| 37 def _CreateGroup(group_data): |
| 38 group = {'name': group_data['group_name']} |
| 39 params_data = group_data.get('params') |
| 40 if (params_data): |
| 41 group['params'] = [{'key': param, 'value': params_data[param]} |
| 42 for param in sorted(params_data.keys())]; |
| 43 enable_features_data = group_data.get('enable_features') |
| 44 if enable_features_data: |
| 45 group['enable_features'] = enable_features_data |
| 46 disable_features_data = group_data.get('disable_features') |
| 47 if disable_features_data: |
| 48 group['disable_features'] = disable_features_data |
| 49 return group |
| 50 |
| 51 def _CreateTrial(trial_name, groups): |
| 52 return { |
| 53 'name': trial_name, |
| 54 'groups': [_CreateGroup(group) for group in groups], |
| 55 } |
| 56 |
37 def _FieldTrialConfigToDescription(config): | 57 def _FieldTrialConfigToDescription(config): |
38 element = {'groups': []} | 58 return { |
39 for study in sorted(config.keys()): | 59 'elements': { |
40 group_data = config[study][0] | 60 'kFieldTrialConfig': { |
41 group = { | 61 'trials': [_CreateTrial(trial_name, config[trial_name]) |
42 'study': study, | 62 for trial_name in sorted(config.keys())] |
43 'group_name': group_data['group_name'] | 63 } |
44 } | 64 } |
45 params_data = group_data.get('params') | 65 } |
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 enable_features_data = group_data.get('enable_features') | |
52 if enable_features_data: | |
53 group['enable_features'] = enable_features_data | |
54 disable_features_data = group_data.get('disable_features') | |
55 if disable_features_data: | |
56 group['disable_features'] = disable_features_data | |
57 element['groups'].append(group) | |
58 return {'elements': {'kFieldTrialConfig': element}} | |
59 | 66 |
60 def main(arguments): | 67 def main(arguments): |
61 parser = optparse.OptionParser( | 68 parser = optparse.OptionParser( |
62 description='Generates an C++ array of struct from a JSON description.', | 69 description='Generates a struct from a JSON description.', |
63 usage='usage: %prog [option] -s schema description') | 70 usage='usage: %prog [option] -s schema description') |
64 parser.add_option('-b', '--destbase', | 71 parser.add_option('-b', '--destbase', |
65 help='base directory of generated files.') | 72 help='base directory of generated files.') |
66 parser.add_option('-d', '--destdir', | 73 parser.add_option('-d', '--destdir', |
67 help='directory to output generated files, relative to destbase.') | 74 help='directory to output generated files, relative to destbase.') |
68 parser.add_option('-n', '--namespace', | 75 parser.add_option('-n', '--namespace', |
69 help='C++ namespace for generated files. e.g search_providers.') | 76 help='C++ namespace for generated files. e.g search_providers.') |
70 parser.add_option('-s', '--schema', help='path to the schema file, ' | 77 parser.add_option('-s', '--schema', help='path to the schema file, ' |
71 'mandatory.') | 78 'mandatory.') |
72 parser.add_option('-o', '--output', help='output filename, ' | 79 parser.add_option('-o', '--output', help='output filename, ' |
(...skipping 19 matching lines...) Expand all Loading... |
92 | 99 |
93 schema = _Load(opts.schema) | 100 schema = _Load(opts.schema) |
94 description = _LoadFieldTrialConfig(description_filename) | 101 description = _LoadFieldTrialConfig(description_filename) |
95 json_to_struct.GenerateStruct( | 102 json_to_struct.GenerateStruct( |
96 basepath, output_root, opts.namespace, schema, description, | 103 basepath, output_root, opts.namespace, schema, description, |
97 os.path.split(description_filename)[1], os.path.split(opts.schema)[1], | 104 os.path.split(description_filename)[1], os.path.split(opts.schema)[1], |
98 opts.year) | 105 opts.year) |
99 | 106 |
100 if __name__ == '__main__': | 107 if __name__ == '__main__': |
101 main(sys.argv[1:]) | 108 main(sys.argv[1:]) |
OLD | NEW |