Skip to content

Module dustpan

View Source
import shutil

from pathlib import Path

from typing import Iterable, Set

DEFAULT_INCLUDE = {"__pycache__", "*.pyc", "*.pyo"}

DEFAULT_EXCLUDE = {".venv/**/*"}

def remove_file(file: Path) -> None:

    """Remove a file

    Args:

        path (Path): The path to the file

    """

    if file.exists():

        file.unlink()

def remove_directory(directory: Path) -> None:

    """Remove a directory

    Args:

        path (Path): The path to the directory

    """

    if directory.exists():

        shutil.rmtree(directory)

def remove(path: Path) -> None:

    """Remove a file or directory

    Args:

        path (Path): The path to the file or directory

    """

    if path.exists():

        if path.is_dir():

            remove_directory(path)

        else:

            remove_file(path)

def search(

    *directories: Path, patterns: Iterable[str] = DEFAULT_INCLUDE, ignore: Iterable[str] = DEFAULT_EXCLUDE

) -> Set[Path]:

    """Search for path patterns within directories

    Args:

        patterns (Iterable[str], optional): Patterns to glob recursively. Defaults to DEFAULT_PATTERNS.

        ignore (Iterable[str], optional): Patterns to ignore. Defaults to DEFAULT_IGNORE.

    Returns:

        Set[Path]: [description]

    """

    paths = set()

    for directory in directories:

        for pattern in patterns:

            for path in directory.rglob(pattern):

                if path.exists():

                    paths.add(path)

        for pattern in ignore:

            for path in directory.rglob(pattern):

                if path in paths:

                    paths.remove(path)

    return paths

__all__ = ["remove", "remove_file", "remove_directory", "search"]

Sub-modules

Functions

remove

1
2
3
def remove(
    path: pathlib.Path
) -> None

Remove a file or directory

Parameters:

Name Type Description Default
path Path The path to the file or directory None
View Source
def remove(path: Path) -> None:

    """Remove a file or directory

    Args:

        path (Path): The path to the file or directory

    """

    if path.exists():

        if path.is_dir():

            remove_directory(path)

        else:

            remove_file(path)

remove_directory

1
2
3
def remove_directory(
    directory: pathlib.Path
) -> None

Remove a directory

Parameters:

Name Type Description Default
path Path The path to the directory None
View Source
def remove_directory(directory: Path) -> None:

    """Remove a directory

    Args:

        path (Path): The path to the directory

    """

    if directory.exists():

        shutil.rmtree(directory)

remove_file

1
2
3
def remove_file(
    file: pathlib.Path
) -> None

Remove a file

Parameters:

Name Type Description Default
path Path The path to the file None
View Source
def remove_file(file: Path) -> None:

    """Remove a file

    Args:

        path (Path): The path to the file

    """

    if file.exists():

        file.unlink()
1
2
3
4
5
def search(
    *directories: pathlib.Path,
    patterns: Iterable[str] = {'*.pyc', '__pycache__', '*.pyo'},
    ignore: Iterable[str] = {'.venv/**/*'}
) -> Set[pathlib.Path]

Search for path patterns within directories

Parameters:

Name Type Description Default
patterns Iterable[str] Patterns to glob recursively. Defaults to DEFAULT_PATTERNS. DEFAULT_PATTERNS
ignore Iterable[str] Patterns to ignore. Defaults to DEFAULT_IGNORE. DEFAULT_IGNORE

Returns:

Type Description
Set[Path] [description]
View Source
def search(

    *directories: Path, patterns: Iterable[str] = DEFAULT_INCLUDE, ignore: Iterable[str] = DEFAULT_EXCLUDE

) -> Set[Path]:

    """Search for path patterns within directories

    Args:

        patterns (Iterable[str], optional): Patterns to glob recursively. Defaults to DEFAULT_PATTERNS.

        ignore (Iterable[str], optional): Patterns to ignore. Defaults to DEFAULT_IGNORE.

    Returns:

        Set[Path]: [description]

    """

    paths = set()

    for directory in directories:

        for pattern in patterns:

            for path in directory.rglob(pattern):

                if path.exists():

                    paths.add(path)

        for pattern in ignore:

            for path in directory.rglob(pattern):

                if path in paths:

                    paths.remove(path)

    return paths