aboutsummaryrefslogtreecommitdiffstats
path: root/library/python/testing/custom_linter_util/reporter.py
blob: 8ae559bca0b9b1749473b90c604cc9bc032523c2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import json
import sys
from enum import Enum
from typing import Optional


class LintStatus(Enum):
    GOOD = "GOOD"
    FAIL = "FAIL"
    SKIPPED = "SKIPPED"


class LintReport():
    def __init__(self):
        self._report = {}

    def add(self, file_name: str, status: LintStatus, message: str = "", elapsed: float = 0.0):
        self._report[file_name] = {
            "status": status.value,
            "message": message,
            "elapsed": elapsed,
        }

    def dump(self, report_file, pretty: Optional[bool] = None):
        data = {
            "report": self._report,
        }
        if report_file == "-":
            if pretty is None:
                pretty = True
            self._do_dump(sys.stdout, data, pretty)
        else:
            with open(report_file, "w") as f:
                self._do_dump(f, data, pretty)

    @staticmethod
    def _do_dump(dest, data, pretty):
        indent = 4 if pretty else None
        json.dump(data, dest, indent=indent)