from __future__ import annotations import shutil from datetime import datetime, timezone from pathlib import Path class FilesystemAdapter: def list_directory(self, directory: Path, show_hidden: bool) -> tuple[list[dict], list[dict]]: directories: list[dict] = [] files: list[dict] = [] for entry in sorted(directory.iterdir(), key=lambda item: item.name.lower()): if not show_hidden and entry.name.startswith("."): continue stat = entry.stat() modified = datetime.fromtimestamp(stat.st_mtime, tz=timezone.utc).isoformat().replace("+00:00", "Z") if entry.is_dir(): directories.append({"name": entry.name, "modified": modified, "absolute": entry}) elif entry.is_file(): files.append( { "name": entry.name, "size": int(stat.st_size), "modified": modified, "absolute": entry, } ) return directories, files def make_directory(self, path: Path) -> None: path.mkdir(parents=False, exist_ok=False) def rename_path(self, source: Path, destination: Path) -> None: source.rename(destination) def move_file(self, source: str, destination: str) -> None: Path(source).rename(Path(destination)) def is_directory_empty(self, path: Path) -> bool: return not any(path.iterdir()) def delete_file(self, path: Path) -> None: path.unlink() def delete_empty_directory(self, path: Path) -> None: path.rmdir() def copy_file(self, source: str, destination: str, on_progress: callable | None = None) -> None: src = Path(source) dst = Path(destination) with src.open("rb") as in_f, dst.open("xb") as out_f: while True: chunk = in_f.read(1024 * 1024) if not chunk: break out_f.write(chunk) if on_progress: on_progress(out_f.tell()) shutil.copystat(src, dst, follow_symlinks=False) def read_text_preview(self, path: Path, max_bytes: int, encoding: str = "utf-8") -> dict: size = int(path.stat().st_size) limit = max_bytes + 1 with path.open("rb") as in_f: raw = in_f.read(limit) modified = self.modified_iso(path) truncated = size > max_bytes or len(raw) > max_bytes if truncated: raw = raw[:max_bytes] return { "size": size, "modified": modified, "truncated": truncated, "content": raw.decode(encoding, errors="replace"), } def write_text_file(self, path: Path, content: str, encoding: str = "utf-8") -> dict: path.write_text(content, encoding=encoding) return { "size": int(path.stat().st_size), "modified": self.modified_iso(path), } @staticmethod def modified_iso(path: Path) -> str: stat = path.stat() return datetime.fromtimestamp(stat.st_mtime, tz=timezone.utc).isoformat().replace("+00:00", "Z")