Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ShExBy/shex_roo.../lib64/python3....
File: glob.py
"""Filename globbing utility."""
[0] Fix | Delete
[1] Fix | Delete
import os
[2] Fix | Delete
import re
[3] Fix | Delete
import fnmatch
[4] Fix | Delete
[5] Fix | Delete
__all__ = ["glob", "iglob", "escape"]
[6] Fix | Delete
[7] Fix | Delete
def glob(pathname, *, recursive=False):
[8] Fix | Delete
"""Return a list of paths matching a pathname pattern.
[9] Fix | Delete
[10] Fix | Delete
The pattern may contain simple shell-style wildcards a la
[11] Fix | Delete
fnmatch. However, unlike fnmatch, filenames starting with a
[12] Fix | Delete
dot are special cases that are not matched by '*' and '?'
[13] Fix | Delete
patterns.
[14] Fix | Delete
[15] Fix | Delete
If recursive is true, the pattern '**' will match any files and
[16] Fix | Delete
zero or more directories and subdirectories.
[17] Fix | Delete
"""
[18] Fix | Delete
return list(iglob(pathname, recursive=recursive))
[19] Fix | Delete
[20] Fix | Delete
def iglob(pathname, *, recursive=False):
[21] Fix | Delete
"""Return an iterator which yields the paths matching a pathname pattern.
[22] Fix | Delete
[23] Fix | Delete
The pattern may contain simple shell-style wildcards a la
[24] Fix | Delete
fnmatch. However, unlike fnmatch, filenames starting with a
[25] Fix | Delete
dot are special cases that are not matched by '*' and '?'
[26] Fix | Delete
patterns.
[27] Fix | Delete
[28] Fix | Delete
If recursive is true, the pattern '**' will match any files and
[29] Fix | Delete
zero or more directories and subdirectories.
[30] Fix | Delete
"""
[31] Fix | Delete
it = _iglob(pathname, recursive, False)
[32] Fix | Delete
if recursive and _isrecursive(pathname):
[33] Fix | Delete
s = next(it) # skip empty string
[34] Fix | Delete
assert not s
[35] Fix | Delete
return it
[36] Fix | Delete
[37] Fix | Delete
def _iglob(pathname, recursive, dironly):
[38] Fix | Delete
dirname, basename = os.path.split(pathname)
[39] Fix | Delete
if not has_magic(pathname):
[40] Fix | Delete
assert not dironly
[41] Fix | Delete
if basename:
[42] Fix | Delete
if os.path.lexists(pathname):
[43] Fix | Delete
yield pathname
[44] Fix | Delete
else:
[45] Fix | Delete
# Patterns ending with a slash should match only directories
[46] Fix | Delete
if os.path.isdir(dirname):
[47] Fix | Delete
yield pathname
[48] Fix | Delete
return
[49] Fix | Delete
if not dirname:
[50] Fix | Delete
if recursive and _isrecursive(basename):
[51] Fix | Delete
yield from _glob2(dirname, basename, dironly)
[52] Fix | Delete
else:
[53] Fix | Delete
yield from _glob1(dirname, basename, dironly)
[54] Fix | Delete
return
[55] Fix | Delete
# `os.path.split()` returns the argument itself as a dirname if it is a
[56] Fix | Delete
# drive or UNC path. Prevent an infinite recursion if a drive or UNC path
[57] Fix | Delete
# contains magic characters (i.e. r'\\?\C:').
[58] Fix | Delete
if dirname != pathname and has_magic(dirname):
[59] Fix | Delete
dirs = _iglob(dirname, recursive, True)
[60] Fix | Delete
else:
[61] Fix | Delete
dirs = [dirname]
[62] Fix | Delete
if has_magic(basename):
[63] Fix | Delete
if recursive and _isrecursive(basename):
[64] Fix | Delete
glob_in_dir = _glob2
[65] Fix | Delete
else:
[66] Fix | Delete
glob_in_dir = _glob1
[67] Fix | Delete
else:
[68] Fix | Delete
glob_in_dir = _glob0
[69] Fix | Delete
for dirname in dirs:
[70] Fix | Delete
for name in glob_in_dir(dirname, basename, dironly):
[71] Fix | Delete
yield os.path.join(dirname, name)
[72] Fix | Delete
[73] Fix | Delete
# These 2 helper functions non-recursively glob inside a literal directory.
[74] Fix | Delete
# They return a list of basenames. _glob1 accepts a pattern while _glob0
[75] Fix | Delete
# takes a literal basename (so it only has to check for its existence).
[76] Fix | Delete
[77] Fix | Delete
def _glob1(dirname, pattern, dironly):
[78] Fix | Delete
names = list(_iterdir(dirname, dironly))
[79] Fix | Delete
if not _ishidden(pattern):
[80] Fix | Delete
names = (x for x in names if not _ishidden(x))
[81] Fix | Delete
return fnmatch.filter(names, pattern)
[82] Fix | Delete
[83] Fix | Delete
def _glob0(dirname, basename, dironly):
[84] Fix | Delete
if not basename:
[85] Fix | Delete
# `os.path.split()` returns an empty basename for paths ending with a
[86] Fix | Delete
# directory separator. 'q*x/' should match only directories.
[87] Fix | Delete
if os.path.isdir(dirname):
[88] Fix | Delete
return [basename]
[89] Fix | Delete
else:
[90] Fix | Delete
if os.path.lexists(os.path.join(dirname, basename)):
[91] Fix | Delete
return [basename]
[92] Fix | Delete
return []
[93] Fix | Delete
[94] Fix | Delete
# Following functions are not public but can be used by third-party code.
[95] Fix | Delete
[96] Fix | Delete
def glob0(dirname, pattern):
[97] Fix | Delete
return _glob0(dirname, pattern, False)
[98] Fix | Delete
[99] Fix | Delete
def glob1(dirname, pattern):
[100] Fix | Delete
return _glob1(dirname, pattern, False)
[101] Fix | Delete
[102] Fix | Delete
# This helper function recursively yields relative pathnames inside a literal
[103] Fix | Delete
# directory.
[104] Fix | Delete
[105] Fix | Delete
def _glob2(dirname, pattern, dironly):
[106] Fix | Delete
assert _isrecursive(pattern)
[107] Fix | Delete
yield pattern[:0]
[108] Fix | Delete
yield from _rlistdir(dirname, dironly)
[109] Fix | Delete
[110] Fix | Delete
# If dironly is false, yields all file names inside a directory.
[111] Fix | Delete
# If dironly is true, yields only directory names.
[112] Fix | Delete
def _iterdir(dirname, dironly):
[113] Fix | Delete
if not dirname:
[114] Fix | Delete
if isinstance(dirname, bytes):
[115] Fix | Delete
dirname = bytes(os.curdir, 'ASCII')
[116] Fix | Delete
else:
[117] Fix | Delete
dirname = os.curdir
[118] Fix | Delete
try:
[119] Fix | Delete
with os.scandir(dirname) as it:
[120] Fix | Delete
for entry in it:
[121] Fix | Delete
try:
[122] Fix | Delete
if not dironly or entry.is_dir():
[123] Fix | Delete
yield entry.name
[124] Fix | Delete
except OSError:
[125] Fix | Delete
pass
[126] Fix | Delete
except OSError:
[127] Fix | Delete
return
[128] Fix | Delete
[129] Fix | Delete
# Recursively yields relative pathnames inside a literal directory.
[130] Fix | Delete
def _rlistdir(dirname, dironly):
[131] Fix | Delete
names = list(_iterdir(dirname, dironly))
[132] Fix | Delete
for x in names:
[133] Fix | Delete
if not _ishidden(x):
[134] Fix | Delete
yield x
[135] Fix | Delete
path = os.path.join(dirname, x) if dirname else x
[136] Fix | Delete
for y in _rlistdir(path, dironly):
[137] Fix | Delete
yield os.path.join(x, y)
[138] Fix | Delete
[139] Fix | Delete
[140] Fix | Delete
magic_check = re.compile('([*?[])')
[141] Fix | Delete
magic_check_bytes = re.compile(b'([*?[])')
[142] Fix | Delete
[143] Fix | Delete
def has_magic(s):
[144] Fix | Delete
if isinstance(s, bytes):
[145] Fix | Delete
match = magic_check_bytes.search(s)
[146] Fix | Delete
else:
[147] Fix | Delete
match = magic_check.search(s)
[148] Fix | Delete
return match is not None
[149] Fix | Delete
[150] Fix | Delete
def _ishidden(path):
[151] Fix | Delete
return path[0] in ('.', b'.'[0])
[152] Fix | Delete
[153] Fix | Delete
def _isrecursive(pattern):
[154] Fix | Delete
if isinstance(pattern, bytes):
[155] Fix | Delete
return pattern == b'**'
[156] Fix | Delete
else:
[157] Fix | Delete
return pattern == '**'
[158] Fix | Delete
[159] Fix | Delete
def escape(pathname):
[160] Fix | Delete
"""Escape all special characters.
[161] Fix | Delete
"""
[162] Fix | Delete
# Escaping is done by wrapping any of "*?[" between square brackets.
[163] Fix | Delete
# Metacharacters do not work in the drive part and shouldn't be escaped.
[164] Fix | Delete
drive, pathname = os.path.splitdrive(pathname)
[165] Fix | Delete
if isinstance(pathname, bytes):
[166] Fix | Delete
pathname = magic_check_bytes.sub(br'[\1]', pathname)
[167] Fix | Delete
else:
[168] Fix | Delete
pathname = magic_check.sub(r'[\1]', pathname)
[169] Fix | Delete
return drive + pathname
[170] Fix | Delete
[171] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function