"""
The IO.reader Module provides Reader Interfaces for the relevant input operations.
Implementations are provided for files as well as std streams.
"""
from abc import ABC, abstractmethod
from typing import TextIO
from pathlib import Path
[Doku]
class IReader(ABC):
__slots__ = "_input"
[Doku]
@abstractmethod
def read(self) -> list[str]:
pass
[Doku]
class StreamReader(IReader):
"""Reader implementation for a text stream object, provided at instantiation"""
def __init__(self, input: TextIO):
self._input = input
[Doku]
def read(self) -> list[str]:
"""Executes the actual read operation, if this is called and nothing is written to stdin, the programm will halt until interrupted by the user or OS."""
return self._input.readlines()
[Doku]
class FileReader(IReader):
"""
Reader implementation for a file-like object, provided at instantiation
"""
def __init__(self, input: Path):
self._input = input
[Doku]
def read(self) -> list[str]:
"""Executes the actual read operation, will fail for file-not existing, etc"""
with self._input.open() as filein:
lines = filein.readlines()
return lines