From ccebecf0b5009772e30526ba296711d01b3e1000 Mon Sep 17 00:00:00 2001 From: Sadra Yahyapour Date: Sun, 9 Jun 2024 09:49:41 +0330 Subject: [PATCH] separation of workflow utils from the main utils --- pyaction/utils.py | 39 ------------- pyaction/workflow/__init__.py | 1 + pyaction/workflow/consts.py | 19 ++++++ pyaction/workflow/stream.py | 58 +++++++++++++++++++ pyaction/{workflow.py => workflow/wrapper.py} | 0 5 files changed, 78 insertions(+), 39 deletions(-) create mode 100644 pyaction/workflow/__init__.py create mode 100644 pyaction/workflow/consts.py create mode 100644 pyaction/workflow/stream.py rename pyaction/{workflow.py => workflow/wrapper.py} (100%) diff --git a/pyaction/utils.py b/pyaction/utils.py index a899716..cc27b68 100644 --- a/pyaction/utils.py +++ b/pyaction/utils.py @@ -1,8 +1,6 @@ import inspect from typing import Callable -from rich.table import Table - from pyaction.exceptions import NotAnnotated from pyaction.logger import setup_logger @@ -31,40 +29,3 @@ def check_parameters(func: Callable) -> None: raise NotAnnotated( f"Parameter `{param_name}` in the action function `{func.__name__}` is not annotated." ) - - -def create_output_table() -> Table: - """returns a `rich.Table` output table - - Returns: - Table: rich table with static columns - """ - - table = Table(show_lines=True) - - table.add_column( - "Variable", - justify="center", - style="blue bold", - vertical="middle", - ) - table.add_column( - "Value", - justify="center", - style="green", - vertical="middle", - ) - table.add_column( - "Type", - justify="center", - style="rgb(249,38,114) italic", - vertical="middle", - ) - table.add_column( - "Workflow Usage", - justify="center", - style="magenta", - vertical="middle", - ) - - return table diff --git a/pyaction/workflow/__init__.py b/pyaction/workflow/__init__.py new file mode 100644 index 0000000..7c94398 --- /dev/null +++ b/pyaction/workflow/__init__.py @@ -0,0 +1 @@ +from pyaction.workflow.wrapper import PyAction # noqa diff --git a/pyaction/workflow/consts.py b/pyaction/workflow/consts.py new file mode 100644 index 0000000..48a274f --- /dev/null +++ b/pyaction/workflow/consts.py @@ -0,0 +1,19 @@ +# Local stream output table fields +LOCAL_TABLE_COLS = [ + { + "header": "Variable", + "style": "blue bold", + }, + { + "header": "Value", + "style": "green", + }, + { + "header": "Type", + "style": "rgb(249,38,114) italic", + }, + { + "header": "Workflow Usage", + "style": "magenta", + }, +] diff --git a/pyaction/workflow/stream.py b/pyaction/workflow/stream.py new file mode 100644 index 0000000..dabaf2e --- /dev/null +++ b/pyaction/workflow/stream.py @@ -0,0 +1,58 @@ +from abc import ABC, abstractmethod +from typing import Any, Dict + +from pyaction.consts import GITHUB_OUTPUT, MULTILINE_OUTPUT +from pyaction.workflow.consts import LOCAL_TABLE_COLS + + +class Stream(ABC): + @abstractmethod + def put(self, context: Dict[str, Any]): + pass + + +class LocalStream(Stream): + def __init__(self) -> None: + """initializer + + Args: + context (Dict[str, Any]): context containing the variables + """ + + from rich.table import Table + + self.table = Table(show_lines=True) + for col in LOCAL_TABLE_COLS: + self.table.add_column(**col, justify="center", vertical="middle") + + def put(self, context: Dict[str, Any]): + """uses `rich` to put the information into the STDOUT""" + + from rich.console import Console + + console = Console() + + for var, val in context.items(): + self.table.add_row( + var, + str(val), + str(type(val)), + f"${{{{ steps.STEP_ID.outputs.{var} }}}}", + ) + + console.print(self.table) + + +class WorkflowStream(Stream): + def __init__(self, stream: str = GITHUB_OUTPUT) -> None: + self.stream = stream + + def put(self, context: Dict[str, Any]): + """writes the `context` into the `GITHUB_OUTPUT` environment variable""" + + with open(self.stream, "+w") as streamline: + for var, val in context.items(): + if "\n" in val: + streamline.write(MULTILINE_OUTPUT.format(variable=var, value=val)) + else: + streamline.write(f"{var}={val}\r\n") diff --git a/pyaction/workflow.py b/pyaction/workflow/wrapper.py similarity index 100% rename from pyaction/workflow.py rename to pyaction/workflow/wrapper.py