41 lines
1.0 KiB
Python
41 lines
1.0 KiB
Python
from __future__ import annotations
|
|
|
|
from dataclasses import dataclass
|
|
from pathlib import Path
|
|
|
|
|
|
@dataclass(frozen=True, slots=True)
|
|
class DirectoryEntry:
|
|
"""Directory metadata used by the navigator layer."""
|
|
|
|
name: str
|
|
path: Path
|
|
has_children: bool
|
|
|
|
|
|
def list_directories(path: Path) -> list[DirectoryEntry]:
|
|
"""Return direct child directories sorted by name."""
|
|
|
|
directory_path = path.expanduser().resolve()
|
|
entries: list[DirectoryEntry] = []
|
|
|
|
for child in directory_path.iterdir():
|
|
if child.is_dir():
|
|
entries.append(
|
|
DirectoryEntry(
|
|
name=child.name,
|
|
path=child,
|
|
has_children=has_subdirectories(child),
|
|
)
|
|
)
|
|
|
|
return sorted(entries, key=lambda entry: entry.name.casefold())
|
|
|
|
|
|
def has_subdirectories(path: Path) -> bool:
|
|
"""Return whether the directory contains at least one subdirectory."""
|
|
|
|
directory_path = path.expanduser().resolve()
|
|
|
|
return any(child.is_dir() for child in directory_path.iterdir())
|