Skip to content

PathFinder

Handsdown API Index / Handsdown / Utils / PathFinder

Auto-generated documentation for handsdown.utils.path_finder module.

PathFinder

Show source in path_finder.py:18

Glob helper for matching paths inside root path.

With .gitignore-like include and exclude patterns.

Examples

path_finder = PathFinder(Path.cwd())
list(path_finder.glob('*.txt'))
['my_new.txt', 'my.txt', 'new.txt']

list(path_finder.include('my*').glob('*.txt'))
['my_new.txt', 'my.txt']

list(path_finder.exclude('*new*').glob('*.txt'))
['my.txt']

Arguments

  • root - Path to root folder.
  • glob_expr - glob expression to lookup in root

Raises

  • PathFinderError - If root is not absolute or not a directory.

Signature

class PathFinder:
    def __init__(self, root: Path) -> None:
        ...

PathFinder().exclude

Show source in path_finder.py:85

Add fnmatch expression to black list.

If black list is empty - no black list filtration applied. If expression does not have * or . characters, appends /* to it.

Arguments

  • fn_exrps - One or more fnmatch expressions.

Returns

A copy of itself.

Signature

def exclude(self: _R, *fn_exrps: str) -> _R:
    ...

PathFinder().glob

Show source in path_finder.py:128

Find all matching Path objects respecting PathFinder().include and PathFinder().exclude patterns.

Yields

Matching Path objects.

Signature

def glob(self, glob_expr: str) -> Iterator[Path]:
    ...

PathFinder().include

Show source in path_finder.py:64

Add fnmatch expression to white list.

If white list is empty - no white list filtration applied. If expression does not have * or . characters, appends /* to it.

Arguments

  • fn_exrps - One or more fnmatch expressions.

Returns

A copy of itself.

Signature

def include(self: _R, *fn_exrps: str) -> _R:
    ...

PathFinder().mkdir

Show source in path_finder.py:173

Create directories up to root if they do not exist.

Arguments

  • force - Delete existing parent if it is not a directory.

Raises

  • PathFinderError - If any existing parent is not a directory and not in force mode.

Signature

def mkdir(self, force: bool = False) -> None:
    ...

PathFinder().relative

Show source in path_finder.py:144

Find a relative path from root to target.

target should be an absolute path.

Arguments

  • target - Target path.

Returns

A relative path to target.

Signature

def relative(self, target: Path) -> Path:
    ...