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