38 lines
1.2 KiB
Python
38 lines
1.2 KiB
Python
from pathlib import Path
|
|
from typing import Iterable
|
|
|
|
from pathspec import PathSpec
|
|
|
|
from ..preset.detector import ENTRY_SKIP
|
|
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, rootpath: Path, basepath: Path, depth: int = 0
|
|
) -> Iterable[Entry]:
|
|
if basepath.is_dir():
|
|
if basepath.name.startswith(".") or self._ignore.match_file(basepath):
|
|
return
|
|
entry = self._detector.detect_entry(basepath, rootpath)
|
|
if entry:
|
|
yield entry
|
|
if (
|
|
not entry == ENTRY_SKIP
|
|
and depth < self._max_depth
|
|
and basepath.is_dir()
|
|
):
|
|
for path in basepath.iterdir():
|
|
yield from self._resolve(rootpath, path, depth + 1)
|
|
|
|
def resolve(self, basepath: Path) -> Iterable[Entry]:
|
|
result = list(self._resolve(basepath, basepath))
|
|
result.reverse()
|
|
return result
|