# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/50_srcs.ipynb.

# %% auto 0
__all__ = ['TREEKWS', 'Src', 'PathSrc', 'FileSrc', 'DirSrc']

# %% ../nbs/50_srcs.ipynb 5
import os
from pathlib import Path

# %% ../nbs/50_srcs.ipynb 7
from atyp import (StrQ, CallQ, WalkParts)

from nchr import DOT
from nlit import (BASE, LEAF, PATH, NAME, TREE, __FSPATH__, PathKind, ABRV, DESC)

from aspec import aspec
from pstr.util import toprivate

# %% ../nbs/50_srcs.ipynb 9
from .util import fullpath, aspath
from .plib import PLib
from .ldir import ListDir

# %% ../nbs/50_srcs.ipynb 11
_PATH, _NAME, _TREE = tuple(map(toprivate, (PATH, NAME, TREE)))
TREEKWS = f'{TREE}kws'

# %% ../nbs/50_srcs.ipynb 12
class Src(str, PLib, os.PathLike, aspec):
    aspec: tuple = (PATH, NAME, ABRV, DESC, TREEKWS, )
    ptype: PathKind = PathKind.PATH
    
    @property
    def kind(self) -> str: return str(self.ptype)
    @property
    def full(self): return fullpath(self.path)

    @property 
    def path(self) -> Path: return getattr(self, _PATH, Path(DOT))
    @path.setter
    def path(self, path: str): setattr(self, _PATH, Path(path))
    
    @property
    def name(self) -> str: return getattr(self, _NAME, self.stem)
    @name.setter
    def name(self, name: str): setattr(self, _NAME, name)

    @property
    def pathname(self): return self.path.name

    @property
    def tree(self) -> ListDir: return getattr(self, _TREE, ListDir(self.path, **getattr(self, TREEKWS, dict())))
    @tree.setter
    def tree(self, tree: str):
        tree = tree if isinstance(tree, ListDir) else ListDir(tree, **getattr(self, TREEKWS, dict()))
        setattr(self, _TREE, tree)
    
    def walk(self, topdown: bool = True, onerror: CallQ = None, followlinks: bool = False) -> WalkParts:
        return os.walk(self.path, topdown, onerror, followlinks)

    def __fspath__(self) -> str: return self.path
    
    def __repr__(self): return f'{self.clsname}({self.path})'
    
    def __new__(cls, path: StrQ = None, *args, **kwargs):
        return super().__new__(cls, aspath(path))
    
    def __init__(
        self, path: StrQ = None, name: StrQ = None, 
        abrv: StrQ = None, desc: StrQ = None, *args, **kwargs        
    ):
        self.path = (path or self.path)
        self.name = (name or self.pathname)
        self.abrv = abrv
        self.desc = desc
        self.treekws = kwargs.pop(TREEKWS, dict())
        self.tree = self.path

    def update(self, **kwargs):
        path = kwargs.pop(PATH, self)
        base = kwargs.pop(BASE, getattr(self, BASE, Path(DOT)))
        leaf = kwargs.pop(LEAF, getattr(self, LEAF, Path(DOT)))
        kwds = dict(leaf=leaf, base=base, path=path, **kwargs)
        return super().update(**kwds)

    def makecopy(self, *args, **kwargs):        
        kwds = self.getattrs(**kwargs)
        path = kwds.pop(PATH, self)
        new = type(self)(path, *args, **kwds)
        return new
    
    def view(self, **kwargs):
        self.tree.view(**kwargs)

# %% ../nbs/50_srcs.ipynb 14
class PathSrc(Src):
    '''Path Entry'''
    ...

# %% ../nbs/50_srcs.ipynb 16
class FileSrc(Src):
    '''File Entry'''
    ptype: PathKind = PathKind.FILE

# %% ../nbs/50_srcs.ipynb 18
class DirSrc(Src):  
    '''Dir Entry'''
    ptype: PathKind = PathKind.DIR
