e6ee700825
Domain services no longer call subprocess or pathlib directly. Introduces two Protocol ports in domain/shared/ports/: MediaProber.list_subtitle_streams(video) -> list[SubtitleStreamInfo] FilesystemScanner.scan_dir / stat / read_text -> list[FileEntry] | ... Concrete adapters live in infrastructure/: FfprobeMediaProber (wraps subprocess + ffprobe + JSON) PathlibFilesystemScanner (wraps pathlib + os reads) SubtitleIdentifier and PatternDetector now take (kb, prober, scanner) at construction time. Their internals work over FileEntry snapshots and SubtitleStreamInfo records — no more ad-hoc Path.is_file/iterdir/stat or embedded subprocess.run loops. _count_entries now takes raw SRT text (returned by scanner.read_text) so SRT-only entry counting stays out of the FS layer. manage_subtitles use case instantiates the two adapters once and injects them into both services. Tests pass real adapters and patch `alfred.infrastructure.probe.ffprobe_prober.subprocess.run` for the ffprobe-failure cases. _classify_single tests build FileEntry via a small helper. Domain is now free of subprocess / direct filesystem reads in the subtitle pipeline. The only remaining I/O hooks are FilePath VO convenience methods (exists/is_file/is_dir) which stay as a deliberate affordance on the value object.
60 lines
1.6 KiB
Python
60 lines
1.6 KiB
Python
"""FilesystemScanner port — abstracts filesystem inspection.
|
|
|
|
The domain never calls ``Path.iterdir``, ``Path.is_file``, ``Path.stat`` or
|
|
``open()`` directly. It asks the scanner for a ``FileEntry`` snapshot and
|
|
reasons from there. One scan = one I/O round-trip; no callbacks back to disk.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from dataclasses import dataclass
|
|
from pathlib import Path
|
|
from typing import Protocol
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class FileEntry:
|
|
"""Frozen snapshot of one filesystem entry, taken at scan time.
|
|
|
|
The entry carries enough metadata for the domain to classify and order
|
|
files without re-querying the OS. ``size_kb`` is ``None`` for directories
|
|
and for files whose size could not be read.
|
|
"""
|
|
|
|
path: Path
|
|
is_file: bool
|
|
is_dir: bool
|
|
size_kb: float | None
|
|
|
|
@property
|
|
def name(self) -> str:
|
|
return self.path.name
|
|
|
|
@property
|
|
def stem(self) -> str:
|
|
return self.path.stem
|
|
|
|
@property
|
|
def suffix(self) -> str:
|
|
return self.path.suffix
|
|
|
|
|
|
class FilesystemScanner(Protocol):
|
|
"""Read-only filesystem inspection."""
|
|
|
|
def scan_dir(self, path: Path) -> list[FileEntry]:
|
|
"""Return sorted entries directly inside ``path``.
|
|
|
|
Returns an empty list when ``path`` is not a directory or is
|
|
unreadable. Adapters must not raise.
|
|
"""
|
|
...
|
|
|
|
def stat(self, path: Path) -> FileEntry | None:
|
|
"""Stat a single path; ``None`` when it doesn't exist or is unreadable."""
|
|
...
|
|
|
|
def read_text(self, path: Path, encoding: str = "utf-8") -> str | None:
|
|
"""Read a text file in one go; ``None`` on any error."""
|
|
...
|