Skip to content

Commit

Permalink
chore: allow passing callables directly as outputwriter
Browse files Browse the repository at this point in the history
  • Loading branch information
phil65 committed Jan 20, 2025
1 parent 67052e9 commit d0be7ba
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/slashed/store.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@
from slashed.events import CommandExecutedEvent
from slashed.exceptions import CommandError
from slashed.log import get_logger
from slashed.output import DefaultOutputWriter, SignalingOutputWriter
from slashed.output import (
CallbackOutputWriter,
DefaultOutputWriter,
SignalingOutputWriter,
)


try:
Expand Down Expand Up @@ -124,7 +128,7 @@ def get_history(
def create_context[TContextData](
self,
data: TContextData | None,
output_writer: OutputWriter | None = None,
output_writer: OutputWriter | Callable[..., Any] | None = None,
metadata: dict[str, Any] | None = None,
) -> CommandContext[TContextData]:
"""Create a command execution context.
Expand All @@ -137,6 +141,8 @@ def create_context[TContextData](
Returns:
Command execution context
"""
if callable(output_writer):
output_writer = CallbackOutputWriter(output_writer)
base_writer = output_writer or DefaultOutputWriter()
writer = SignalingOutputWriter(self.output, base_writer)
meta = metadata or {}
Expand Down Expand Up @@ -379,7 +385,7 @@ async def execute_command_with_context[T](
self,
command_str: str,
context: T | None = None, # type: ignore[type-var]
output_writer: OutputWriter | None = None,
output_writer: OutputWriter | Callable[..., Any] | None = None,
metadata: dict[str, Any] | None = None,
):
"""Execute a command with a custom context.
Expand Down

0 comments on commit d0be7ba

Please sign in to comment.