Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/AnonR/anonr.TX.../opt/imh-pyth.../lib/python3....
File: fnmatch.py
"""Filename matching with shell patterns.
[0] Fix | Delete
[1] Fix | Delete
fnmatch(FILENAME, PATTERN) matches according to the local convention.
[2] Fix | Delete
fnmatchcase(FILENAME, PATTERN) always takes case in account.
[3] Fix | Delete
[4] Fix | Delete
The functions operate by translating the pattern into a regular
[5] Fix | Delete
expression. They cache the compiled regular expressions for speed.
[6] Fix | Delete
[7] Fix | Delete
The function translate(PATTERN) returns a regular expression
[8] Fix | Delete
corresponding to PATTERN. (It does not compile it.)
[9] Fix | Delete
"""
[10] Fix | Delete
import os
[11] Fix | Delete
import posixpath
[12] Fix | Delete
import re
[13] Fix | Delete
import functools
[14] Fix | Delete
[15] Fix | Delete
__all__ = ["filter", "fnmatch", "fnmatchcase", "translate"]
[16] Fix | Delete
[17] Fix | Delete
# Build a thread-safe incrementing counter to help create unique regexp group
[18] Fix | Delete
# names across calls.
[19] Fix | Delete
from itertools import count
[20] Fix | Delete
_nextgroupnum = count().__next__
[21] Fix | Delete
del count
[22] Fix | Delete
[23] Fix | Delete
def fnmatch(name, pat):
[24] Fix | Delete
"""Test whether FILENAME matches PATTERN.
[25] Fix | Delete
[26] Fix | Delete
Patterns are Unix shell style:
[27] Fix | Delete
[28] Fix | Delete
* matches everything
[29] Fix | Delete
? matches any single character
[30] Fix | Delete
[seq] matches any character in seq
[31] Fix | Delete
[!seq] matches any char not in seq
[32] Fix | Delete
[33] Fix | Delete
An initial period in FILENAME is not special.
[34] Fix | Delete
Both FILENAME and PATTERN are first case-normalized
[35] Fix | Delete
if the operating system requires it.
[36] Fix | Delete
If you don't want this, use fnmatchcase(FILENAME, PATTERN).
[37] Fix | Delete
"""
[38] Fix | Delete
name = os.path.normcase(name)
[39] Fix | Delete
pat = os.path.normcase(pat)
[40] Fix | Delete
return fnmatchcase(name, pat)
[41] Fix | Delete
[42] Fix | Delete
@functools.lru_cache(maxsize=256, typed=True)
[43] Fix | Delete
def _compile_pattern(pat):
[44] Fix | Delete
if isinstance(pat, bytes):
[45] Fix | Delete
pat_str = str(pat, 'ISO-8859-1')
[46] Fix | Delete
res_str = translate(pat_str)
[47] Fix | Delete
res = bytes(res_str, 'ISO-8859-1')
[48] Fix | Delete
else:
[49] Fix | Delete
res = translate(pat)
[50] Fix | Delete
return re.compile(res).match
[51] Fix | Delete
[52] Fix | Delete
def filter(names, pat):
[53] Fix | Delete
"""Construct a list from those elements of the iterable NAMES that match PAT."""
[54] Fix | Delete
result = []
[55] Fix | Delete
pat = os.path.normcase(pat)
[56] Fix | Delete
match = _compile_pattern(pat)
[57] Fix | Delete
if os.path is posixpath:
[58] Fix | Delete
# normcase on posix is NOP. Optimize it away from the loop.
[59] Fix | Delete
for name in names:
[60] Fix | Delete
if match(name):
[61] Fix | Delete
result.append(name)
[62] Fix | Delete
else:
[63] Fix | Delete
for name in names:
[64] Fix | Delete
if match(os.path.normcase(name)):
[65] Fix | Delete
result.append(name)
[66] Fix | Delete
return result
[67] Fix | Delete
[68] Fix | Delete
def fnmatchcase(name, pat):
[69] Fix | Delete
"""Test whether FILENAME matches PATTERN, including case.
[70] Fix | Delete
[71] Fix | Delete
This is a version of fnmatch() which doesn't case-normalize
[72] Fix | Delete
its arguments.
[73] Fix | Delete
"""
[74] Fix | Delete
match = _compile_pattern(pat)
[75] Fix | Delete
return match(name) is not None
[76] Fix | Delete
[77] Fix | Delete
[78] Fix | Delete
def translate(pat):
[79] Fix | Delete
"""Translate a shell PATTERN to a regular expression.
[80] Fix | Delete
[81] Fix | Delete
There is no way to quote meta-characters.
[82] Fix | Delete
"""
[83] Fix | Delete
[84] Fix | Delete
STAR = object()
[85] Fix | Delete
res = []
[86] Fix | Delete
add = res.append
[87] Fix | Delete
i, n = 0, len(pat)
[88] Fix | Delete
while i < n:
[89] Fix | Delete
c = pat[i]
[90] Fix | Delete
i = i+1
[91] Fix | Delete
if c == '*':
[92] Fix | Delete
# compress consecutive `*` into one
[93] Fix | Delete
if (not res) or res[-1] is not STAR:
[94] Fix | Delete
add(STAR)
[95] Fix | Delete
elif c == '?':
[96] Fix | Delete
add('.')
[97] Fix | Delete
elif c == '[':
[98] Fix | Delete
j = i
[99] Fix | Delete
if j < n and pat[j] == '!':
[100] Fix | Delete
j = j+1
[101] Fix | Delete
if j < n and pat[j] == ']':
[102] Fix | Delete
j = j+1
[103] Fix | Delete
while j < n and pat[j] != ']':
[104] Fix | Delete
j = j+1
[105] Fix | Delete
if j >= n:
[106] Fix | Delete
add('\\[')
[107] Fix | Delete
else:
[108] Fix | Delete
stuff = pat[i:j]
[109] Fix | Delete
if '--' not in stuff:
[110] Fix | Delete
stuff = stuff.replace('\\', r'\\')
[111] Fix | Delete
else:
[112] Fix | Delete
chunks = []
[113] Fix | Delete
k = i+2 if pat[i] == '!' else i+1
[114] Fix | Delete
while True:
[115] Fix | Delete
k = pat.find('-', k, j)
[116] Fix | Delete
if k < 0:
[117] Fix | Delete
break
[118] Fix | Delete
chunks.append(pat[i:k])
[119] Fix | Delete
i = k+1
[120] Fix | Delete
k = k+3
[121] Fix | Delete
chunks.append(pat[i:j])
[122] Fix | Delete
# Escape backslashes and hyphens for set difference (--).
[123] Fix | Delete
# Hyphens that create ranges shouldn't be escaped.
[124] Fix | Delete
stuff = '-'.join(s.replace('\\', r'\\').replace('-', r'\-')
[125] Fix | Delete
for s in chunks)
[126] Fix | Delete
# Escape set operations (&&, ~~ and ||).
[127] Fix | Delete
stuff = re.sub(r'([&~|])', r'\\\1', stuff)
[128] Fix | Delete
i = j+1
[129] Fix | Delete
if stuff[0] == '!':
[130] Fix | Delete
stuff = '^' + stuff[1:]
[131] Fix | Delete
elif stuff[0] in ('^', '['):
[132] Fix | Delete
stuff = '\\' + stuff
[133] Fix | Delete
add(f'[{stuff}]')
[134] Fix | Delete
else:
[135] Fix | Delete
add(re.escape(c))
[136] Fix | Delete
assert i == n
[137] Fix | Delete
[138] Fix | Delete
# Deal with STARs.
[139] Fix | Delete
inp = res
[140] Fix | Delete
res = []
[141] Fix | Delete
add = res.append
[142] Fix | Delete
i, n = 0, len(inp)
[143] Fix | Delete
# Fixed pieces at the start?
[144] Fix | Delete
while i < n and inp[i] is not STAR:
[145] Fix | Delete
add(inp[i])
[146] Fix | Delete
i += 1
[147] Fix | Delete
# Now deal with STAR fixed STAR fixed ...
[148] Fix | Delete
# For an interior `STAR fixed` pairing, we want to do a minimal
[149] Fix | Delete
# .*? match followed by `fixed`, with no possibility of backtracking.
[150] Fix | Delete
# We can't spell that directly, but can trick it into working by matching
[151] Fix | Delete
# .*?fixed
[152] Fix | Delete
# in a lookahead assertion, save the matched part in a group, then
[153] Fix | Delete
# consume that group via a backreference. If the overall match fails,
[154] Fix | Delete
# the lookahead assertion won't try alternatives. So the translation is:
[155] Fix | Delete
# (?=(?P<name>.*?fixed))(?P=name)
[156] Fix | Delete
# Group names are created as needed: g0, g1, g2, ...
[157] Fix | Delete
# The numbers are obtained from _nextgroupnum() to ensure they're unique
[158] Fix | Delete
# across calls and across threads. This is because people rely on the
[159] Fix | Delete
# undocumented ability to join multiple translate() results together via
[160] Fix | Delete
# "|" to build large regexps matching "one of many" shell patterns.
[161] Fix | Delete
while i < n:
[162] Fix | Delete
assert inp[i] is STAR
[163] Fix | Delete
i += 1
[164] Fix | Delete
if i == n:
[165] Fix | Delete
add(".*")
[166] Fix | Delete
break
[167] Fix | Delete
assert inp[i] is not STAR
[168] Fix | Delete
fixed = []
[169] Fix | Delete
while i < n and inp[i] is not STAR:
[170] Fix | Delete
fixed.append(inp[i])
[171] Fix | Delete
i += 1
[172] Fix | Delete
fixed = "".join(fixed)
[173] Fix | Delete
if i == n:
[174] Fix | Delete
add(".*")
[175] Fix | Delete
add(fixed)
[176] Fix | Delete
else:
[177] Fix | Delete
groupnum = _nextgroupnum()
[178] Fix | Delete
add(f"(?=(?P<g{groupnum}>.*?{fixed}))(?P=g{groupnum})")
[179] Fix | Delete
assert i == n
[180] Fix | Delete
res = "".join(res)
[181] Fix | Delete
return fr'(?s:{res})\Z'
[182] Fix | Delete
[183] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function