Skip to content

TransformOutputWriter

Base classes

Name Children Inherits
OutputWriter
slashed.base
Interface for command output.

⋔ Inheritance diagram

graph TD
  1032438840["output.TransformOutputWriter"]
  1594020653["base.OutputWriter"]
  1073422405["typing.Protocol"]
  3481597255["typing.Generic"]
  2751680898["builtins.object"]
  1594020653 --> 1032438840
  1073422405 --> 1594020653
  3481597255 --> 1073422405
  2751680898 --> 3481597255

🛈 DocStrings

Bases: OutputWriter

Output writer that transforms messages before printing via another writer.

Unlike CallbackOutputWriter, this doesn't handle the actual output. Instead, it transforms the message and delegates printing to a base writer. Use this for adding prefixes, timestamps, or other message modifications.

Example
async def add_timestamp(msg: str, fmt: str = "%H:%M:%S") -> str:
    from datetime import datetime
    return f"[{datetime.now().strftime(fmt)}] {msg}"

writer = TransformOutputWriter(add_timestamp, base_writer=console_writer)
Source code in src/slashed/output.py
class TransformOutputWriter(OutputWriter):
    """Output writer that transforms messages before printing via another writer.

    Unlike CallbackOutputWriter, this doesn't handle the actual output.
    Instead, it transforms the message and delegates printing to a base writer.
    Use this for adding prefixes, timestamps, or other message modifications.

    Example:
        ```python
        async def add_timestamp(msg: str, fmt: str = "%H:%M:%S") -> str:
            from datetime import datetime
            return f"[{datetime.now().strftime(fmt)}] {msg}"

        writer = TransformOutputWriter(add_timestamp, base_writer=console_writer)
        ```
    """

    def __init__(
        self,
        transform: Callable[..., Awaitable[str]],
        *args: Any,
        base_writer: OutputWriter | None = None,
        **kwargs: Any,
    ) -> None:
        """Initialize with transform function and base writer."""
        self._transform = transform
        self._args = args
        self._kwargs = kwargs
        self._base_writer = base_writer or DefaultOutputWriter()

    async def print(self, message: str) -> None:
        """Transform and write message."""
        transformed = await self._transform(message, *self._args, **self._kwargs)
        await self._base_writer.print(transformed)

__init__

__init__(
    transform: Callable[..., Awaitable[str]],
    *args: Any,
    base_writer: OutputWriter | None = None,
    **kwargs: Any,
) -> None

Initialize with transform function and base writer.

Source code in src/slashed/output.py
def __init__(
    self,
    transform: Callable[..., Awaitable[str]],
    *args: Any,
    base_writer: OutputWriter | None = None,
    **kwargs: Any,
) -> None:
    """Initialize with transform function and base writer."""
    self._transform = transform
    self._args = args
    self._kwargs = kwargs
    self._base_writer = base_writer or DefaultOutputWriter()

print async

print(message: str) -> None

Transform and write message.

Source code in src/slashed/output.py
async def print(self, message: str) -> None:
    """Transform and write message."""
    transformed = await self._transform(message, *self._args, **self._kwargs)
    await self._base_writer.print(transformed)

:fa-brands-github: Show source on GitHub