Skip to content

glob

GlobSearch

Source code in muxtools/utils/glob.py
class GlobSearch:
    paths: list[Path]

    def __init__(
        self,
        pattern: str,
        allow_multiple: bool = False,
        dir: PathLike = None,
        recursive: bool = True,
    ) -> None:
        """
        Glob Pattern based search for files

        :param pattern:         Glob pattern
        :param allow_multiple:  Will return all file matches if True and only the first if False
        :param dir:             Directory to run the search in. Defaults to current working dir.
        :param recursive:       Search recursively
        """
        self.paths = []
        dir = Path(dir) if isinstance(dir, str) else dir

        if dir is None:
            dir = Path(os.getcwd()).resolve()

        search = dir.rglob(pattern) if recursive else dir.glob(pattern)

        for f in search:
            self.paths.append(f)

            if not allow_multiple:
                break

__init__(pattern, allow_multiple=False, dir=None, recursive=True)

Glob Pattern based search for files

Parameters:

Name Type Description Default
pattern str

Glob pattern

required
allow_multiple bool

Will return all file matches if True and only the first if False

False
dir PathLike

Directory to run the search in. Defaults to current working dir.

None
recursive bool

Search recursively

True
Source code in muxtools/utils/glob.py
def __init__(
    self,
    pattern: str,
    allow_multiple: bool = False,
    dir: PathLike = None,
    recursive: bool = True,
) -> None:
    """
    Glob Pattern based search for files

    :param pattern:         Glob pattern
    :param allow_multiple:  Will return all file matches if True and only the first if False
    :param dir:             Directory to run the search in. Defaults to current working dir.
    :param recursive:       Search recursively
    """
    self.paths = []
    dir = Path(dir) if isinstance(dir, str) else dir

    if dir is None:
        dir = Path(os.getcwd()).resolve()

    search = dir.rglob(pattern) if recursive else dir.glob(pattern)

    for f in search:
        self.paths.append(f)

        if not allow_multiple:
            break