Skip to content

CSV Reporter

CSV Report management for ANTA.

ReportCsv

Build a CSV report.

Headers dataclass

Headers(
    device: str = "Device",
    test_name: str = "Test Name",
    test_status: str = "Test Status",
    messages: str = "Message(s)",
    description: str = "Test description",
    categories: str = "Test category",
)

Headers for the CSV report.

convert_to_list classmethod

convert_to_list(result: TestResult) -> list[str]

Convert a TestResult into a list of string for creating file content.

Parameters:

Name Type Description Default
result TestResult

A TestResult to convert into list.

required

Returns:

Type Description
list[str]

TestResult converted into a list.

Source code in anta/reporter/csv_reporter.py
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
@classmethod
def convert_to_list(cls, result: TestResult) -> list[str]:
    """
    Convert a TestResult into a list of string for creating file content.

    Parameters
    ----------
    result
        A TestResult to convert into list.

    Returns
    -------
    list[str]
        TestResult converted into a list.
    """
    message = cls.split_list_to_txt_list(result.messages) if len(result.messages) > 0 else ""
    categories = cls.split_list_to_txt_list(convert_categories(result.categories)) if len(result.categories) > 0 else "None"
    return [
        str(result.name),
        result.test,
        result.result,
        message,
        result.description,
        categories,
    ]

generate classmethod

generate(
    results: ResultManager, csv_filename: pathlib.Path
) -> None

Build CSV flle with tests results.

Parameters:

Name Type Description Default
results ResultManager

A ResultManager instance.

required
csv_filename Path

File path where to save CSV data.

required

Raises:

Type Description
OSError

if any is raised while writing the CSV file.

Source code in anta/reporter/csv_reporter.py
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
@classmethod
def generate(cls, results: ResultManager, csv_filename: pathlib.Path) -> None:
    """Build CSV flle with tests results.

    Parameters
    ----------
    results
        A ResultManager instance.
    csv_filename
        File path where to save CSV data.

    Raises
    ------
    OSError
        if any is raised while writing the CSV file.
    """
    headers = [
        cls.Headers.device,
        cls.Headers.test_name,
        cls.Headers.test_status,
        cls.Headers.messages,
        cls.Headers.description,
        cls.Headers.categories,
    ]

    try:
        with csv_filename.open(mode="w", encoding="utf-8") as csvfile:
            csvwriter = csv.writer(
                csvfile,
                delimiter=",",
            )
            csvwriter.writerow(headers)
            for entry in results.results:
                csvwriter.writerow(cls.convert_to_list(entry))
    except OSError as exc:
        message = f"OSError caught while writing the CSV file '{csv_filename.resolve()}'."
        anta_log_exception(exc, message, logger)
        raise

split_list_to_txt_list classmethod

split_list_to_txt_list(
    usr_list: list[str], delimiter: str = " - "
) -> str

Split list to multi-lines string.

Parameters:

Name Type Description Default
usr_list list[str]

List of string to concatenate.

required
delimiter str

A delimiter to use to start string. Defaults to None.

' - '

Returns:

Type Description
str

Multi-lines string.

Source code in anta/reporter/csv_reporter.py
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
@classmethod
def split_list_to_txt_list(cls, usr_list: list[str], delimiter: str = " - ") -> str:
    """Split list to multi-lines string.

    Parameters
    ----------
    usr_list
        List of string to concatenate.
    delimiter
        A delimiter to use to start string. Defaults to None.

    Returns
    -------
    str
        Multi-lines string.

    """
    return f"{delimiter}".join(f"{line}" for line in usr_list)