blob: 30c0a922f229464317b3f6726597d5fe252cf380 [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.
Guido van Rossum762c39e1991-01-01 18:11:14 +000019
Guido van Rossum762c39e1991-01-01 18:11:14 +000020def fnmatch(name, pat):
Tim Peters88869f92001-01-14 23:36:06 +000021 """Test whether FILENAME matches PATTERN.
22
23 Patterns are Unix shell style:
24
25 * matches everything
26 ? matches any single character
27 [seq] matches any character in seq
28 [!seq] matches any char not in seq
29
30 An initial period in FILENAME is not special.
31 Both FILENAME and PATTERN are first case-normalized
32 if the operating system requires it.
33 If you don't want this, use fnmatchcase(FILENAME, PATTERN).
34 """
35
36 import os
37 name = os.path.normcase(name)
38 pat = os.path.normcase(pat)
39 return fnmatchcase(name, pat)
Guido van Rossum7e4b2de1995-01-27 02:41:45 +000040
Guido van Rossumf0af3e32008-10-02 18:55:37 +000041def _compile_pattern(pat):
Guido van Rossum3f2291f2008-10-03 16:38:30 +000042 cache = _cacheb if isinstance(pat, bytes) else _cache
43 regex = cache.get(pat)
Guido van Rossumf0af3e32008-10-02 18:55:37 +000044 if regex is None:
45 if isinstance(pat, bytes):
46 pat_str = str(pat, 'ISO-8859-1')
47 res_str = translate(pat_str)
48 res = bytes(res_str, 'ISO-8859-1')
49 else:
50 res = translate(pat)
Guido van Rossum3f2291f2008-10-03 16:38:30 +000051 cache[pat] = regex = re.compile(res)
Guido van Rossumf0af3e32008-10-02 18:55:37 +000052 return regex.match
53
Martin v. Löwisb5d4d2a2001-06-06 06:24:38 +000054def filter(names, pat):
55 """Return the subset of the list NAMES that match PAT"""
56 import os,posixpath
Guido van Rossumf0af3e32008-10-02 18:55:37 +000057 result = []
58 pat = os.path.normcase(pat)
59 match = _compile_pattern(pat)
Martin v. Löwisb5d4d2a2001-06-06 06:24:38 +000060 if os.path is posixpath:
61 # normcase on posix is NOP. Optimize it away from the loop.
62 for name in names:
63 if match(name):
64 result.append(name)
65 else:
66 for name in names:
67 if match(os.path.normcase(name)):
68 result.append(name)
69 return result
70
Guido van Rossum7e4b2de1995-01-27 02:41:45 +000071def fnmatchcase(name, pat):
Tim Peters88869f92001-01-14 23:36:06 +000072 """Test whether FILENAME matches PATTERN, including case.
73
74 This is a version of fnmatch() which doesn't case-normalize
75 its arguments.
76 """
77
Guido van Rossumf0af3e32008-10-02 18:55:37 +000078 match = _compile_pattern(pat)
79 return match(name) is not None
Guido van Rossum762c39e1991-01-01 18:11:14 +000080
Guido van Rossum05e52191992-01-12 23:29:29 +000081def translate(pat):
Tim Peters88869f92001-01-14 23:36:06 +000082 """Translate a shell PATTERN to a regular expression.
83
84 There is no way to quote meta-characters.
85 """
86
87 i, n = 0, len(pat)
88 res = ''
89 while i < n:
90 c = pat[i]
91 i = i+1
92 if c == '*':
93 res = res + '.*'
94 elif c == '?':
95 res = res + '.'
96 elif c == '[':
97 j = i
98 if j < n and pat[j] == '!':
99 j = j+1
100 if j < n and pat[j] == ']':
101 j = j+1
102 while j < n and pat[j] != ']':
103 j = j+1
104 if j >= n:
105 res = res + '\\['
106 else:
Fred Drake46d9fda2001-03-21 18:05:48 +0000107 stuff = pat[i:j].replace('\\','\\\\')
Tim Peters88869f92001-01-14 23:36:06 +0000108 i = j+1
109 if stuff[0] == '!':
Fred Drake46d9fda2001-03-21 18:05:48 +0000110 stuff = '^' + stuff[1:]
111 elif stuff[0] == '^':
112 stuff = '\\' + stuff
113 res = '%s[%s]' % (res, stuff)
Tim Peters88869f92001-01-14 23:36:06 +0000114 else:
115 res = res + re.escape(c)
Gregory P. Smith01099702009-08-16 18:58:46 +0000116 return res + '\Z(?ms)'