"""Filename matching with shell patterns.fnmatch(FILENAME, PATTERN) matches according to the local convention.fnmatchcase(FILENAME, PATTERN) always takes case in account.The functions operate by translating the pattern into a regularexpression. They cache the compiled regular expressions for speed.The function translate(PATTERN) returns a regular expressioncorresponding to PATTERN. (It does not compile it.)"""import osimport posixpathimport reimport functools__all__ = ["filter", "fnmatch", "fnmatchcase", "translate"]def fnmatch(name, pat):"""Test whether FILENAME matches PATTERN.Patterns are Unix shell style:* matches everything? matches any single character[seq] matches any character in seq[!seq] matches any char not in seqAn initial period in FILENAME is not special.Both FILENAME and PATTERN are first case-normalizedif the operating system requires it.If you don't want this, use fnmatchcase(FILENAME, PATTERN)."""name = os.path.normcase(name)pat = os.path.normcase(pat)return fnmatchcase(name, pat)@functools.lru_cache(maxsize=256, typed=True)def _compile_pattern(pat):if isinstance(pat, bytes):pat_str = str(pat, 'ISO-8859-1')res_str = translate(pat_str)res = bytes(res_str, 'ISO-8859-1')else:res = translate(pat)return re.compile(res).matchdef filter(names, pat):"""Return the subset of the list NAMES that match PAT."""result = []pat = os.path.normcase(pat)match = _compile_pattern(pat)if os.path is posixpath:# normcase on posix is NOP. Optimize it away from the loop.for name in names:if match(name):result.append(name)else:for name in names:if match(os.path.normcase(name)):result.append(name)return resultdef fnmatchcase(name, pat):"""Test whether FILENAME matches PATTERN, including case.This is a version of fnmatch() which doesn't case-normalizeits arguments."""match = _compile_pattern(pat)return match(name) is not Nonedef translate(pat):"""Translate a shell PATTERN to a regular expression.There is no way to quote meta-characters."""i, n = 0, len(pat)res = ''while i < n:c = pat[i]i = i+1if c == '*':res = res + '.*'elif c == '?':res = res + '.'elif c == '[':j = iif j < n and pat[j] == '!':j = j+1if j < n and pat[j] == ']':j = j+1while j < n and pat[j] != ']':j = j+1if j >= n:res = res + '\\['else:stuff = pat[i:j]if '--' not in stuff:stuff = stuff.replace('\\', r'\\')else:chunks = []k = i+2 if pat[i] == '!' else i+1while True:k = pat.find('-', k, j)if k < 0:breakchunks.append(pat[i:k])i = k+1k = k+3chunks.append(pat[i:j])# Escape backslashes and hyphens for set difference (--).# Hyphens that create ranges shouldn't be escaped.stuff = '-'.join(s.replace('\\', r'\\').replace('-', r'\-')for s in chunks)# Escape set operations (&&, ~~ and ||).stuff = re.sub(r'([&~|])', r'\\1円', stuff)i = j+1if stuff[0] == '!':stuff = '^' + stuff[1:]elif stuff[0] in ('^', '['):stuff = '\\' + stuffres = '%s[%s]' % (res, stuff)else:res = res + re.escape(c)return r'(?s:%s)\Z' % res
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。