The function translate(PATTERN) returns a regular expression
corresponding to PATTERN. (It does not compile it.)
"""
-
+import os
+import posixpath
import re
__all__ = ["filter", "fnmatch", "fnmatchcase", "purge", "translate"]
_cache = {} # Maps text patterns to compiled regexen.
_cacheb = {} # Ditto for bytes patterns.
-_MAXCACHE = 100 # Maximum size of caches
+_MAXCACHE = 100 # Maximum size of caches.
+
def purge():
- """Clear the pattern cache"""
+ """Clear the pattern cache."""
_cache.clear()
_cacheb.clear()
+
def fnmatch(name, pat):
"""Test whether FILENAME matches PATTERN.
if the operating system requires it.
If you don't want this, use fnmatchcase(FILENAME, PATTERN).
"""
-
- import os
name = os.path.normcase(name)
pat = os.path.normcase(pat)
return fnmatchcase(name, pat)
+
def _compile_pattern(pat):
cache = _cacheb if isinstance(pat, bytes) else _cache
regex = cache.get(pat)
cache[pat] = regex = re.compile(res)
return regex.match
+
def filter(names, pat):
- """Return the subset of the list NAMES that match PAT"""
- import os,posixpath
+ """Return the subset of the list NAMES that match PAT."""
result = []
pat = os.path.normcase(pat)
match = _compile_pattern(pat)
result.append(name)
return result
+
def fnmatchcase(name, pat):
"""Test whether FILENAME matches PATTERN, including case.
This is a version of fnmatch() which doesn't case-normalize
its arguments.
"""
-
match = _compile_pattern(pat)
return match(name) is not None
+
def translate(pat):
"""Translate a shell PATTERN to a regular expression.