Update glob.py and test/test_glob.py from CPython v3.11.2 by MegasKomnenos · Pull Request #4744 · RustPython/RustPython
__all__ = ["glob", "iglob", "escape"]
def glob(pathname, *, root_dir=None, dir_fd=None, recursive=False): def glob(pathname, *, root_dir=None, dir_fd=None, recursive=False, include_hidden=False): """Return a list of paths matching a pathname pattern.
The pattern may contain simple shell-style wildcards a la fnmatch. However, unlike fnmatch, filenames starting with a fnmatch. Unlike fnmatch, filenames starting with a dot are special cases that are not matched by '*' and '?' patterns. patterns by default.
If recursive is true, the pattern '**' will match any files and If `include_hidden` is true, the patterns '*', '?', '**' will match hidden directories.
If `recursive` is true, the pattern '**' will match any files and zero or more directories and subdirectories. """ return list(iglob(pathname, root_dir=root_dir, dir_fd=dir_fd, recursive=recursive)) return list(iglob(pathname, root_dir=root_dir, dir_fd=dir_fd, recursive=recursive, include_hidden=include_hidden))
def iglob(pathname, *, root_dir=None, dir_fd=None, recursive=False): def iglob(pathname, *, root_dir=None, dir_fd=None, recursive=False, include_hidden=False): """Return an iterator which yields the paths matching a pathname pattern.
The pattern may contain simple shell-style wildcards a la
def _iglob(pathname, root_dir, dir_fd, recursive, dironly): def _iglob(pathname, root_dir, dir_fd, recursive, dironly, include_hidden=False): dirname, basename = os.path.split(pathname) if not has_magic(pathname): assert not dironly
# These 2 helper functions non-recursively glob inside a literal directory. # They return a list of basenames. _glob1 accepts a pattern while _glob0 # takes a literal basename (so it only has to check for its existence).
def _glob1(dirname, pattern, dir_fd, dironly): def _glob1(dirname, pattern, dir_fd, dironly, include_hidden=False): names = _listdir(dirname, dir_fd, dironly) if not _ishidden(pattern): names = (x for x in names if not _ishidden(x)) if include_hidden or not _ishidden(pattern): names = (x for x in names if include_hidden or not _ishidden(x)) return fnmatch.filter(names, pattern)
def _glob0(dirname, basename, dir_fd, dironly): def _glob0(dirname, basename, dir_fd, dironly, include_hidden=False): if basename: if _lexists(_join(dirname, basename), dir_fd): return [basename]
def _glob2(dirname, pattern, dir_fd, dironly): def _glob2(dirname, pattern, dir_fd, dironly, include_hidden=False): assert _isrecursive(pattern) yield pattern[:0] yield from _rlistdir(dirname, dir_fd, dironly) yield from _rlistdir(dirname, dir_fd, dironly, include_hidden=include_hidden)
# If dironly is false, yields all file names inside a directory. # If dironly is true, yields only directory names.
# Recursively yields relative pathnames inside a literal directory. def _rlistdir(dirname, dir_fd, dironly): def _rlistdir(dirname, dir_fd, dironly, include_hidden=False): names = _listdir(dirname, dir_fd, dironly) for x in names: if not _ishidden(x): if include_hidden or not _ishidden(x): yield x path = _join(dirname, x) if dirname else x for y in _rlistdir(path, dir_fd, dironly): for y in _rlistdir(path, dir_fd, dironly, include_hidden=include_hidden): yield _join(x, y)