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 | |
R. David Murray | 0425a8e | 2010-07-10 13:52:13 +0000 | [diff] [blame] | 15 | __all__ = ["filter", "fnmatch", "fnmatchcase", "purge", "translate"] |
Skip Montanaro | eccd02a | 2001-01-20 23:34:12 +0000 | [diff] [blame] | 16 | |
Guido van Rossum | 3f2291f | 2008-10-03 16:38:30 +0000 | [diff] [blame] | 17 | _cache = {} # Maps text patterns to compiled regexen. |
| 18 | _cacheb = {} # Ditto for bytes patterns. |
R. David Murray | ea340a3 | 2010-07-09 12:23:21 +0000 | [diff] [blame] | 19 | _MAXCACHE = 100 # Maximum size of caches |
Guido van Rossum | 762c39e | 1991-01-01 18:11:14 +0000 | [diff] [blame] | 20 | |
R. David Murray | 0425a8e | 2010-07-10 13:52:13 +0000 | [diff] [blame] | 21 | def purge(): |
| 22 | """Clear the pattern cache""" |
| 23 | _cache.clear() |
| 24 | _cacheb.clear() |
| 25 | |
Guido van Rossum | 762c39e | 1991-01-01 18:11:14 +0000 | [diff] [blame] | 26 | def fnmatch(name, pat): |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 27 | """Test whether FILENAME matches PATTERN. |
| 28 | |
| 29 | Patterns are Unix shell style: |
| 30 | |
| 31 | * matches everything |
| 32 | ? matches any single character |
| 33 | [seq] matches any character in seq |
| 34 | [!seq] matches any char not in seq |
| 35 | |
| 36 | An initial period in FILENAME is not special. |
| 37 | Both FILENAME and PATTERN are first case-normalized |
| 38 | if the operating system requires it. |
| 39 | If you don't want this, use fnmatchcase(FILENAME, PATTERN). |
| 40 | """ |
| 41 | |
| 42 | import os |
| 43 | name = os.path.normcase(name) |
| 44 | pat = os.path.normcase(pat) |
| 45 | return fnmatchcase(name, pat) |
Guido van Rossum | 7e4b2de | 1995-01-27 02:41:45 +0000 | [diff] [blame] | 46 | |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 47 | def _compile_pattern(pat): |
Guido van Rossum | 3f2291f | 2008-10-03 16:38:30 +0000 | [diff] [blame] | 48 | cache = _cacheb if isinstance(pat, bytes) else _cache |
| 49 | regex = cache.get(pat) |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 50 | if regex is None: |
| 51 | if isinstance(pat, bytes): |
| 52 | pat_str = str(pat, 'ISO-8859-1') |
| 53 | res_str = translate(pat_str) |
| 54 | res = bytes(res_str, 'ISO-8859-1') |
| 55 | else: |
| 56 | res = translate(pat) |
R. David Murray | ea340a3 | 2010-07-09 12:23:21 +0000 | [diff] [blame] | 57 | if len(cache) >= _MAXCACHE: |
| 58 | cache.clear() |
Guido van Rossum | 3f2291f | 2008-10-03 16:38:30 +0000 | [diff] [blame] | 59 | cache[pat] = regex = re.compile(res) |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 60 | return regex.match |
| 61 | |
Martin v. Löwis | b5d4d2a | 2001-06-06 06:24:38 +0000 | [diff] [blame] | 62 | def filter(names, pat): |
| 63 | """Return the subset of the list NAMES that match PAT""" |
| 64 | import os,posixpath |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 65 | result = [] |
| 66 | pat = os.path.normcase(pat) |
| 67 | match = _compile_pattern(pat) |
Martin v. Löwis | b5d4d2a | 2001-06-06 06:24:38 +0000 | [diff] [blame] | 68 | if os.path is posixpath: |
| 69 | # normcase on posix is NOP. Optimize it away from the loop. |
| 70 | for name in names: |
| 71 | if match(name): |
| 72 | result.append(name) |
| 73 | else: |
| 74 | for name in names: |
| 75 | if match(os.path.normcase(name)): |
| 76 | result.append(name) |
| 77 | return result |
| 78 | |
Guido van Rossum | 7e4b2de | 1995-01-27 02:41:45 +0000 | [diff] [blame] | 79 | def fnmatchcase(name, pat): |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 80 | """Test whether FILENAME matches PATTERN, including case. |
| 81 | |
| 82 | This is a version of fnmatch() which doesn't case-normalize |
| 83 | its arguments. |
| 84 | """ |
| 85 | |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 86 | match = _compile_pattern(pat) |
| 87 | return match(name) is not None |
Guido van Rossum | 762c39e | 1991-01-01 18:11:14 +0000 | [diff] [blame] | 88 | |
Guido van Rossum | 05e5219 | 1992-01-12 23:29:29 +0000 | [diff] [blame] | 89 | def translate(pat): |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 90 | """Translate a shell PATTERN to a regular expression. |
| 91 | |
| 92 | There is no way to quote meta-characters. |
| 93 | """ |
| 94 | |
| 95 | i, n = 0, len(pat) |
| 96 | res = '' |
| 97 | while i < n: |
| 98 | c = pat[i] |
| 99 | i = i+1 |
| 100 | if c == '*': |
| 101 | res = res + '.*' |
| 102 | elif c == '?': |
| 103 | res = res + '.' |
| 104 | elif c == '[': |
| 105 | j = i |
| 106 | if j < n and pat[j] == '!': |
| 107 | j = j+1 |
| 108 | if j < n and pat[j] == ']': |
| 109 | j = j+1 |
| 110 | while j < n and pat[j] != ']': |
| 111 | j = j+1 |
| 112 | if j >= n: |
| 113 | res = res + '\\[' |
| 114 | else: |
Fred Drake | 46d9fda | 2001-03-21 18:05:48 +0000 | [diff] [blame] | 115 | stuff = pat[i:j].replace('\\','\\\\') |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 116 | i = j+1 |
| 117 | if stuff[0] == '!': |
Fred Drake | 46d9fda | 2001-03-21 18:05:48 +0000 | [diff] [blame] | 118 | stuff = '^' + stuff[1:] |
| 119 | elif stuff[0] == '^': |
| 120 | stuff = '\\' + stuff |
| 121 | res = '%s[%s]' % (res, stuff) |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 122 | else: |
| 123 | res = res + re.escape(c) |
Gregory P. Smith | 0109970 | 2009-08-16 18:58:46 +0000 | [diff] [blame] | 124 | return res + '\Z(?ms)' |