Skip to main content

virtual-glob

PyPI

Globbing of remote/virtual file systems.

Motivation

Globbing is a common operation in many programs, but it is not always required to use the local file system, as is the case for glob.glob or pathlib.Path.glob. This package provides a way to glob over remote/virtual file systems.

Apart from python-glob2, which is unmaintained, I could not find another Python packages that provided this functionality, so I made my own!

Usage

Install the package:

pip install virtual-glob

Use the glob function, which can take a pathlib.Path instance, the provided InMemoryPath class, or any other class that implements the VirtualPath below:

from virtual_glob import glob, InMemoryPath

fs = InMemoryPath.from_list(["a/b/c/my1.txt", "e/f/g/my2.txt", "x/y/z/other.txt"])
matches = {p.path for p in glob(fs, "**/my[0-9].txt")}
assert matches == {"a/b/c/my1.txt", "e/f/g/my2.txt"}

glob also accepts the keyword arguments:

  • follow_symlinks: whether to follow symbolic links when iterating directories (default: False)
  • depth_first: whether to iterate directories in depth-first order, or breadth-first (default: True)

See the tests for more examples.

Virtual file systems

The file system must be accessible via a single class, directly mimicking pathlib.Path, with the following protocol:

class VirtualPath(Protocol):
    @property
    def name(self) -> str:
        """Return the name of this path."""

    def is_dir(self) -> bool:
        """Return True if this path is a directory."""

    def is_symlink(self) -> bool:
        """Return True if this path is a symbolic link."""

    def iterdir(self: PathType) -> Iterable[PathType]:
        """Iterate over the contents of this directory."""

    def joinpath(self: PathType, *parts: str) -> PathType:
        """Join this path with the given parts."""

    def exists(self) -> bool:
        """Return True if this path exists."""

Rules

  • Patterns must be in POSIX format, i.e. / is the path separator.
  • Patterns must be relative, i.e. they must not start with /.
  • Patterns are case-sensitive.
  • Patterns with a trailing / only match directories.
  • ** matches zero or more directories, it must be the only thing in a path segment.
  • * matches zero or more characters, except /.
  • ? matches exactly one character, except /.
  • [...] matches one character in the set, except /.
  • [!...] matches one character not in the set, except /.

Design

Make as few calls to the underlying file system as possible, particularly for iterdir. For example, looking to "short-circuit" when a pattern contains no "magic" characters.

Release files for virtual_glob 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for virtual_glob 0.2.0
File Size Uploaded
virtual_glob-0.2.0.tar.gz 10.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for virtual_glob 0.2.0
File Interpreter ABI Platform
virtual_glob-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 17.5 kB

Release files / virtual_glob-0.2.0.tar.gz

Download URL virtual_glob-0.2.0.tar.gz
Size 10.8 kB
Tags Source
SHA-256 checksum
How to use checksums
1681cae20882de9591c663b82d156019db96cb53c180c664853ca15a9707bdd2
BLAKE2b-256 checksum
How to use checksums
5240790d9784e99965f4c389240a7929d54d5b72487a3d595992d0e905a62ee5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-requests/2.28.1

Release files / virtual_glob-0.2.0-py3-none-any.whl

Download URL virtual_glob-0.2.0-py3-none-any.whl
Size 6.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
09e51d39c44f3f2422493f978ff77a9b8520771624933b4adc9d54d4cf70af32
BLAKE2b-256 checksum
How to use checksums
18a5ff3e499220e37681c083ee86335ec1f56a880c9f6f4b1e06e295c8d6f999
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-requests/2.28.1

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page