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.
10 _script_path
= os
.path
.realpath(__file__
)
12 sys
.path
.insert(0, os
.path
.normpath(_script_path
+ "/../../json_comment_eater"))
14 import json_comment_eater
18 sys
.path
.insert(0, os
.path
.normpath(_script_path
+ "/../../json_to_struct"))
25 """Loads a JSON file into a Python object and return this object.
27 with
open(filename
, 'r') as handle
:
28 result
= json
.loads(json_comment_eater
.Nom(handle
.read()))
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.
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]
43 'group_name': group_data
['group_name']
45 params_data
= group_data
.get('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
}}
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, '
66 parser
.add_option('-o', '--output', help='output filename, '
68 parser
.add_option('-y', '--year',
69 help='year to put in the copy-right.')
70 (opts
, args
) = parser
.parse_args(args
=arguments
)
73 parser
.error('You must specify a --schema.')
75 description_filename
= os
.path
.normpath(args
[0])
76 shortroot
= opts
.output
78 output_root
= os
.path
.join(os
.path
.normpath(opts
.destdir
), shortroot
)
80 output_root
= shortroot
83 basepath
= os
.path
.normpath(opts
.destbase
)
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],
94 if __name__
== '__main__':