blob: 726fbe556e8fcb85b23461601807c8d5c0d7673a [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"""
Brett Cannoncc143202010-07-23 16:22:25 +000012import os
13import posixpath
Guido van Rossum9694fca1997-10-22 21:00:49 +000014import re
Antoine Pitrou6fdb74f2010-08-13 16:26:40 +000015import functools
Guido van Rossum9694fca1997-10-22 21:00:49 +000016
Antoine Pitrou6fdb74f2010-08-13 16:26:40 +000017__all__ = ["filter", "fnmatch", "fnmatchcase", "translate"]
Brett Cannoncc143202010-07-23 16:22:25 +000018
Guido van Rossum762c39e1991-01-01 18:11:14 +000019def fnmatch(name, pat):
Tim Peters88869f92001-01-14 23:36:06 +000020 """Test whether FILENAME matches PATTERN.
21
22 Patterns are Unix shell style:
23
24 * matches everything
25 ? matches any single character
26 [seq] matches any character in seq
27 [!seq] matches any char not in seq
28
29 An initial period in FILENAME is not special.
30 Both FILENAME and PATTERN are first case-normalized
31 if the operating system requires it.
32 If you don't want this, use fnmatchcase(FILENAME, PATTERN).
33 """
Tim Peters88869f92001-01-14 23:36:06 +000034 name = os.path.normcase(name)
35 pat = os.path.normcase(pat)
36 return fnmatchcase(name, pat)
Guido van Rossum7e4b2de1995-01-27 02:41:45 +000037
Antoine Pitrou6fdb74f2010-08-13 16:26:40 +000038@functools.lru_cache(maxsize=250)
39def _compile_pattern(pat, is_bytes=False):
40 if is_bytes:
41 pat_str = str(pat, 'ISO-8859-1')
42 res_str = translate(pat_str)
43 res = bytes(res_str, 'ISO-8859-1')
44 else:
45 res = translate(pat)
46 return re.compile(res).match
Brett Cannoncc143202010-07-23 16:22:25 +000047
Martin v. Löwisb5d4d2a2001-06-06 06:24:38 +000048def filter(names, pat):
Brett Cannoncc143202010-07-23 16:22:25 +000049 """Return the subset of the list NAMES that match PAT."""
Guido van Rossumf0af3e32008-10-02 18:55:37 +000050 result = []
51 pat = os.path.normcase(pat)
Antoine Pitrou6fdb74f2010-08-13 16:26:40 +000052 match = _compile_pattern(pat, isinstance(pat, bytes))
Martin v. Löwisb5d4d2a2001-06-06 06:24:38 +000053 if os.path is posixpath:
54 # normcase on posix is NOP. Optimize it away from the loop.
55 for name in names:
56 if match(name):
57 result.append(name)
58 else:
59 for name in names:
60 if match(os.path.normcase(name)):
61 result.append(name)
62 return result
63
Guido van Rossum7e4b2de1995-01-27 02:41:45 +000064def fnmatchcase(name, pat):
Tim Peters88869f92001-01-14 23:36:06 +000065 """Test whether FILENAME matches PATTERN, including case.
66
67 This is a version of fnmatch() which doesn't case-normalize
68 its arguments.
69 """
Antoine Pitrou6fdb74f2010-08-13 16:26:40 +000070 match = _compile_pattern(pat, isinstance(pat, bytes))
Guido van Rossumf0af3e32008-10-02 18:55:37 +000071 return match(name) is not None
Guido van Rossum762c39e1991-01-01 18:11:14 +000072
Brett Cannoncc143202010-07-23 16:22:25 +000073
Guido van Rossum05e52191992-01-12 23:29:29 +000074def translate(pat):
Tim Peters88869f92001-01-14 23:36:06 +000075 """Translate a shell PATTERN to a regular expression.
76
77 There is no way to quote meta-characters.
78 """
79
80 i, n = 0, len(pat)
81 res = ''
82 while i < n:
83 c = pat[i]
84 i = i+1
85 if c == '*':
86 res = res + '.*'
87 elif c == '?':
88 res = res + '.'
89 elif c == '[':
90 j = i
91 if j < n and pat[j] == '!':
92 j = j+1
93 if j < n and pat[j] == ']':
94 j = j+1
95 while j < n and pat[j] != ']':
96 j = j+1
97 if j >= n:
98 res = res + '\\['
99 else:
Fred Drake46d9fda2001-03-21 18:05:48 +0000100 stuff = pat[i:j].replace('\\','\\\\')
Tim Peters88869f92001-01-14 23:36:06 +0000101 i = j+1
102 if stuff[0] == '!':
Fred Drake46d9fda2001-03-21 18:05:48 +0000103 stuff = '^' + stuff[1:]
104 elif stuff[0] == '^':
105 stuff = '\\' + stuff
106 res = '%s[%s]' % (res, stuff)
Tim Peters88869f92001-01-14 23:36:06 +0000107 else:
108 res = res + re.escape(c)
Gregory P. Smith01099702009-08-16 18:58:46 +0000109 return res + '\Z(?ms)'