Skip to content

Commit

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

# ----------------------------------------- Project Metadata -------------------------------------
[project]
version = "0.0.0.dev13"
version = "0.0.0.dev14"
name = "ActionMan"
requires-python = ">=3.10"
dependencies = [
"PyProtocol",
"rich",
]
15 changes: 15 additions & 0 deletions src/actionman/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,21 @@
from actionman import exception, log, env_var, step_output, step_summary


def in_gha() -> bool:
"""Check if the current script is running in a GitHub Actions environment.
Returns
-------
bool
Whether the current script is running in a GitHub Actions environment.
References
----------
- [GitHub Docs: Default environment variables](https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/store-information-in-variables#default-environment-variables)
"""
return bool(env_var.read("GITHUB_ACTIONS", bool))


if hasattr(_sys.stdout, 'buffer'):
# Wrap the standard output stream to change its encoding to UTF-8,
# which is required for writing unicode characters (e.g., emojis) to the console in Windows.
Expand Down
91 changes: 59 additions & 32 deletions src/actionman/log.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,31 @@
"""Create workflow annotations and logs for a GitHub Actions workflow run."""

from __future__ import annotations as _annotations

from typing import Literal as _Literal
from typing import TYPE_CHECKING as _TYPE_CHECKING

from pyprotocol import Stringable as _Stringable
from rich.console import Console, Group as _Group
from rich import segment as _segment
from rich.text import Text as _Text

if _TYPE_CHECKING:
from typing import Literal
from rich.console import RenderableType
from rich.text import TextType
from pyprotocol import Stringable

def debug(message: _Stringable, print_: bool = True) -> str:

DEFAULT_CONSOLE = Console(force_terminal=True, emoji_variant="emoji")


def debug(*contents: RenderableType, console: Console | None = None, out: bool = True) -> _Group:
"""Create a debug log.
Parameters
----------
message : actionman.protocol.Stringable
The log message.
print_ : bool, default: True
out : bool, default: True
Whether to directly print the debug log.
Returns
Expand All @@ -25,22 +37,30 @@ def debug(message: _Stringable, print_: bool = True) -> str:
----------
- [GitHub Docs: Workflow Commands for GitHub Actions: Setting a debug message](https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#setting-a-debug-message)
"""
output = f"::debug:: {message}"
if print_:
print(output, flush=True)
console = console or DEFAULT_CONSOLE
final_lines: list[_segment.Segments] = []
for content in contents:
for line in console.render_lines(content):
line.insert(0, _segment.Segment("::debug::"))
final_lines.append(_segment.Segments(line))
output = _Group(*final_lines)
if out:
console = console or DEFAULT_CONSOLE
console.print(output)
return output


def annotation(
typ: _Literal["notice", "warning", "error"],
message: _Stringable,
title: _Stringable = "",
filename: _Stringable = "",
typ: Literal["notice", "warning", "error"],
message: TextType,
title: TextType = "",
filename: Stringable = "",
line_start: int = 0,
line_end: int = 0,
column_start: int = 0,
column_end: int = 0,
print_: bool = True,
console: Console | None = None,
out: bool = True,
) -> str:
"""Create a notice, warning, or error annotation.
Expand All @@ -66,7 +86,7 @@ def annotation(
column_end : int, optional
The ending column number in the line specified by the 'line_start' argument,
to associate the message with.
print_ : bool, default: True
out : bool, default: True
Whether to directly print the annotation.
Returns
Expand All @@ -81,7 +101,7 @@ def annotation(
- [GitHub Docs: Workflow Commands for GitHub Actions: Setting an error message](https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#setting-an-error-message)
"""
args = locals()
output = f"::{typ} "
sig = f"::{typ} "
args_added = False
for arg_name, github_arg_name in (
("title", "title"),
Expand All @@ -92,16 +112,18 @@ def annotation(
("column_end", "endColumn"),
):
if args[arg_name]:
output += f"{github_arg_name}={args[arg_name]},"
sig += f"{github_arg_name}={args[arg_name]},"
args_added = True
output = output.removesuffix("," if args_added else " ")
output += f"::{message}"
if print_:
print(output, flush=True)
sig = sig.removesuffix("," if args_added else " ")
output = _Text(sig)
output.append(message)
if out:
console = console or DEFAULT_CONSOLE
console.print(output)
return output


def group(title: _Stringable, details: _Stringable, print_: bool = True) -> str:
def group(*contents: RenderableType, title: TextType | None = None, console: Console | None = None, out: bool = True) -> _Group:
"""Create an expandable log group.
Parameters
Expand All @@ -122,15 +144,16 @@ def group(title: _Stringable, details: _Stringable, print_: bool = True) -> str:
----------
- [GitHub Docs: Workflow Commands for GitHub Actions: Grouping log output](https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#grouping-log-output)
"""
start = group_open(title, print_=False)
end = group_close(print_=False)
output = f"{start}\n{details}\n{end}"
if print_:
print(output, flush=True)
start = group_open(title, out=False)
end = group_close(out=False)
output = _Group(start, *contents, end)
if out:
console = console or DEFAULT_CONSOLE
console.print(output)
return output


def group_open(title: _Stringable, print_: bool = True) -> str:
def group_open(title: TextType | None = None, console: Console | None = None, out: bool = True) -> _Text:
"""Open an expandable log group.
Parameters
Expand All @@ -149,13 +172,16 @@ def group_open(title: _Stringable, print_: bool = True) -> str:
----------
- [GitHub Docs: Workflow Commands for GitHub Actions: Grouping log output](https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#grouping-log-output)
"""
output = f"::group::{title}"
if print_:
print(output, flush=True)
output = _Text("::group::")
if title:
output.append(title)
if out:
console = console or DEFAULT_CONSOLE
console.print(output)
return output


def group_close(print_: bool = True) -> str:
def group_close(console: Console | None = None, out: bool = True) -> str:
"""Close an expandable log group.
Parameters
Expand All @@ -173,6 +199,7 @@ def group_close(print_: bool = True) -> str:
- [GitHub Docs: Workflow Commands for GitHub Actions: Grouping log output](https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#grouping-log-output)
"""
output = "::endgroup::"
if print_:
print(output, flush=True)
if out:
console = console or DEFAULT_CONSOLE
console.print(output)
return output

0 comments on commit e368c0a

Please sign in to comment.