38 lines
1.0 KiB
Python
38 lines
1.0 KiB
Python
from pathlib import Path
|
|
from typing import Iterable, List, 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, ignore: List[str]):
|
|
self._detector = detector
|
|
self._ignore = ignore
|
|
|
|
def resolve(self, basepath: Path, depth: int = 128) -> Iterable[Entry]:
|
|
if basepath.is_dir():
|
|
if basepath.name in self._ignore:
|
|
return
|
|
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)
|