From d0be7babfc1a4282499fc125c7036bce1d3e6843 Mon Sep 17 00:00:00 2001 From: Philipp Temminghoff Date: Mon, 20 Jan 2025 08:02:05 +0100 Subject: [PATCH] chore: allow passing callables directly as outputwriter --- src/slashed/store.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/slashed/store.py b/src/slashed/store.py index 998f9c8..c23222f 100644 --- a/src/slashed/store.py +++ b/src/slashed/store.py @@ -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: @@ -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. @@ -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 {} @@ -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.