Skip to content

Commit

Permalink
refactor: ReportCsv to dedicated module
Browse files Browse the repository at this point in the history
  • Loading branch information
titom73 committed Jul 5, 2024
1 parent 09b6577 commit bf501e2
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 1 deletion.
3 changes: 2 additions & 1 deletion anta/cli/nrfu/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@

from anta.cli.console import console
from anta.models import AntaTest
from anta.reporter import ReportCsv, ReportJinja, ReportTable
from anta.reporter import ReportJinja, ReportTable
from anta.reporter.csv_reporter import ReportCsv
from anta.runner import main

if TYPE_CHECKING:
Expand Down
90 changes: 90 additions & 0 deletions anta/reporter/csv_reporter.py
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))

0 comments on commit bf501e2

Please sign in to comment.