# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/01_base.ipynb.

# %% auto 0
__all__ = ['TREEKWS', 'Xath', 'Path', 'File', 'Dir']

# %% ../nbs/01_base.ipynb 6
import pathlib

# %% ../nbs/01_base.ipynb 8
#| export


# %% ../nbs/01_base.ipynb 10
from atyp import StrQ

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

from pstr.util import toprivate

# %% ../nbs/01_base.ipynb 12
from psrc import fullpath, aspath, ListDir

# %% ../nbs/01_base.ipynb 14
from .mxns.plib import PathLibMixin
from .mxns.kind import FileKindMixin, DirKindMixin

# %% ../nbs/01_base.ipynb 16
_NAME, _TREE = tuple(map(toprivate, (NAME, TREE)))
TREEKWS = f'{TREE}kws'

# %% ../nbs/01_base.ipynb 17
class Xath(ListDir, PathLibMixin):
    pkind: str = PathKind.PATH
    aspec: tuple = (PATH, NAME, ABRV, DESC, TREEKWS, )
    inherit_aspec = True
    
    @property
    def kind(self) -> str: return str(self.pkind)
    @property
    def full(self) -> pathlib.Path: return fullpath(self.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)
    @tree.deleter
    def tree(self): self._tree = None

    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, pathlib.Path(DOT)))
        leaf = kwargs.pop(LEAF, getattr(self, LEAF, pathlib.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/01_base.ipynb 22
class Path(Xath):
    '''Path Entry'''
    ...

# %% ../nbs/01_base.ipynb 24
class File(FileKindMixin, Path):
    '''File Entry'''

# %% ../nbs/01_base.ipynb 26
class Dir(DirKindMixin, Path):
    '''Dir Entry'''
