1#!/usr/bin/python
2# Copyright (C) 2016 The Android Open Source Project
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#      http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15#
16
17import argparse, json, sys
18
19class MetricsParser(object):
20  """Executor of this utility"""
21
22  def __init__(self):
23    self._parser = argparse.ArgumentParser('Parse CTS Test metrics jsons')
24    self._parser.add_argument('filenames', metavar='filenames', nargs='+',
25                              help='filenames of metrics jsons to be parsed')
26    self._metrics = []
27
28  def _ParseArgs(self):
29    self._args = self._parser.parse_args()
30
31  def _Parse(self, filename):
32    json_file = open(filename)
33    json_data = json.load(json_file)
34    self._metrics.append(json_data)
35    self._PrintJson(filename, json_data)
36
37  def _PrintJson(self, filename, json_data):
38    print "\nFilename: %s" % filename
39    stream_names = json_data.keys()
40    for stream_name in stream_names:
41      metrics_list = json_data.get(stream_name)
42      for metrics in metrics_list:
43        print "\nStream Name: %s" % stream_name
44        for key in metrics.keys():
45          print "Key: %s \t Value: %s" % (key, str(metrics.get(key)))
46
47  def Run(self):
48    self._ParseArgs()
49    try:
50      for filename in self._args.filenames:
51        self._Parse(filename)
52    except (IOError, ValueError) as e:
53      print >> sys.stderr, e
54      raise KeyboardInterrupt
55
56if __name__ == '__main__':
57  MetricsParser().Run()
58
59