29 lines
913 B
Python
29 lines
913 B
Python
from pathlib import Path
|
|
from typing import Iterable
|
|
|
|
from pathspec import PathSpec
|
|
|
|
from .detector import EntryDetector
|
|
from .entry import Entry
|
|
|
|
|
|
class EntryResolver:
|
|
def __init__(self, detector: EntryDetector, ignore: PathSpec, max_depth: int = 4):
|
|
self._detector = detector
|
|
self._ignore = ignore
|
|
self._max_depth = max_depth
|
|
|
|
def _resolve(self, basepath: Path, depth: int = 0) -> Iterable[Entry]:
|
|
if basepath.is_dir():
|
|
if self._ignore.match_file(basepath):
|
|
return
|
|
entry = self._detector.detect_entry(basepath)
|
|
if entry:
|
|
yield entry
|
|
if depth < self._max_depth and basepath.is_dir():
|
|
for path in basepath.iterdir():
|
|
yield from self._resolve(path, depth + 1)
|
|
|
|
def resolve(self, basepath: Path) -> Iterable[Entry]:
|
|
return self._resolve(basepath)
|