from abc import ABC, abstractmethod
from pathlib import Path
[Doku]
class IWriter(ABC):
"""Abstraktion für Ausgabe Operationen"""
[Doku]
@abstractmethod
def write(self, content: list[str]):
pass
[Doku]
@abstractmethod
def writecheck(self) -> bool:
pass
[Doku]
class StreamWriter(IWriter):
"""Writes directly to stdout, may be piped or redirected in most shells."""
[Doku]
def write(self, content: list[str]):
for line in content:
print(line)
[Doku]
def writecheck(self) -> bool:
return True
[Doku]
class FileWriter(IWriter):
"""Writes to a specified file, relative and absolute paths are possible."""
def __init__(self, file: Path):
self._file = file
[Doku]
def write(self, content: list[str]):
"""Performs the actual filesystem operation, will throw any access error or similar"""
with self._file.open("w") as fileout:
fileout.writelines([f"{line}\n" for line in content])
[Doku]
def writecheck(self) -> bool:
with self._file.open("w") as f:
return f.writable()