from pathlib import Path from typing import Iterable, Optional, Type from .entry import Entry __all__ = ( "EntryDetector", "EntryResolver", ) class EntryDetector: def __init__(self, *entry_types: Type[Entry]): self._entry_types = entry_types def detect(self, basepath: Path) -> Optional[Entry]: for entry_type in self._entry_types: entry = entry_type.detect(basepath) if entry: return entry class EntryResolver: def __init__(self, detector: EntryDetector): self._detector = detector def resolve(self, basepath: Path, depth: int = 2) -> Iterable[Entry]: if basepath.is_dir(): entry = self._detector.detect(basepath) if entry: yield entry if depth > 0 and basepath.is_dir(): for path in basepath.iterdir(): yield from self.resolve(path, depth - 1)