Path: blob/master/ invest-robot-contest_TinkoffBotTwitch-main/venv/lib/python3.8/site-packages/setuptools/glob.py
7763 views
"""1Filename globbing utility. Mostly a copy of `glob` from Python 3.5.23Changes include:4* `yield from` and PEP3102 `*` removed.5* Hidden files are not ignored.6"""78import os9import re10import fnmatch1112__all__ = ["glob", "iglob", "escape"]131415def glob(pathname, recursive=False):16"""Return a list of paths matching a pathname pattern.1718The pattern may contain simple shell-style wildcards a la19fnmatch. However, unlike fnmatch, filenames starting with a20dot are special cases that are not matched by '*' and '?'21patterns.2223If recursive is true, the pattern '**' will match any files and24zero or more directories and subdirectories.25"""26return list(iglob(pathname, recursive=recursive))272829def iglob(pathname, recursive=False):30"""Return an iterator which yields the paths matching a pathname pattern.3132The pattern may contain simple shell-style wildcards a la33fnmatch. However, unlike fnmatch, filenames starting with a34dot are special cases that are not matched by '*' and '?'35patterns.3637If recursive is true, the pattern '**' will match any files and38zero or more directories and subdirectories.39"""40it = _iglob(pathname, recursive)41if recursive and _isrecursive(pathname):42s = next(it) # skip empty string43assert not s44return it454647def _iglob(pathname, recursive):48dirname, basename = os.path.split(pathname)49if not has_magic(pathname):50if basename:51if os.path.lexists(pathname):52yield pathname53else:54# Patterns ending with a slash should match only directories55if os.path.isdir(dirname):56yield pathname57return58if not dirname:59if recursive and _isrecursive(basename):60for x in glob2(dirname, basename):61yield x62else:63for x in glob1(dirname, basename):64yield x65return66# `os.path.split()` returns the argument itself as a dirname if it is a67# drive or UNC path. Prevent an infinite recursion if a drive or UNC path68# contains magic characters (i.e. r'\\?\C:').69if dirname != pathname and has_magic(dirname):70dirs = _iglob(dirname, recursive)71else:72dirs = [dirname]73if has_magic(basename):74if recursive and _isrecursive(basename):75glob_in_dir = glob276else:77glob_in_dir = glob178else:79glob_in_dir = glob080for dirname in dirs:81for name in glob_in_dir(dirname, basename):82yield os.path.join(dirname, name)838485# These 2 helper functions non-recursively glob inside a literal directory.86# They return a list of basenames. `glob1` accepts a pattern while `glob0`87# takes a literal basename (so it only has to check for its existence).888990def glob1(dirname, pattern):91if not dirname:92if isinstance(pattern, bytes):93dirname = os.curdir.encode('ASCII')94else:95dirname = os.curdir96try:97names = os.listdir(dirname)98except OSError:99return []100return fnmatch.filter(names, pattern)101102103def glob0(dirname, basename):104if not basename:105# `os.path.split()` returns an empty basename for paths ending with a106# directory separator. 'q*x/' should match only directories.107if os.path.isdir(dirname):108return [basename]109else:110if os.path.lexists(os.path.join(dirname, basename)):111return [basename]112return []113114115# This helper function recursively yields relative pathnames inside a literal116# directory.117118119def glob2(dirname, pattern):120assert _isrecursive(pattern)121yield pattern[:0]122for x in _rlistdir(dirname):123yield x124125126# Recursively yields relative pathnames inside a literal directory.127def _rlistdir(dirname):128if not dirname:129if isinstance(dirname, bytes):130dirname = os.curdir.encode('ASCII')131else:132dirname = os.curdir133try:134names = os.listdir(dirname)135except os.error:136return137for x in names:138yield x139path = os.path.join(dirname, x) if dirname else x140for y in _rlistdir(path):141yield os.path.join(x, y)142143144magic_check = re.compile('([*?[])')145magic_check_bytes = re.compile(b'([*?[])')146147148def has_magic(s):149if isinstance(s, bytes):150match = magic_check_bytes.search(s)151else:152match = magic_check.search(s)153return match is not None154155156def _isrecursive(pattern):157if isinstance(pattern, bytes):158return pattern == b'**'159else:160return pattern == '**'161162163def escape(pathname):164"""Escape all special characters.165"""166# Escaping is done by wrapping any of "*?[" between square brackets.167# Metacharacters do not work in the drive part and shouldn't be escaped.168drive, pathname = os.path.splitdrive(pathname)169if isinstance(pathname, bytes):170pathname = magic_check_bytes.sub(br'[\1]', pathname)171else:172pathname = magic_check.sub(r'[\1]', pathname)173return drive + pathname174175176