Make fnmatch be more PEP 8 compliant.
Partially closes issue 9356. Thanks to Brian Brazil for the patch.
This commit is contained in:
parent
8cb3619f86
commit
cc14320159
|
@ -9,20 +9,23 @@ expression. They cache the compiled regular expressions for speed.
|
||||||
The function translate(PATTERN) returns a regular expression
|
The function translate(PATTERN) returns a regular expression
|
||||||
corresponding to PATTERN. (It does not compile it.)
|
corresponding to PATTERN. (It does not compile it.)
|
||||||
"""
|
"""
|
||||||
|
import os
|
||||||
|
import posixpath
|
||||||
import re
|
import re
|
||||||
|
|
||||||
__all__ = ["filter", "fnmatch", "fnmatchcase", "purge", "translate"]
|
__all__ = ["filter", "fnmatch", "fnmatchcase", "purge", "translate"]
|
||||||
|
|
||||||
_cache = {} # Maps text patterns to compiled regexen.
|
_cache = {} # Maps text patterns to compiled regexen.
|
||||||
_cacheb = {} # Ditto for bytes patterns.
|
_cacheb = {} # Ditto for bytes patterns.
|
||||||
_MAXCACHE = 100 # Maximum size of caches
|
_MAXCACHE = 100 # Maximum size of caches.
|
||||||
|
|
||||||
|
|
||||||
def purge():
|
def purge():
|
||||||
"""Clear the pattern cache"""
|
"""Clear the pattern cache."""
|
||||||
_cache.clear()
|
_cache.clear()
|
||||||
_cacheb.clear()
|
_cacheb.clear()
|
||||||
|
|
||||||
|
|
||||||
def fnmatch(name, pat):
|
def fnmatch(name, pat):
|
||||||
"""Test whether FILENAME matches PATTERN.
|
"""Test whether FILENAME matches PATTERN.
|
||||||
|
|
||||||
|
@ -38,12 +41,11 @@ def fnmatch(name, pat):
|
||||||
if the operating system requires it.
|
if the operating system requires it.
|
||||||
If you don't want this, use fnmatchcase(FILENAME, PATTERN).
|
If you don't want this, use fnmatchcase(FILENAME, PATTERN).
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import os
|
|
||||||
name = os.path.normcase(name)
|
name = os.path.normcase(name)
|
||||||
pat = os.path.normcase(pat)
|
pat = os.path.normcase(pat)
|
||||||
return fnmatchcase(name, pat)
|
return fnmatchcase(name, pat)
|
||||||
|
|
||||||
|
|
||||||
def _compile_pattern(pat):
|
def _compile_pattern(pat):
|
||||||
cache = _cacheb if isinstance(pat, bytes) else _cache
|
cache = _cacheb if isinstance(pat, bytes) else _cache
|
||||||
regex = cache.get(pat)
|
regex = cache.get(pat)
|
||||||
|
@ -59,9 +61,9 @@ def _compile_pattern(pat):
|
||||||
cache[pat] = regex = re.compile(res)
|
cache[pat] = regex = re.compile(res)
|
||||||
return regex.match
|
return regex.match
|
||||||
|
|
||||||
|
|
||||||
def filter(names, pat):
|
def filter(names, pat):
|
||||||
"""Return the subset of the list NAMES that match PAT"""
|
"""Return the subset of the list NAMES that match PAT."""
|
||||||
import os,posixpath
|
|
||||||
result = []
|
result = []
|
||||||
pat = os.path.normcase(pat)
|
pat = os.path.normcase(pat)
|
||||||
match = _compile_pattern(pat)
|
match = _compile_pattern(pat)
|
||||||
|
@ -76,16 +78,17 @@ def filter(names, pat):
|
||||||
result.append(name)
|
result.append(name)
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
def fnmatchcase(name, pat):
|
def fnmatchcase(name, pat):
|
||||||
"""Test whether FILENAME matches PATTERN, including case.
|
"""Test whether FILENAME matches PATTERN, including case.
|
||||||
|
|
||||||
This is a version of fnmatch() which doesn't case-normalize
|
This is a version of fnmatch() which doesn't case-normalize
|
||||||
its arguments.
|
its arguments.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
match = _compile_pattern(pat)
|
match = _compile_pattern(pat)
|
||||||
return match(name) is not None
|
return match(name) is not None
|
||||||
|
|
||||||
|
|
||||||
def translate(pat):
|
def translate(pat):
|
||||||
"""Translate a shell PATTERN to a regular expression.
|
"""Translate a shell PATTERN to a regular expression.
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue