OLD | NEW |
(Empty) | |
| 1 # Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. |
| 4 |
| 5 import collections |
| 6 import csv |
| 7 import json |
| 8 import os |
| 9 import tempfile |
| 10 |
| 11 from telemetry.internal.results import output_formatter |
| 12 from tracing.value import histograms_to_csv |
| 13 |
| 14 |
| 15 def _ReadCsv(text): |
| 16 dicts = [] |
| 17 header = None |
| 18 for row in csv.reader(text.split('\n')): |
| 19 if header is None: |
| 20 header = row |
| 21 elif row: |
| 22 dicts.append(collections.OrderedDict(zip(header, row))) |
| 23 return dicts |
| 24 |
| 25 |
| 26 def _WriteCsv(dicts, fileobj): |
| 27 header = [] |
| 28 for d in dicts: |
| 29 for k in d.iterkeys(): |
| 30 if k not in header: |
| 31 header.append(k) |
| 32 rows = [header] |
| 33 for d in dicts: |
| 34 rows.append([d.get(k, '') for k in header]) |
| 35 csv.writer(fileobj).writerows(rows) |
| 36 |
| 37 |
| 38 class CsvOutputFormatter(output_formatter.OutputFormatter): |
| 39 def __init__(self, output_stream, reset_results=False): |
| 40 super(CsvOutputFormatter, self).__init__(output_stream) |
| 41 self._reset_results = reset_results |
| 42 |
| 43 def Format(self, page_test_results): |
| 44 histograms = page_test_results.AsHistogramDicts() |
| 45 file_descriptor, json_path = tempfile.mkstemp() |
| 46 os.close(file_descriptor) |
| 47 json.dump(histograms, file(json_path, 'w')) |
| 48 vinn_result = histograms_to_csv.HistogramsToCsv(json_path) |
| 49 dicts = _ReadCsv(vinn_result.stdout) |
| 50 |
| 51 self._output_stream.seek(0) |
| 52 if not self._reset_results: |
| 53 dicts += _ReadCsv(self._output_stream.read()) |
| 54 self._output_stream.seek(0) |
| 55 _WriteCsv(dicts, self._output_stream) |
| 56 self._output_stream.truncate() |
OLD | NEW |