Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/AnonR/anonr.TX.../opt/imh-pyth.../lib/python2....
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
[11] Fix | Delete
import re
[12] Fix | Delete
[13] Fix | Delete
__all__ = ["filter", "fnmatch", "fnmatchcase", "translate"]
[14] Fix | Delete
[15] Fix | Delete
_cache = {}
[16] Fix | Delete
_MAXCACHE = 100
[17] Fix | Delete
[18] Fix | Delete
def _purge():
[19] Fix | Delete
"""Clear the pattern cache"""
[20] Fix | Delete
_cache.clear()
[21] Fix | Delete
[22] Fix | Delete
def fnmatch(name, pat):
[23] Fix | Delete
"""Test whether FILENAME matches PATTERN.
[24] Fix | Delete
[25] Fix | Delete
Patterns are Unix shell style:
[26] Fix | Delete
[27] Fix | Delete
* matches everything
[28] Fix | Delete
? matches any single character
[29] Fix | Delete
[seq] matches any character in seq
[30] Fix | Delete
[!seq] matches any char not in seq
[31] Fix | Delete
[32] Fix | Delete
An initial period in FILENAME is not special.
[33] Fix | Delete
Both FILENAME and PATTERN are first case-normalized
[34] Fix | Delete
if the operating system requires it.
[35] Fix | Delete
If you don't want this, use fnmatchcase(FILENAME, PATTERN).
[36] Fix | Delete
"""
[37] Fix | Delete
[38] Fix | Delete
import os
[39] Fix | Delete
name = os.path.normcase(name)
[40] Fix | Delete
pat = os.path.normcase(pat)
[41] Fix | Delete
return fnmatchcase(name, pat)
[42] Fix | Delete
[43] Fix | Delete
def filter(names, pat):
[44] Fix | Delete
"""Return the subset of the list NAMES that match PAT"""
[45] Fix | Delete
import os,posixpath
[46] Fix | Delete
result=[]
[47] Fix | Delete
pat=os.path.normcase(pat)
[48] Fix | Delete
try:
[49] Fix | Delete
re_pat = _cache[pat]
[50] Fix | Delete
except KeyError:
[51] Fix | Delete
res = translate(pat)
[52] Fix | Delete
if len(_cache) >= _MAXCACHE:
[53] Fix | Delete
_cache.clear()
[54] Fix | Delete
_cache[pat] = re_pat = re.compile(res)
[55] Fix | Delete
match = re_pat.match
[56] Fix | Delete
if os.path is posixpath:
[57] Fix | Delete
# normcase on posix is NOP. Optimize it away from the loop.
[58] Fix | Delete
for name in names:
[59] Fix | Delete
if match(name):
[60] Fix | Delete
result.append(name)
[61] Fix | Delete
else:
[62] Fix | Delete
for name in names:
[63] Fix | Delete
if match(os.path.normcase(name)):
[64] Fix | Delete
result.append(name)
[65] Fix | Delete
return result
[66] Fix | Delete
[67] Fix | Delete
def fnmatchcase(name, pat):
[68] Fix | Delete
"""Test whether FILENAME matches PATTERN, including case.
[69] Fix | Delete
[70] Fix | Delete
This is a version of fnmatch() which doesn't case-normalize
[71] Fix | Delete
its arguments.
[72] Fix | Delete
"""
[73] Fix | Delete
[74] Fix | Delete
try:
[75] Fix | Delete
re_pat = _cache[pat]
[76] Fix | Delete
except KeyError:
[77] Fix | Delete
res = translate(pat)
[78] Fix | Delete
if len(_cache) >= _MAXCACHE:
[79] Fix | Delete
_cache.clear()
[80] Fix | Delete
_cache[pat] = re_pat = re.compile(res)
[81] Fix | Delete
return re_pat.match(name) is not None
[82] Fix | Delete
[83] Fix | Delete
def translate(pat):
[84] Fix | Delete
"""Translate a shell PATTERN to a regular expression.
[85] Fix | Delete
[86] Fix | Delete
There is no way to quote meta-characters.
[87] Fix | Delete
"""
[88] Fix | Delete
[89] Fix | Delete
i, n = 0, len(pat)
[90] Fix | Delete
res = ''
[91] Fix | Delete
while i < n:
[92] Fix | Delete
c = pat[i]
[93] Fix | Delete
i = i+1
[94] Fix | Delete
if c == '*':
[95] Fix | Delete
res = res + '.*'
[96] Fix | Delete
elif c == '?':
[97] Fix | Delete
res = res + '.'
[98] Fix | Delete
elif c == '[':
[99] Fix | Delete
j = i
[100] Fix | Delete
if j < n and pat[j] == '!':
[101] Fix | Delete
j = j+1
[102] Fix | Delete
if j < n and pat[j] == ']':
[103] Fix | Delete
j = j+1
[104] Fix | Delete
while j < n and pat[j] != ']':
[105] Fix | Delete
j = j+1
[106] Fix | Delete
if j >= n:
[107] Fix | Delete
res = res + '\\['
[108] Fix | Delete
else:
[109] Fix | Delete
stuff = pat[i:j].replace('\\','\\\\')
[110] Fix | Delete
i = j+1
[111] Fix | Delete
if stuff[0] == '!':
[112] Fix | Delete
stuff = '^' + stuff[1:]
[113] Fix | Delete
elif stuff[0] == '^':
[114] Fix | Delete
stuff = '\\' + stuff
[115] Fix | Delete
res = '%s[%s]' % (res, stuff)
[116] Fix | Delete
else:
[117] Fix | Delete
res = res + re.escape(c)
[118] Fix | Delete
return res + '\Z(?ms)'
[119] Fix | Delete
[120] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function