arch-design-agent-skill-das.../backend/app/shared/infrastructure/filesystem.py

34 lines
956 B
Python

from pathlib import Path
from app.shared.kernel.exceptions import FileSystemError
def read_text(path: Path) -> str:
try:
return path.read_text(encoding="utf-8")
except OSError as e:
raise FileSystemError(str(path), str(e)) from e
def write_text(path: Path, content: str) -> None:
try:
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text(content, encoding="utf-8")
except OSError as e:
raise FileSystemError(str(path), str(e)) from e
def list_files(directory: Path, extensions: list[str] | None = None) -> list[Path]:
if not directory.is_dir():
raise FileSystemError(str(directory), "Not a directory")
files: list[Path] = []
for p in sorted(directory.rglob("*")):
if p.is_file():
if extensions is None or p.suffix in extensions:
files.append(p)
return files
def file_exists(path: Path) -> bool:
return path.is_file()