-
Notifications
You must be signed in to change notification settings - Fork 1
/
io.py
40 lines (30 loc) · 1.13 KB
/
io.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import re
import sys
from io import TextIOWrapper
from typing import Any, Iterable
class FilteredStream:
def __init__(self, stream: TextIOWrapper, patterns: Iterable | str) -> None:
self.stream = stream
if isinstance(patterns, str):
patterns = [patterns]
self.patterns = [re.compile(p) for p in patterns]
def __getattr__(self, attr_name: str) -> Any:
return getattr(self.stream, attr_name)
def write(self, data: str) -> None:
if any([p.search(data) for p in self.patterns]):
self.stream.write(data + "\n")
def flush(self) -> None:
self.stream.flush()
class filter_stdout:
"""This context manager temporarily hijacks sys.stdout to only allow
printing of lines that correspond to certain patterns.
"""
def __init__(self, patterns: Iterable | str) -> None:
self.stream = FilteredStream(sys.stdout, patterns)
def __enter__(self) -> None:
sys.stdout.flush()
self._old_stdout = sys.stdout
sys.stdout = self.stream
def __exit__(self, *_) -> None:
self.stream.flush()
sys.stdout = self._old_stdout