Skip to content

API reference

Glob

Glob pattern matcher with configurable features.

Use Glob.default() for POSIX fnmatch-like matching (bracket expansion and escape sequences enabled), or Glob.empty() for a minimal configuration. Chain builder methods to enable or disable features, then call match() to test patterns.

Examples:

>>> from globlin import Glob
>>> glob = Glob.default()
>>> glob.match("*.py", "foo.py")
True
>>> glob.match("*.py", "foo.txt")
False

New in version 0.3.

Methods:

default classmethod

default() -> Glob

Create a glob with POSIX fnmatch-like defaults.

Enables bracket expansion and escape sequences. Additional features like globstar can be added via builder methods.

Examples:

>>> glob = Glob.default()
>>> glob.match("[abc]", "b")
True
>>> glob.match(r"\*", "*")
True

New in version 0.3.

Source code in python/globlin/__init__.py
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
"""

BRACKET_EXPANSION = _flags.BRACKET_EXPANSION
"""Enable ``[abc]`` / ``[a-z]`` bracket expansion.

*New in version 0.3.*
"""

BRACE_EXPANSION = _flags.BRACE_EXPANSION
"""Enable ``{a,b,c}`` brace expansion.

*New in version 0.3.*
"""

NEGATE = _flags.NEGATE
"""Enable ``!pattern`` negation.

empty classmethod

empty() -> Glob

Create a glob with no features enabled.

Only * and ? wildcards work.

Examples:

>>> glob = Glob.empty()
>>> glob.match("*.py", "foo.py")
True
>>> glob.match("[abc]", "b")
False

New in version 0.3.

Source code in python/globlin/__init__.py
46
47
48
49
50
51
52
53
54
55
56
57
58
59
*New in version 0.3.*
"""

ESCAPE = _flags.ESCAPE
r"""Enable ``\`` escape sequences.

*New in version 0.3.*
"""

PATH_SEPARATOR = _flags.PATH_SEPARATOR
r"""Treat ``/`` as path separator  ``*`` and ``?`` do not match ``/``.

*New in version 0.3.*
"""

from_flags classmethod

from_flags(flags: int) -> Glob

Create a glob from raw bitflags.

Parameters:

  • flags

    (int) –

    Bitwise combination of constants from Flags.

Raises:

  • ValueError

    If any bits outside the valid range are set.

Examples:

>>> from globlin import Flags
>>> glob = Glob.from_flags(Flags.GLOB_STAR | Flags.PATH_SEPARATOR | Flags.ESCAPE)
>>> glob.match("src/**/*.py", "src/a/b/foo.py")
True

New in version 0.3.

Source code in python/globlin/__init__.py
62
63
64
65
66
67
68
69
70
71
"""All features enabled.

*New in version 0.3.*
"""

DEFAULT = _flags.DEFAULT
"""POSIX fnmatch-like defaults (``BRACKET_EXPANSION | ESCAPE``).

*New in version 0.3.*
"""

globstar

globstar() -> Self

Enable ** globstar matching.

Implies path_separator() since globstar is only meaningful when / is treated as a path separator.

Examples:

>>> glob = Glob.default().globstar()
>>> glob.match("src/**/*.py", "src/a/b/foo.py")
True
>>> glob.match("src/**/*.py", "src/foo.py")
True

New in version 0.3.

no_globstar

no_globstar() -> Self

Disable ** globstar matching.

New in version 0.3.

bracket_expansion

bracket_expansion() -> Self

Enable [abc] / [a-z] bracket expansion.

Examples:

>>> glob = Glob.empty().bracket_expansion()
>>> glob.match("[a-z]", "m")
True
>>> glob.match("[!abc]", "d")
True

New in version 0.3.

no_bracket_expansion

no_bracket_expansion() -> Self

Disable [abc] / [a-z] bracket expansion.

Examples:

>>> glob = Glob.default().no_bracket_expansion()
>>> glob.match("[abc]", "[abc]")
True

New in version 0.3.

brace_expansion

brace_expansion() -> Self

Enable {a,b,c} brace expansion.

Examples:

>>> glob = Glob.default().brace_expansion()
>>> glob.match("{src,lib}/*.py", "src/foo.py")
True
>>> glob.match("{src,lib}/*.py", "test/foo.py")
False

New in version 0.3.

no_brace_expansion

no_brace_expansion() -> Self

Disable {a,b,c} brace expansion.

New in version 0.3.

negate

negate() -> Self

Enable !pattern negation.

Examples:

>>> glob = Glob.default().negate()
>>> glob.match("!*.py", "foo.txt")
True
>>> glob.match("!*.py", "foo.py")
False

New in version 0.3.

no_negate

no_negate() -> Self

Disable !pattern negation.

New in version 0.3.

escape

escape() -> Self

Enable \ escape sequences.

Examples:

>>> glob = Glob.empty().escape()
>>> glob.match("\\*", "*")
True
>>> glob.match("\\*", "foo")
False

New in version 0.3.

no_escape

no_escape() -> Self

Disable \ escape sequences.

Examples:

>>> glob = Glob.default().no_escape()
>>> glob.match("\\*", "\\foo")
True

New in version 0.3.

path_separator

path_separator() -> Self

Enable path separator handling (* and ? do not match /).

Examples:

>>> glob = Glob.default().path_separator()
>>> glob.match("*.py", "foo.py")
True
>>> glob.match("*.py", "dir/foo.py")
False

New in version 0.3.

no_path_separator

no_path_separator() -> Self

Disable path separator handling (* and ? match /).

New in version 0.3.

match

match(pattern: str, value: str) -> bool

Match a glob pattern against a value.

Parameters:

  • pattern

    (str) –

    The glob pattern.

  • value

    (str) –

    The string to match against.

Returns:

  • bool

    True if the value matches the pattern.

Examples:

>>> glob = Glob.default()
>>> glob.match("test_?", "test_a")
True
>>> glob.match("test_?", "test_ab")
False

New in version 0.3.

Flags

Bases: IntFlag

Low-level flag constants for bitwise composition.

Attributes:

  • EMPTY

    No features enabled.

  • GLOB_STAR

    Enable ** globstar matching.

  • BRACKET_EXPANSION

    Enable [abc] / [a-z] bracket expansion.

  • BRACE_EXPANSION

    Enable {a,b,c} brace expansion.

  • NEGATE

    Enable !pattern negation.

  • ESCAPE

    Enable \ escape sequences.

  • PATH_SEPARATOR

    Treat / as path separator — * and ? do not match /.

  • ALL

    All features enabled.

  • DEFAULT

    POSIX fnmatch-like defaults (BRACKET_EXPANSION | ESCAPE).

EMPTY class-attribute instance-attribute

EMPTY = EMPTY

No features enabled.

New in version 0.3.

GLOB_STAR class-attribute instance-attribute

GLOB_STAR = GLOB_STAR

Enable ** globstar matching.

New in version 0.3.

BRACKET_EXPANSION class-attribute instance-attribute

BRACKET_EXPANSION = BRACKET_EXPANSION

Enable [abc] / [a-z] bracket expansion.

New in version 0.3.

BRACE_EXPANSION class-attribute instance-attribute

BRACE_EXPANSION = BRACE_EXPANSION

Enable {a,b,c} brace expansion.

New in version 0.3.

NEGATE class-attribute instance-attribute

NEGATE = NEGATE

Enable !pattern negation.

New in version 0.3.

ESCAPE class-attribute instance-attribute

ESCAPE = ESCAPE

Enable \ escape sequences.

New in version 0.3.

PATH_SEPARATOR class-attribute instance-attribute

PATH_SEPARATOR = PATH_SEPARATOR

Treat / as path separator — * and ? do not match /.

New in version 0.3.

ALL class-attribute instance-attribute

ALL = ALL

All features enabled.

New in version 0.3.

DEFAULT class-attribute instance-attribute

DEFAULT = DEFAULT

POSIX fnmatch-like defaults (BRACKET_EXPANSION | ESCAPE).

New in version 0.3.

fnmatch builtin

fnmatch(pattern: str, value: str, *flags: Flag) -> bool
Deprecated

Use Glob.default() instead

Match a glob pattern against a value.

Flag deprecated

Deprecated

Use Glob instead

Flag enum for fnmatch.

Attributes:

EMPTY instance-attribute

EMPTY: Flag

No features enabled.

GLOB_STAR instance-attribute

GLOB_STAR: Flag

Enable ** globstar matching.

BRACKET_EXPANSION instance-attribute

BRACKET_EXPANSION: Flag

Enable [abc] / [a-z] bracket expansion.

BRACE_EXPANSION instance-attribute

BRACE_EXPANSION: Flag

Enable {a,b,c} brace expansion.

NEGATE instance-attribute

NEGATE: Flag

Enable !pattern negation.

ESCAPE instance-attribute

ESCAPE: Flag

Enable \ escape sequences.

NO_PATH instance-attribute

NO_PATH: Flag

* and ? match / (disables path separator handling).