"""Implementation of the Executor class."""
import sys
import warnings
from pathlib import Path
from typing import TypedDict, Unpack
from yaml import YAMLError
from ..helpers.click_helpers import recho
from ..helpers.config import read_config
from ..helpers.misc import PROJECT_BUILD_ROOT, file_name_from_current_time
from .data_handling.data_handler_factory import DataHandlerFactory
from .data_handling.data_source_types import DataSourceTypes
from .drawer.graph_drawer_factory import GraphDrawerFactory
from .drawer.line_graph_drawer import LineGraphDrawer
class KwargsTyping(TypedDict):
"""Types of the Executor kwargs argument"""
data_source_type: str | None
no_tmp: bool
class Executor:
"""Class to execute the plot software"""
def __init__(
self,
input_data: list[Path],
plot_config: Path,
data_config: Path | None = None,
output: Path | None = None,
**kwargs: Unpack[KwargsTyping],
) -> None:
"""Initialise the Executor"""
self.input_data = input_data
data_source_type = kwargs.get("data_source_type")
self.data_source_type = Executor._get_data_source_type(
data_source_type, input_data
)
self.data_config = data_config
self.plot_config = plot_config
if (output is None) or (Path(output).is_file()):
output = PROJECT_BUILD_ROOT / "plots" / file_name_from_current_time()
self.output = output
self.no_tmp: bool = kwargs.get("no_tmp", False)
def create_plots(self) -> None:
"""Create plots from the given data."""
try:
handler = DataHandlerFactory().get_object(
self.data_source_type, self.data_config
)
data_files = self._get_data_files()
for file in data_files:
data = handler.get_data(Path(file), self.no_tmp)
plot_dir = Path(self.output) / Path(file).stem
plot_dir.mkdir(parents=True, exist_ok=True)
for graph_config in read_config(self.plot_config):
with warnings.catch_warnings(record=True) as w:
graph_drawer = GraphDrawerFactory().get_object(graph_config)
graph_drawer.draw(data=data)
graph_drawer.save(plot_dir)
graph_drawer.show()
Executor._handle_pyplot_warnings(w, graph_drawer)
except (
UnicodeDecodeError,
YAMLError,
FileNotFoundError,
OSError,
TypeError,
) as err:
recho(f"Plot creation failed: {err}")
sys.exit(1)
def _get_data_files(self) -> list[Path]:
"""Get all files from input_data attribute"""
files = []
for data in self.input_data:
if Path(data).is_file():
files.append(data)
elif Path(data).is_dir():
ext = str(self.data_source_type.name).lower()
files = files + [
i for i in sorted(Path(data).glob(f"*.{ext}")) if i.is_file()
]
else:
recho("Input data has to contain files or directories only.")
sys.exit(1)
return files
@staticmethod
def _get_data_source_type(
data_source_type: str | None, input_data: list[Path]
) -> DataSourceTypes:
"""Determines the data source type depending on the passed input data
and the type string
"""
if data_source_type is None:
num_dirs = len([d for d in input_data if d.is_dir()])
if num_dirs > 0:
recho(
"Data source type is required when a directory is passed as input data."
)
sys.exit(1)
else:
data_source_type = input_data[0].suffix[1:].upper()
try:
return DataSourceTypes[data_source_type]
except KeyError:
recho(f"Data Type {data_source_type} is not valid for input data.")
sys.exit(1)
@staticmethod
def _handle_pyplot_warnings(
warning_handle: list, graph_drawer: LineGraphDrawer
) -> None:
"""This method should handle all warning coming from pyplot"""
if warning_handle:
warning_texts = [str(x.message) for x in warning_handle]
if any("Tight layout not applied" in x for x in warning_texts):
recho(
f"Plot layout of {graph_drawer.name} seems too "
"small to generate the plot correctly. "
"It is recommended to increase the pixel width "
"and height.",
"yellow",
)