-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: ReportCsv to dedicated module
- Loading branch information
Showing
2 changed files
with
92 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
# Copyright (c) 2023-2024 Arista Networks, Inc. | ||
# Use of this source code is governed by the Apache License 2.0 | ||
# that can be found in the LICENSE file. | ||
"""CSV Report management for ANTA.""" | ||
|
||
# pylint: disable = too-few-public-methods | ||
from __future__ import annotations | ||
|
||
import csv | ||
import logging | ||
import pathlib | ||
from typing import TYPE_CHECKING | ||
|
||
from pydantic import BaseModel | ||
|
||
if TYPE_CHECKING: | ||
from anta.result_manager import ResultManager | ||
from anta.result_manager.models import TestResult | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class ReportCsv: | ||
"""Build a CSV report.""" | ||
|
||
class Headers(BaseModel): | ||
"""Headers for the CSV report.""" | ||
|
||
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" | ||
|
||
def _split_list_to_txt_list(self, usr_list: list[str], delimiter: str | None = None) -> str: | ||
"""Split list to multi-lines string. | ||
Args: | ||
---- | ||
usr_list (list[str]): List of string to concatenate | ||
delimiter (str, optional): A delimiter to use to start string. Defaults to None. | ||
Returns | ||
------- | ||
str: Multi-lines string | ||
""" | ||
if delimiter is not None: | ||
return "\n".join(f"{delimiter} {line}" for line in usr_list) | ||
return "\n".join(f"{line}" for line in usr_list) | ||
|
||
def csv_report(self, results: ResultManager, csv_filename: pathlib.Path) -> None: | ||
"""Build CSV flle with tests results. | ||
Args: | ||
---- | ||
results: A ResultManager instance. | ||
csv_filename: File path where to save CSV data. | ||
""" | ||
|
||
def add_line(result: TestResult) -> list[str]: | ||
message = self._split_list_to_txt_list(result.messages) if len(result.messages) > 0 else "" | ||
categories = ", ".join(result.categories) | ||
return [ | ||
str(result.name), | ||
result.test, | ||
result.result, | ||
message.replace("\n", "\r\n"), | ||
result.description, | ||
categories, | ||
] | ||
|
||
headers = [ | ||
self.Headers().device, | ||
self.Headers().test_name, | ||
self.Headers().test_status, | ||
self.Headers().messages, | ||
self.Headers().description, | ||
self.Headers().categories, | ||
] | ||
|
||
with pathlib.Path.open(csv_filename, "w", encoding="utf-8") as csvfile: | ||
spamwriter = csv.writer( | ||
csvfile, | ||
delimiter=",", | ||
) | ||
spamwriter.writerow(headers) | ||
for entry in results.results: | ||
spamwriter.writerow(add_line(entry)) |