OLD | NEW |
---|---|
(Empty) | |
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. | |
5 | |
6 import json | |
7 import os.path | |
8 import sys | |
9 import optparse | |
10 _script_path = os.path.realpath(__file__) | |
11 | |
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) | |
17 | |
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) | |
23 | |
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 | |
30 | |
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 element = {'groups': []} | |
36 with open(filename, 'r') as handle: | |
37 config = json.loads(json_comment_eater.Nom(handle.read())) | |
38 for study in config: | |
39 group_data = config[study][0] | |
40 group = { | |
41 'study': study, | |
42 'group_name': group_data['group_name'] | |
43 } | |
44 params_data = group_data.get('params') | |
45 if (params_data): | |
46 params = [] | |
47 for param in params_data: | |
48 params.append({'key': param, 'value': params_data[param]}) | |
49 group['params'] = params | |
50 element['groups'].append(group) | |
51 return {'elements': {'kFieldTrialConfig': element}} | |
52 | |
53 if __name__ == '__main__': | |
54 parser = optparse.OptionParser( | |
55 description='Generates an C++ array of struct from a JSON description.', | |
56 usage='usage: %prog [option] -s schema description') | |
57 parser.add_option('-b', '--destbase', | |
58 help='base directory of generated files.') | |
59 parser.add_option('-d', '--destdir', | |
60 help='directory to output generated files, relative to destbase.') | |
61 parser.add_option('-n', '--namespace', | |
62 help='C++ namespace for generated files. e.g search_providers.') | |
63 parser.add_option('-s', '--schema', help='path to the schema file, ' | |
64 'mandatory.') | |
65 parser.add_option('-o', '--output', help='output filename, ' | |
66 'mandatory.') | |
67 (opts, args) = parser.parse_args() | |
68 | |
69 if not opts.schema: | |
70 parser.error('You must specify a --schema.') | |
71 | |
72 description_filename = os.path.normpath(args[0]) | |
73 root, ext = os.path.splitext(description_filename) | |
74 shortroot = opts.output | |
75 if opts.destdir: | |
76 output_root = os.path.join(os.path.normpath(opts.destdir), shortroot) | |
77 else: | |
78 output_root = shortroot | |
79 | |
80 if opts.destbase: | |
81 basepath = os.path.normpath(opts.destbase) | |
82 else: | |
83 basepath = '' | |
84 | |
85 schema = _Load(opts.schema) | |
86 description = _LoadFieldTrialConfig(description_filename) | |
87 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
| |
88 basepath, output_root, opts.namespace, schema, description, | |
89 description_filename, opts.schema) | |
OLD | NEW |