Skip to content

Commit

Permalink
✨ Added TeeContext
Browse files Browse the repository at this point in the history
  • Loading branch information
s-weigand committed Oct 1, 2022
1 parent e1467ec commit 3b5036f
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
62 changes: 62 additions & 0 deletions glotaran/utils/tee.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
"""Module containing ``tee`` like functionality."""
from __future__ import annotations

import sys
from io import StringIO
from types import TracebackType


class TeeContext:
"""Context manager that allows to work with string written to stdout."""

def __init__(self) -> None:
"""Create new ``StringIO`` buffer and save reference to original ``sys.stdout``."""
self.buffer = StringIO()
self.stdout = sys.stdout

def __enter__(self) -> TeeContext:
"""Replace ``sys.stdout`` on entering the context.
Returns
-------
TeeContext
Instance that can be read from.
"""
sys.stdout = self # type:ignore[assignment]
return self

def __exit__(
self,
exc_type: type[BaseException] | None,
exc: BaseException | None,
exc_tb: TracebackType | None,
) -> bool | None:
"""Restore ``sys.stdout`` on exiting the context."""
sys.stdout = self.stdout
self.buffer.close()
return None

def write(self, data: str) -> None:
"""Write to buffer and original ``sys.stdout``.
Parameters
----------
data: str
String to write to stdout and buffer.
"""
self.buffer.write(data)
self.stdout.write(data)

def read(self) -> str:
"""Return values written to buffer.
Returns
-------
str
Text written to buffer.
"""
return self.buffer.getvalue()

def flush(self) -> None:
"""Flush values in the buffer."""
self.buffer.flush()
23 changes: 23 additions & 0 deletions glotaran/utils/test/test_tee.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
"""Test for ``glotaran.utils.tee``."""
from __future__ import annotations

from typing import TYPE_CHECKING

from glotaran.utils.tee import TeeContext

if TYPE_CHECKING:
from _pytest.capture import CaptureFixture


def test_tee_context(capsys: CaptureFixture):
"""Test that print writes to stdout and is readable in context."""
print_str = "foobar"
expected = f"{print_str}\n"
with TeeContext() as tee:
print(print_str)
result = tee.read()

stdout, _ = capsys.readouterr()

assert stdout == expected
assert result == expected

0 comments on commit 3b5036f

Please sign in to comment.