This commit is contained in:
lz_db
2026-10-16 22:48:30 +08:00
parent 190de72571
commit 0b36fd3383
3161 changed files with 12787 additions and 77274 deletions
@@ -6,21 +6,14 @@ Changes include:
* Hidden files are not ignored.
"""
from __future__ import annotations
import fnmatch
import os
import re
from collections.abc import Iterable, Iterator
from typing import TYPE_CHECKING, AnyStr, overload
if TYPE_CHECKING:
from _typeshed import BytesPath, StrOrBytesPath, StrPath
import fnmatch
__all__ = ["glob", "iglob", "escape"]
def glob(pathname: AnyStr, recursive: bool = False) -> list[AnyStr]:
def glob(pathname, recursive=False):
"""Return a list of paths matching a pathname pattern.
The pattern may contain simple shell-style wildcards a la
@@ -34,7 +27,7 @@ def glob(pathname: AnyStr, recursive: bool = False) -> list[AnyStr]:
return list(iglob(pathname, recursive=recursive))
def iglob(pathname: AnyStr, recursive: bool = False) -> Iterator[AnyStr]:
def iglob(pathname, recursive=False):
"""Return an iterator which yields the paths matching a pathname pattern.
The pattern may contain simple shell-style wildcards a la
@@ -52,7 +45,7 @@ def iglob(pathname: AnyStr, recursive: bool = False) -> Iterator[AnyStr]:
return it
def _iglob(pathname: AnyStr, recursive: bool) -> Iterator[AnyStr]:
def _iglob(pathname, recursive):
dirname, basename = os.path.split(pathname)
glob_in_dir = glob2 if recursive and _isrecursive(basename) else glob1
@@ -73,7 +66,7 @@ def _iglob(pathname: AnyStr, recursive: bool) -> Iterator[AnyStr]:
# drive or UNC path. Prevent an infinite recursion if a drive or UNC path
# contains magic characters (i.e. r'\\?\C:').
if dirname != pathname and has_magic(dirname):
dirs: Iterable[AnyStr] = _iglob(dirname, recursive)
dirs = _iglob(dirname, recursive)
else:
dirs = [dirname]
if not has_magic(basename):
@@ -88,11 +81,7 @@ def _iglob(pathname: AnyStr, recursive: bool) -> Iterator[AnyStr]:
# takes a literal basename (so it only has to check for its existence).
@overload
def glob1(dirname: StrPath, pattern: str) -> list[str]: ...
@overload
def glob1(dirname: BytesPath, pattern: bytes) -> list[bytes]: ...
def glob1(dirname: StrOrBytesPath, pattern: str | bytes) -> list[str] | list[bytes]:
def glob1(dirname, pattern):
if not dirname:
if isinstance(pattern, bytes):
dirname = os.curdir.encode('ASCII')
@@ -102,8 +91,7 @@ def glob1(dirname: StrOrBytesPath, pattern: str | bytes) -> list[str] | list[byt
names = os.listdir(dirname)
except OSError:
return []
# mypy false-positives: str or bytes type possibility is always kept in sync
return fnmatch.filter(names, pattern) # type: ignore[type-var, return-value]
return fnmatch.filter(names, pattern)
def glob0(dirname, basename):
@@ -122,22 +110,15 @@ def glob0(dirname, basename):
# directory.
@overload
def glob2(dirname: StrPath, pattern: str) -> Iterator[str]: ...
@overload
def glob2(dirname: BytesPath, pattern: bytes) -> Iterator[bytes]: ...
def glob2(dirname: StrOrBytesPath, pattern: str | bytes) -> Iterator[str | bytes]:
def glob2(dirname, pattern):
assert _isrecursive(pattern)
yield pattern[:0]
yield from _rlistdir(dirname)
for x in _rlistdir(dirname):
yield x
# Recursively yields relative pathnames inside a literal directory.
@overload
def _rlistdir(dirname: StrPath) -> Iterator[str]: ...
@overload
def _rlistdir(dirname: BytesPath) -> Iterator[bytes]: ...
def _rlistdir(dirname: StrOrBytesPath) -> Iterator[str | bytes]:
def _rlistdir(dirname):
if not dirname:
if isinstance(dirname, bytes):
dirname = os.curdir.encode('ASCII')
@@ -145,28 +126,28 @@ def _rlistdir(dirname: StrOrBytesPath) -> Iterator[str | bytes]:
dirname = os.curdir
try:
names = os.listdir(dirname)
except OSError:
except os.error:
return
for x in names:
yield x
# mypy false-positives: str or bytes type possibility is always kept in sync
path = os.path.join(dirname, x) if dirname else x # type: ignore[arg-type]
path = os.path.join(dirname, x) if dirname else x
for y in _rlistdir(path):
yield os.path.join(x, y) # type: ignore[arg-type]
yield os.path.join(x, y)
magic_check = re.compile('([*?[])')
magic_check_bytes = re.compile(b'([*?[])')
def has_magic(s: str | bytes) -> bool:
def has_magic(s):
if isinstance(s, bytes):
return magic_check_bytes.search(s) is not None
match = magic_check_bytes.search(s)
else:
return magic_check.search(s) is not None
match = magic_check.search(s)
return match is not None
def _isrecursive(pattern: str | bytes) -> bool:
def _isrecursive(pattern):
if isinstance(pattern, bytes):
return pattern == b'**'
else:
@@ -174,12 +155,13 @@ def _isrecursive(pattern: str | bytes) -> bool:
def escape(pathname):
"""Escape all special characters."""
"""Escape all special characters.
"""
# Escaping is done by wrapping any of "*?[" between square brackets.
# Metacharacters do not work in the drive part and shouldn't be escaped.
drive, pathname = os.path.splitdrive(pathname)
if isinstance(pathname, bytes):
pathname = magic_check_bytes.sub(rb'[\1]', pathname)
pathname = magic_check_bytes.sub(br'[\1]', pathname)
else:
pathname = magic_check.sub(r'[\1]', pathname)
return drive + pathname