Skip to content

Commit

Permalink
separation of workflow utils from the main utils
Browse files Browse the repository at this point in the history
  • Loading branch information
lnxpy committed Jun 9, 2024
1 parent f6cb1a2 commit ccebecf
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 39 deletions.
39 changes: 0 additions & 39 deletions pyaction/utils.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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
1 change: 1 addition & 0 deletions pyaction/workflow/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from pyaction.workflow.wrapper import PyAction # noqa
19 changes: 19 additions & 0 deletions pyaction/workflow/consts.py
Original file line number Diff line number Diff line change
@@ -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",
},
]
58 changes: 58 additions & 0 deletions pyaction/workflow/stream.py
Original file line number Diff line number Diff line change
@@ -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")
File renamed without changes.

0 comments on commit ccebecf

Please sign in to comment.