Skip to content

Glob

Bases: BaseFileFilter, FrozenModel

Filter files or directories with path matching a glob expression.

.. versionadded:: 0.8.0 Replaces deprecated onetl.core.FileFilter

Parameters

pattern : str

Pattern (e.g. ``*.csv``) for which any **file** (only file) path should match

Examples

Create glob filter:

.. code:: python

from onetl.file.filter import Glob

glob = Glob("*.csv")
Source code in onetl/file/filter/glob.py
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
class Glob(BaseFileFilter, FrozenModel):
    """Filter files or directories with path matching a glob expression.

    .. versionadded:: 0.8.0
        Replaces deprecated ``onetl.core.FileFilter``

    Parameters
    ----------

    pattern : str

        Pattern (e.g. ``*.csv``) for which any **file** (only file) path should match

    Examples
    --------

    Create glob filter:

    .. code:: python

        from onetl.file.filter import Glob

        glob = Glob("*.csv")
    """

    class Config:
        arbitrary_types_allowed = True

    pattern: str

    def __init__(self, pattern: str):
        # this is only to allow passing glob as positional argument
        super().__init__(pattern=pattern)  # type: ignore

    def __repr__(self):
        return f"{self.__class__.__name__}({self.pattern!r})"

    def match(self, path: PathProtocol) -> bool:
        if not path.is_file():
            return True

        return path.match(self.pattern)

    @validator("pattern", pre=True)
    def _validate_pattern(cls, value: str) -> str:
        if not glob.has_magic(value):
            raise ValueError(f"Invalid glob: {value!r}")

        return value

match(path)

Source code in onetl/file/filter/glob.py
53
54
55
56
57
def match(self, path: PathProtocol) -> bool:
    if not path.is_file():
        return True

    return path.match(self.pattern)