# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/11_path.ipynb.

# %% auto 0
__all__ = ['ispath', 'isxath', 'ispathlike', 'isdir', 'isfile', 'ishidden', 'ispathy']

# %% ../nbs/11_path.ipynb 4
import os, pathlib
from typing import TypeGuard
from typing import Union, ForwardRef

# %% ../nbs/11_path.ipynb 5
from .atyp import PathLike
from .fref import dereference

# %% ../nbs/11_path.ipynb 7
def ispath(x) -> TypeGuard[pathlib.Path]:
    '''Check if `x` is `pathlib.Path`.'''
    return isinstance(x, pathlib.Path)

def isxath(
    x,
    globalsns: dict = None, localsns: dict = None, recursive_guard: set = None
) -> TypeGuard['Xath']:
    '''Check if `x` is `Xath`.'''
    xref = ForwardRef('Xath', module='xath', is_class=True)
    xtyp = dereference(xref, globalsns=globalsns, localsns=localsns, recursive_guard=recursive_guard)
    return isinstance(x, xtyp)

def ispathlike(x) -> TypeGuard[PathLike]:
    '''Check if `x` is `str`, `pathlib.Path` or `os.PathLike`.'''
    return isinstance(x, (str, pathlib.Path, os.PathLike))

def isdir(x) -> TypeGuard[pathlib.Path]: 
    '''Check if `x` is `pathlib.Path` and exists.'''
    return ispathlike(x) and pathlib.Path(x).is_dir()

def isfile(x) -> TypeGuard[pathlib.Path]: 
    '''Check if `x` is `pathlib.Path` and exists.'''
    return ispathlike(x) and pathlib.Path(x).is_file()

def ishidden(x) -> TypeGuard[pathlib.Path]: 
    '''Check if `x` is `pathlib.Path` and is a hidden file or directory.'''
    pname = pathlib.Path(x).name
    return ispathlike(x) and (pname.startswith('.') or pname.startswith('_'))

def ispathy(x) -> TypeGuard[Union['Xath', PathLike]]:
    '''Check if `x` is `str`, `pathlib.Path`, `os.PathLike`, or `Xath`.'''
    return isxath(x) and ispathlike(x)
