Yield entries under the workspace root respecting depth/hidden filters.
(
root: Path | str,
*,
recursive: bool = True,
max_depth: int = 5,
include_hidden: bool = False,
)
| 17 | |
| 18 | |
| 19 | def iter_workspace_entries( |
| 20 | root: Path | str, |
| 21 | *, |
| 22 | recursive: bool = True, |
| 23 | max_depth: int = 5, |
| 24 | include_hidden: bool = False, |
| 25 | ) -> Iterator[WorkspaceEntry]: |
| 26 | """Yield entries under the workspace root respecting depth/hidden filters.""" |
| 27 | |
| 28 | base = Path(root).resolve() |
| 29 | if not base.exists(): |
| 30 | return |
| 31 | |
| 32 | stack: List[tuple[Path, int]] = [(base, 0)] |
| 33 | while stack: |
| 34 | current, depth = stack.pop() |
| 35 | try: |
| 36 | children = sorted(current.iterdir(), key=lambda p: p.name.lower()) |
| 37 | except FileNotFoundError: |
| 38 | continue |
| 39 | except PermissionError: |
| 40 | continue |
| 41 | for child in children: |
| 42 | try: |
| 43 | rel = child.relative_to(base) |
| 44 | except ValueError: |
| 45 | continue |
| 46 | if not include_hidden and _is_hidden(rel): |
| 47 | continue |
| 48 | entry_type = "directory" if child.is_dir() else "file" |
| 49 | size = None |
| 50 | modified = None |
| 51 | try: |
| 52 | stat = child.stat() |
| 53 | modified = stat.st_mtime |
| 54 | if child.is_file(): |
| 55 | size = stat.st_size |
| 56 | except (FileNotFoundError, PermissionError, OSError): |
| 57 | pass |
| 58 | child_depth = depth + 1 |
| 59 | yield WorkspaceEntry( |
| 60 | path=str(rel), |
| 61 | type=entry_type, |
| 62 | size=size, |
| 63 | modified_ts=modified, |
| 64 | depth=child_depth, |
| 65 | ) |
| 66 | if recursive and child.is_dir() and child_depth < max_depth: |
| 67 | stack.append((child, child_depth)) |
| 68 | |
| 69 | |
| 70 | def _is_hidden(relative_path: Path) -> bool: |
no test coverage detected