Guido van Rossum | 7e4b2de | 1995-01-27 02:41:45 +0000 | [diff] [blame] | 1 | """Filename matching with shell patterns. |
Guido van Rossum | 05e5219 | 1992-01-12 23:29:29 +0000 | [diff] [blame] | 2 | |
Guido van Rossum | 7e4b2de | 1995-01-27 02:41:45 +0000 | [diff] [blame] | 3 | fnmatch(FILENAME, PATTERN) matches according to the local convention. |
| 4 | fnmatchcase(FILENAME, PATTERN) always takes case in account. |
Guido van Rossum | 05e5219 | 1992-01-12 23:29:29 +0000 | [diff] [blame] | 5 | |
Guido van Rossum | 7e4b2de | 1995-01-27 02:41:45 +0000 | [diff] [blame] | 6 | The functions operate by translating the pattern into a regular |
| 7 | expression. They cache the compiled regular expressions for speed. |
| 8 | |
| 9 | The function translate(PATTERN) returns a regular expression |
| 10 | corresponding to PATTERN. (It does not compile it.) |
| 11 | """ |
| 12 | |
Guido van Rossum | 9694fca | 1997-10-22 21:00:49 +0000 | [diff] [blame] | 13 | import re |
| 14 | |
Raymond Hettinger | 4a4296e | 2003-07-13 16:06:26 +0000 | [diff] [blame] | 15 | __all__ = ["filter", "fnmatch","fnmatchcase","translate"] |
Skip Montanaro | eccd02a | 2001-01-20 23:34:12 +0000 | [diff] [blame] | 16 | |
Guido van Rossum | 7e4b2de | 1995-01-27 02:41:45 +0000 | [diff] [blame] | 17 | _cache = {} |
R. David Murray | bda5f2b | 2010-07-09 13:29:33 +0000 | [diff] [blame^] | 18 | _MAXCACHE = 100 |
Guido van Rossum | 762c39e | 1991-01-01 18:11:14 +0000 | [diff] [blame] | 19 | |
Guido van Rossum | 762c39e | 1991-01-01 18:11:14 +0000 | [diff] [blame] | 20 | def fnmatch(name, pat): |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 21 | """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 Rossum | 7e4b2de | 1995-01-27 02:41:45 +0000 | [diff] [blame] | 40 | |
Martin v. Löwis | b5d4d2a | 2001-06-06 06:24:38 +0000 | [diff] [blame] | 41 | def filter(names, pat): |
| 42 | """Return the subset of the list NAMES that match PAT""" |
| 43 | import os,posixpath |
| 44 | result=[] |
| 45 | pat=os.path.normcase(pat) |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 46 | if not pat in _cache: |
Martin v. Löwis | b5d4d2a | 2001-06-06 06:24:38 +0000 | [diff] [blame] | 47 | res = translate(pat) |
R. David Murray | bda5f2b | 2010-07-09 13:29:33 +0000 | [diff] [blame^] | 48 | if len(_cache) >= _MAXCACHE: |
| 49 | _cache.clear() |
Martin v. Löwis | b5d4d2a | 2001-06-06 06:24:38 +0000 | [diff] [blame] | 50 | _cache[pat] = re.compile(res) |
| 51 | match=_cache[pat].match |
| 52 | if os.path is posixpath: |
| 53 | # normcase on posix is NOP. Optimize it away from the loop. |
| 54 | for name in names: |
| 55 | if match(name): |
| 56 | result.append(name) |
| 57 | else: |
| 58 | for name in names: |
| 59 | if match(os.path.normcase(name)): |
| 60 | result.append(name) |
| 61 | return result |
| 62 | |
Guido van Rossum | 7e4b2de | 1995-01-27 02:41:45 +0000 | [diff] [blame] | 63 | def fnmatchcase(name, pat): |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 64 | """Test whether FILENAME matches PATTERN, including case. |
| 65 | |
| 66 | This is a version of fnmatch() which doesn't case-normalize |
| 67 | its arguments. |
| 68 | """ |
| 69 | |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 70 | if not pat in _cache: |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 71 | res = translate(pat) |
R. David Murray | bda5f2b | 2010-07-09 13:29:33 +0000 | [diff] [blame^] | 72 | if len(_cache) >= _MAXCACHE: |
| 73 | _cache.clear() |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 74 | _cache[pat] = re.compile(res) |
| 75 | return _cache[pat].match(name) is not None |
Guido van Rossum | 762c39e | 1991-01-01 18:11:14 +0000 | [diff] [blame] | 76 | |
Guido van Rossum | 05e5219 | 1992-01-12 23:29:29 +0000 | [diff] [blame] | 77 | def translate(pat): |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 78 | """Translate a shell PATTERN to a regular expression. |
| 79 | |
| 80 | There is no way to quote meta-characters. |
| 81 | """ |
| 82 | |
| 83 | i, n = 0, len(pat) |
| 84 | res = '' |
| 85 | while i < n: |
| 86 | c = pat[i] |
| 87 | i = i+1 |
| 88 | if c == '*': |
| 89 | res = res + '.*' |
| 90 | elif c == '?': |
| 91 | res = res + '.' |
| 92 | elif c == '[': |
| 93 | j = i |
| 94 | if j < n and pat[j] == '!': |
| 95 | j = j+1 |
| 96 | if j < n and pat[j] == ']': |
| 97 | j = j+1 |
| 98 | while j < n and pat[j] != ']': |
| 99 | j = j+1 |
| 100 | if j >= n: |
| 101 | res = res + '\\[' |
| 102 | else: |
Fred Drake | 46d9fda | 2001-03-21 18:05:48 +0000 | [diff] [blame] | 103 | stuff = pat[i:j].replace('\\','\\\\') |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 104 | i = j+1 |
| 105 | if stuff[0] == '!': |
Fred Drake | 46d9fda | 2001-03-21 18:05:48 +0000 | [diff] [blame] | 106 | stuff = '^' + stuff[1:] |
| 107 | elif stuff[0] == '^': |
| 108 | stuff = '\\' + stuff |
| 109 | res = '%s[%s]' % (res, stuff) |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 110 | else: |
| 111 | res = res + re.escape(c) |
Gregory P. Smith | 5662924 | 2009-11-01 20:33:31 +0000 | [diff] [blame] | 112 | return res + '\Z(?ms)' |