from abc import ABC, abstractmethod
from ppc_robot_lib.output.types import OutputContext, SheetData
from ppc_robot_lib.tasks import task_context
[docs]
class AbstractOutputAdapter(ABC):
"""
Abstract class for output adapters. Adapters handles writing spreadsheets to external data source, e.g. Google
Sheets.
"""
def __init__(self, output_ctx: OutputContext):
"""
:param output_ctx: Output context. Contains empty dictionary, that can be used to store state, previous
output path (if any) and state form previous run (if any). Adapter should update the ``state``
and ``output_path`` attributes. Both will be stored even if the adapter fails.
"""
self._output_ctx = output_ctx
@property
def output_ctx(self):
return self._output_ctx
[docs]
@abstractmethod
def initialize(self, task_ctx: 'task_context.TaskContextInterface'):
"""
Initializes the output file. This method is called at the beginning of output, before any sheets are written.
:param task_ctx: Task context.
"""
pass
[docs]
@abstractmethod
def write_output(self, task_ctx: 'task_context.TaskContextInterface', sheets: list[SheetData]):
"""
Writes one or more sheets to the output.
:param task_ctx: Task context.
:param sheets: List of preprocessed sheet data.
"""
pass
[docs]
@abstractmethod
def finalize(self, task_ctx: 'task_context.TaskContextInterface', exc: BaseException = None):
"""
Closes the output file and performs any additional required tasks, such as uploading the output etc.
This method is always called at the end of the report, even in case of an exception. If an exception
is raised, it is passed as an argument. Called only if :py:meth:`initialize` was previously called.
:param task_ctx: Task Context.
:param exc: If an exception has occurred after the output was initialized
"""
pass