903e9e7117
The placer performs filesystem I/O (os.link) — it belongs in the application layer, not the domain. Domain services should be pure. - Move alfred/domain/subtitles/services/placer.py to alfred/application/subtitles/placer.py - Move tests/domain/test_subtitle_placer.py to tests/application/test_subtitle_placer.py - Update all callers (manage_subtitles use case, metadata store, tests) - Drop placer re-exports from domain.subtitles.services.__init__
117 lines
3.6 KiB
Python
117 lines
3.6 KiB
Python
"""SubtitlePlacer — hard-links matched subtitle tracks next to the destination video."""
|
|
|
|
import logging
|
|
import os
|
|
from dataclasses import dataclass
|
|
from pathlib import Path
|
|
|
|
from alfred.domain.subtitles.entities import SubtitleCandidate
|
|
from alfred.domain.subtitles.value_objects import SubtitleType
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
def _build_dest_name(track: SubtitleCandidate, video_stem: str) -> str:
|
|
"""
|
|
Build the destination filename for a subtitle track.
|
|
|
|
Format: {video_stem}.{lang}.{ext}
|
|
{video_stem}.{lang}.sdh.{ext}
|
|
{video_stem}.{lang}.forced.{ext}
|
|
"""
|
|
if not track.language or not track.format:
|
|
raise ValueError("Cannot compute destination name: language or format missing")
|
|
|
|
ext = track.format.extensions[0].lstrip(".")
|
|
parts = [video_stem, track.language.code]
|
|
if track.subtitle_type == SubtitleType.SDH:
|
|
parts.append("sdh")
|
|
elif track.subtitle_type == SubtitleType.FORCED:
|
|
parts.append("forced")
|
|
return ".".join(parts) + "." + ext
|
|
|
|
|
|
@dataclass
|
|
class PlacedTrack:
|
|
source: Path
|
|
destination: Path
|
|
filename: str
|
|
|
|
|
|
@dataclass
|
|
class PlaceResult:
|
|
placed: list[PlacedTrack]
|
|
skipped: list[tuple[SubtitleCandidate, str]] # (track, reason)
|
|
|
|
@property
|
|
def placed_count(self) -> int:
|
|
return len(self.placed)
|
|
|
|
@property
|
|
def skipped_count(self) -> int:
|
|
return len(self.skipped)
|
|
|
|
|
|
class SubtitlePlacer:
|
|
"""
|
|
Hard-links matched SubtitleCandidate files next to a destination video.
|
|
|
|
Uses the same hard-link strategy as FileManager.copy_file:
|
|
instant, no data duplication, qBittorrent keeps seeding.
|
|
|
|
Embedded tracks are skipped — nothing to place on disk.
|
|
"""
|
|
|
|
def place(
|
|
self,
|
|
tracks: list[SubtitleCandidate],
|
|
destination_video: Path,
|
|
) -> PlaceResult:
|
|
placed: list[PlacedTrack] = []
|
|
skipped: list[tuple[SubtitleCandidate, str]] = []
|
|
|
|
dest_dir = destination_video.parent
|
|
|
|
for track in tracks:
|
|
if track.is_embedded:
|
|
logger.debug(f"SubtitlePlacer: skip embedded track ({track.language})")
|
|
skipped.append((track, "embedded — no file to place"))
|
|
continue
|
|
|
|
if not track.file_path:
|
|
skipped.append((track, "source file not set"))
|
|
continue
|
|
|
|
try:
|
|
dest_name = _build_dest_name(track, destination_video.stem)
|
|
except ValueError as e:
|
|
skipped.append((track, str(e)))
|
|
continue
|
|
|
|
dest_path = dest_dir / dest_name
|
|
|
|
try:
|
|
os.link(track.file_path, dest_path)
|
|
placed.append(
|
|
PlacedTrack(
|
|
source=track.file_path,
|
|
destination=dest_path,
|
|
filename=dest_name,
|
|
)
|
|
)
|
|
logger.info(f"SubtitlePlacer: placed {dest_name}")
|
|
except FileNotFoundError:
|
|
skipped.append((track, "source file not found"))
|
|
except FileExistsError:
|
|
logger.debug(f"SubtitlePlacer: skip {dest_name} — already exists")
|
|
skipped.append((track, "destination already exists"))
|
|
except OSError as e:
|
|
logger.warning(f"SubtitlePlacer: failed to place {dest_name}: {e}")
|
|
skipped.append((track, str(e)))
|
|
|
|
logger.info(
|
|
f"SubtitlePlacer: {len(placed)} placed, {len(skipped)} skipped "
|
|
f"for {destination_video.name}"
|
|
)
|
|
return PlaceResult(placed=placed, skipped=skipped)
|