Chromium Code Reviews| Index: tools/variations/fieldtrial_to_struct.py |
| diff --git a/tools/variations/fieldtrial_to_struct.py b/tools/variations/fieldtrial_to_struct.py |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..7e134ab49f050fcbd3efccadc6756b055f8eaaf1 |
| --- /dev/null |
| +++ b/tools/variations/fieldtrial_to_struct.py |
| @@ -0,0 +1,89 @@ |
| +#!/usr/bin/env python |
| +# Copyright 2015 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. |
| + |
| +import json |
| +import os.path |
| +import sys |
| +import optparse |
| +_script_path = os.path.realpath(__file__) |
| + |
| +sys.path.insert(0, os.path.normpath(_script_path + "/../../json_comment_eater")) |
| +try: |
| + import json_comment_eater |
| +finally: |
| + sys.path.pop(0) |
| + |
| +sys.path.insert(0, os.path.normpath(_script_path + "/../../json_to_struct")) |
| +try: |
| + import json_to_struct |
| +finally: |
| + sys.path.pop(0) |
| + |
| +def _Load(filename): |
| + """Loads a JSON file into a Python object and return this object. |
| + """ |
| + with open(filename, 'r') as handle: |
| + result = json.loads(json_comment_eater.Nom(handle.read())) |
| + return result |
| + |
| +def _LoadFieldTrialConfig(filename): |
| + """Loads a field trial config JSON and converts it into a format that can be |
| + used by json_to_struct. |
| + """ |
| + element = {'groups': []} |
| + with open(filename, 'r') as handle: |
| + config = json.loads(json_comment_eater.Nom(handle.read())) |
| + for study in config: |
| + group_data = config[study][0] |
| + group = { |
| + 'study': study, |
| + 'group_name': group_data['group_name'] |
| + } |
| + params_data = group_data.get('params') |
| + if (params_data): |
| + params = [] |
| + for param in params_data: |
| + params.append({'key': param, 'value': params_data[param]}) |
| + group['params'] = params |
| + element['groups'].append(group) |
| + return {'elements': {'kFieldTrialConfig': element}} |
| + |
| +if __name__ == '__main__': |
| + parser = optparse.OptionParser( |
| + description='Generates an C++ array of struct from a JSON description.', |
| + usage='usage: %prog [option] -s schema description') |
| + parser.add_option('-b', '--destbase', |
| + help='base directory of generated files.') |
| + parser.add_option('-d', '--destdir', |
| + help='directory to output generated files, relative to destbase.') |
| + parser.add_option('-n', '--namespace', |
| + help='C++ namespace for generated files. e.g search_providers.') |
| + parser.add_option('-s', '--schema', help='path to the schema file, ' |
| + 'mandatory.') |
| + parser.add_option('-o', '--output', help='output filename, ' |
| + 'mandatory.') |
| + (opts, args) = parser.parse_args() |
| + |
| + if not opts.schema: |
| + parser.error('You must specify a --schema.') |
| + |
| + description_filename = os.path.normpath(args[0]) |
| + root, ext = os.path.splitext(description_filename) |
| + shortroot = opts.output |
| + if opts.destdir: |
| + output_root = os.path.join(os.path.normpath(opts.destdir), shortroot) |
| + else: |
| + output_root = shortroot |
| + |
| + if opts.destbase: |
| + basepath = os.path.normpath(opts.destbase) |
| + else: |
| + basepath = '' |
| + |
| + schema = _Load(opts.schema) |
| + description = _LoadFieldTrialConfig(description_filename) |
| + json_to_struct.GenerateStruct( |
|
Alexei Svitkine (slow)
2015/07/08 16:54:54
Can you add some tests for this implementation? I'
danduong
2015/07/09 00:12:54
I added a unittest for the most interesting part o
Alexei Svitkine (slow)
2015/07/09 16:55:32
I think it's worth having a test with an expectati
|
| + basepath, output_root, opts.namespace, schema, description, |
| + description_filename, opts.schema) |