Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
AAriam committed Sep 21, 2024
1 parent e368c0a commit 9fbd564
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 40 deletions.
6 changes: 3 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ namespaces = true

# ----------------------------------------- Project Metadata -------------------------------------
[project]
version = "0.0.0.dev14"
version = "0.0.0.dev15"
name = "ActionMan"
requires-python = ">=3.10"
dependencies = [
"PyProtocol",
"rich",
"ProtocolMan == 0.0.0.dev2",
"rich == 13.8.1",
]
15 changes: 4 additions & 11 deletions src/actionman/__init__.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,15 @@
"""ActionMan
Use workflow commands for GitHub Actions from Python.
"""

"""ActionMan: GitHub Actions utilities for Python."""

import io as _io
import sys as _sys

from actionman import exception, log, env_var, step_output, step_summary
from actionman import log, env_var, step_output, step_summary


def in_gha() -> bool:
"""Check if the current script is running in a GitHub Actions environment.
"""Check whether the current program is running in a GitHub Actions environment.
Returns
-------
bool
Whether the current script is running in a GitHub Actions environment.
This is done by checking the presence of the 'GITHUB_ACTIONS' environment variable.
References
----------
Expand Down
32 changes: 14 additions & 18 deletions src/actionman/_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,19 @@
)


def output_variable(name: str, value: dict | list | tuple | str | bool | int | float | None) -> str:
"""Format a variable name and value to a string
that could be written as an environment variable (to 'GITHUB_ENV')
or output parameter (to 'GITHUB_OUTPUT') in a job step of a GitHub Actions workflow.
def output_variable(key: str, value: dict | list | tuple | str | bool | int | float | None) -> str:
"""Format a key-value pair for output.
This converts a variable name and its corresponding value to a string
that could be written as an environment variable (to `GITHUB_ENV`)
or output parameter (to `GITHUB_OUTPUT`) in a job step of a GitHub Actions workflow.
Parameters
----------
name : str
The name of the variable.
value : dict | list | tuple | str | bool | int | float | None
The value of the variable.
Returns
-------
str
The formatted variable name and value
as a string ready to be added to 'GITHUB_ENV' or 'GITHUB_OUTPUT'.
key
Name of the variable.
value
Value of the variable.
Raises
------
Expand All @@ -43,19 +39,19 @@ def output_variable(name: str, value: dict | list | tuple | str | bool | int | f
with open("/dev/urandom", "rb") as f:
random_bytes = f.read(15)
random_delimeter = _base64.b64encode(random_bytes).decode("utf-8")
return f"{name}<<{random_delimeter}\n{value}\n{random_delimeter}"
return f"{key}<<{random_delimeter}\n{value}\n{random_delimeter}"
elif isinstance(value, (dict, list, tuple, bool, int, float, _NoneType)):
try:
value = _json.dumps(value)
except Exception as e:
raise _ActionManOutputVariableSerializationError(
var_name=name,
var_name=key,
var_value=value,
exception=e,
) from e
else:
raise _ActionManOutputVariableTypeError(
var_name=name,
var_name=key,
var_value=value,
)
return f"{name}={value}"
return f"{key}={value}"
2 changes: 1 addition & 1 deletion src/actionman/env_var.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def write(name: str, value: dict | list | tuple | str | bool | int | float | Non
"""
if not _FILEPATH:
raise _ActionManGitHubError(missing_env_var=_ENV_VAR_NAME)
output = _format.output_variable(name=name, value=value)
output = _format.output_variable(key=name, value=value)
with open(_FILEPATH, "a") as f:
print(output, file=f)
return output
2 changes: 1 addition & 1 deletion src/actionman/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from typing import Literal
from rich.console import RenderableType
from rich.text import TextType
from pyprotocol import Stringable
from protocolman import Stringable


DEFAULT_CONSOLE = Console(force_terminal=True, emoji_variant="emoji")
Expand Down
3 changes: 1 addition & 2 deletions src/actionman/step_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
- [GitHub Docs: Workflow Commands for GitHub Actions: Setting an output parameter](https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#setting-an-output-parameter)
"""


import os as _os
from pathlib import Path as _Path

Expand Down Expand Up @@ -48,7 +47,7 @@ def write(name: str, value: dict | list | tuple | str | bool | int | float | Non
"""
if not _FILEPATH:
raise _ActionManGitHubError(missing_env_var=_ENV_VAR_NAME)
output = _format.output_variable(name=name, value=value)
output = _format.output_variable(key=name, value=value)
with open(_FILEPATH, "a") as f:
print(output, file=f)
return output
11 changes: 7 additions & 4 deletions src/actionman/step_summary.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@
- [GitHub Docs: Workflow Commands for GitHub Actions: Adding a job summary](https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#adding-a-job-summary)
"""


from __future__ import annotations as _annotations
from typing import TYPE_CHECKING as _TYPE_CHECKING
import os as _os
from pathlib import Path as _Path

from pyprotocol import Stringable as _Stringable
from actionman.exception import ActionManGitHubError as _ActionManGitHubError

if _TYPE_CHECKING:
from protocolman import Stringable


_ENV_VAR_NAME = "GITHUB_STEP_SUMMARY"
_FILEPATH: _Path | None = _Path(_os.environ[_ENV_VAR_NAME]) if _ENV_VAR_NAME in _os.environ else None
Expand Down Expand Up @@ -43,7 +46,7 @@ def read() -> str | None:
return _FILEPATH.read_text() if _FILEPATH.is_file() else None


def append(content: _Stringable) -> None:
def append(content: Stringable) -> None:
"""Append the given content to the step summary file.
Raises
Expand All @@ -58,7 +61,7 @@ def append(content: _Stringable) -> None:
return


def write(content: _Stringable) -> None:
def write(content: Stringable) -> None:
"""Overwrite the step summary file with the given content.
Raises
Expand Down

0 comments on commit 9fbd564

Please sign in to comment.