# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/04_walk.ipynb.

# %% auto 0
__all__ = ['walktree', 'walkparts']

# %% ../nbs/04_walk.ipynb 5
import os
from pathlib import Path
from typing import (Any, Union, Generator, Optional, TypeAlias, Callable, Iterable)

# %% ../nbs/04_walk.ipynb 7
from nchr import NIL, SPACE, TreeChr
from nlit.byte import (BYTE_SYMBS as UNITS, BYTE, BYTES)
from quac import (richtree, intq, strq, boolq)
from chck.path import ishidden

# %% ../nbs/04_walk.ipynb 9
from .util import fullpath, pathsort
from .pfmt import fmtpath, PathFormatFunc

# %% ../nbs/04_walk.ipynb 12
def walktree(
    path: Path, 
    tree: Optional[richtree] = None,
    prefix: str = NIL, 
    hidden: bool = False, 
    fmtfunc: PathFormatFunc = fmtpath,
) -> Generator[str, Any, Any]:
    
    path = fullpath(path or os.getcwd())
    srtd = pathsort(path)
    strs = [TreeChr.TEE] * (len(srtd) - 1) + [TreeChr.END]
       
    for tstr, path in zip(strs, srtd):
        if ishidden(path) and not hidden: continue
        name = fmtfunc(path, prefix, tstr, suffix='')       
        brch = tree.add(name) if tree else None
        yield name
        
        if path.is_dir():
            newfix = TreeChr.join(prefix, tstr)
            yield from walktree(path, tree=brch, prefix=newfix, hidden=hidden, fmtfunc=fmtfunc)

# %% ../nbs/04_walk.ipynb 14
def walkparts(path: Path) -> set:
    parts = set()
    for dpath, _, _ in os.walk(path): 
        parts.update(Path(dpath).parts)
    parts ^= set(path.parts)
    return parts
