diff options
Diffstat (limited to 'Lib/fnmatch.py')
-rw-r--r-- | Lib/fnmatch.py | 54 |
1 files changed, 45 insertions, 9 deletions
diff --git a/Lib/fnmatch.py b/Lib/fnmatch.py index 5c1bc71..9b31856 100644 --- a/Lib/fnmatch.py +++ b/Lib/fnmatch.py @@ -1,23 +1,59 @@ -# module 'fnmatch' -- filename matching with shell patterns -# This version translates the pattern to a regular expression -# and moreover caches the expressions. +"""Filename matching with shell patterns. -import os -import regex +fnmatch(FILENAME, PATTERN) matches according to the local convention. +fnmatchcase(FILENAME, PATTERN) always takes case in account. -cache = {} +The functions operate by translating the pattern into a regular +expression. They cache the compiled regular expressions for speed. + +The function translate(PATTERN) returns a regular expression +corresponding to PATTERN. (It does not compile it.) +""" + +_cache = {} 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 seq + + An initial period in FILENAME is not special. + Both FILENAME and PATTERN are first case-normalized + 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) - if not cache.has_key(pat): + return fnmatchcase(name, pat) + +def fnmatchcase(name, pat): + """Test wheter FILENAME matches PATTERN, including case. + + This is a version of fnmatch() which doesn't case-normalize + its arguments. + """ + + if not _cache.has_key(pat): res = translate(pat) + import regex save_syntax = regex.set_syntax(0) - cache[pat] = regex.compile(res) + _cache[pat] = regex.compile(res) save_syntax = regex.set_syntax(save_syntax) - return cache[pat].match(name) == len(name) + return _cache[pat].match(name) == len(name) def 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: |