blob: 26ae4ccb14d70533fa13a69b7d38ad82a1786da1 [file] [log] [blame]
Guido van Rossum7e4b2de1995-01-27 02:41:45 +00001"""Filename matching with shell patterns.
Guido van Rossum05e52191992-01-12 23:29:29 +00002
Guido van Rossum7e4b2de1995-01-27 02:41:45 +00003fnmatch(FILENAME, PATTERN) matches according to the local convention.
4fnmatchcase(FILENAME, PATTERN) always takes case in account.
Guido van Rossum05e52191992-01-12 23:29:29 +00005
Guido van Rossum7e4b2de1995-01-27 02:41:45 +00006The functions operate by translating the pattern into a regular
7expression. They cache the compiled regular expressions for speed.
8
9The function translate(PATTERN) returns a regular expression
10corresponding to PATTERN. (It does not compile it.)
11"""
12
Guido van Rossum9694fca1997-10-22 21:00:49 +000013import re
14
Raymond Hettinger4a4296e2003-07-13 16:06:26 +000015__all__ = ["filter", "fnmatch","fnmatchcase","translate"]
Skip Montanaroeccd02a2001-01-20 23:34:12 +000016
Guido van Rossum3f2291f2008-10-03 16:38:30 +000017_cache = {} # Maps text patterns to compiled regexen.
18_cacheb = {} # Ditto for bytes patterns.
R. David Murrayead883a2010-07-09 13:16:26 +000019_MAXCACHE = 100 # Maximum size of caches
Guido van Rossum762c39e1991-01-01 18:11:14 +000020
Guido van Rossum762c39e1991-01-01 18:11:14 +000021def fnmatch(name, pat):
Tim Peters88869f92001-01-14 23:36:06 +000022 """Test whether FILENAME matches PATTERN.
23
24 Patterns are Unix shell style:
25
26 * matches everything
27 ? matches any single character
28 [seq] matches any character in seq
29 [!seq] matches any char not in seq
30
31 An initial period in FILENAME is not special.
32 Both FILENAME and PATTERN are first case-normalized
33 if the operating system requires it.
34 If you don't want this, use fnmatchcase(FILENAME, PATTERN).
35 """
36
37 import os
38 name = os.path.normcase(name)
39 pat = os.path.normcase(pat)
40 return fnmatchcase(name, pat)
Guido van Rossum7e4b2de1995-01-27 02:41:45 +000041
Guido van Rossumf0af3e32008-10-02 18:55:37 +000042def _compile_pattern(pat):
Guido van Rossum3f2291f2008-10-03 16:38:30 +000043 cache = _cacheb if isinstance(pat, bytes) else _cache
44 regex = cache.get(pat)
Guido van Rossumf0af3e32008-10-02 18:55:37 +000045 if regex is None:
46 if isinstance(pat, bytes):
47 pat_str = str(pat, 'ISO-8859-1')
48 res_str = translate(pat_str)
49 res = bytes(res_str, 'ISO-8859-1')
50 else:
51 res = translate(pat)
R. David Murrayead883a2010-07-09 13:16:26 +000052 if len(cache) >= _MAXCACHE:
53 cache.clear()
Guido van Rossum3f2291f2008-10-03 16:38:30 +000054 cache[pat] = regex = re.compile(res)
Guido van Rossumf0af3e32008-10-02 18:55:37 +000055 return regex.match
56
Martin v. Löwisb5d4d2a2001-06-06 06:24:38 +000057def filter(names, pat):
58 """Return the subset of the list NAMES that match PAT"""
59 import os,posixpath
Guido van Rossumf0af3e32008-10-02 18:55:37 +000060 result = []
61 pat = os.path.normcase(pat)
62 match = _compile_pattern(pat)
Martin v. Löwisb5d4d2a2001-06-06 06:24:38 +000063 if os.path is posixpath:
64 # normcase on posix is NOP. Optimize it away from the loop.
65 for name in names:
66 if match(name):
67 result.append(name)
68 else:
69 for name in names:
70 if match(os.path.normcase(name)):
71 result.append(name)
72 return result
73
Guido van Rossum7e4b2de1995-01-27 02:41:45 +000074def fnmatchcase(name, pat):
Tim Peters88869f92001-01-14 23:36:06 +000075 """Test whether FILENAME matches PATTERN, including case.
76
77 This is a version of fnmatch() which doesn't case-normalize
78 its arguments.
79 """
80
Guido van Rossumf0af3e32008-10-02 18:55:37 +000081 match = _compile_pattern(pat)
82 return match(name) is not None
Guido van Rossum762c39e1991-01-01 18:11:14 +000083
Guido van Rossum05e52191992-01-12 23:29:29 +000084def translate(pat):
Tim Peters88869f92001-01-14 23:36:06 +000085 """Translate a shell PATTERN to a regular expression.
86
87 There is no way to quote meta-characters.
88 """
89
90 i, n = 0, len(pat)
91 res = ''
92 while i < n:
93 c = pat[i]
94 i = i+1
95 if c == '*':
96 res = res + '.*'
97 elif c == '?':
98 res = res + '.'
99 elif c == '[':
100 j = i
101 if j < n and pat[j] == '!':
102 j = j+1
103 if j < n and pat[j] == ']':
104 j = j+1
105 while j < n and pat[j] != ']':
106 j = j+1
107 if j >= n:
108 res = res + '\\['
109 else:
Fred Drake46d9fda2001-03-21 18:05:48 +0000110 stuff = pat[i:j].replace('\\','\\\\')
Tim Peters88869f92001-01-14 23:36:06 +0000111 i = j+1
112 if stuff[0] == '!':
Fred Drake46d9fda2001-03-21 18:05:48 +0000113 stuff = '^' + stuff[1:]
114 elif stuff[0] == '^':
115 stuff = '\\' + stuff
116 res = '%s[%s]' % (res, stuff)
Tim Peters88869f92001-01-14 23:36:06 +0000117 else:
118 res = res + re.escape(c)
Gregory P. Smith6c4a7252009-11-01 20:36:24 +0000119 return res + '\Z(?ms)'