Unverified Commit 3f1511d9 authored by Philipp Sommer's avatar Philipp Sommer
Browse files

add show_methods class attribute for reports

parent f274aea3
Pipeline #35643 failed with stage
......@@ -2,7 +2,16 @@ from __future__ import annotations
import curses
from enum import Enum
from typing import TYPE_CHECKING, Any, Dict, Optional, Type, Union
from typing import (
TYPE_CHECKING,
Any,
Callable,
ClassVar,
Dict,
Optional,
Type,
Union,
)
from deprogressapi.settings import ReportSettings, ShowReportMethod
from pydantic import BaseModel, Field, create_model
......@@ -35,6 +44,12 @@ class BaseReport(BaseModel):
_response_properties: Optional[Dict] = None
_window: Optional[curses.window] = None # type: ignore
show_methods: ClassVar[Dict[ShowReportMethod, str]] = {
ShowReportMethod.none: "show_none",
ShowReportMethod.print_: "show_print",
ShowReportMethod.curses: "show_curses",
}
# settings for this report
settings: ReportSettings = Field(
default_factory=ReportSettings, description="Settings for the report."
......@@ -181,18 +196,13 @@ class BaseReport(BaseModel):
def show(self) -> None:
"""Output the report.
This method, supposed to be implemented by subclasses, reports to the
final user on the command-line.
The default implementation just prints the json string of this report.
This uses the methods defined in the :attr:`show_methods` attribute
based on the specified show method in the :attr:`settings`.
"""
method = self.settings.get_show_method()
if method == ShowReportMethod.print_:
self.show_print()
elif method == ShowReportMethod.curses:
self.show_curses()
elif method == ShowReportMethod.none:
self.show_none()
method_identifier = self.settings.get_show_method()
method_name = self.show_methods[method_identifier]
method: Callable[[], None] = getattr(self, method_name)
return method()
def show_print(self) -> None:
"""Show the report using pythons built-in :func:`print` function."""
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment