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 | """ |
Brett Cannon | cc14320 | 2010-07-23 16:22:25 +0000 | [diff] [blame] | 12 | import os |
| 13 | import posixpath |
Guido van Rossum | 9694fca | 1997-10-22 21:00:49 +0000 | [diff] [blame] | 14 | import re |
Antoine Pitrou | 6fdb74f | 2010-08-13 16:26:40 +0000 | [diff] [blame] | 15 | import functools |
Guido van Rossum | 9694fca | 1997-10-22 21:00:49 +0000 | [diff] [blame] | 16 | |
Antoine Pitrou | 6fdb74f | 2010-08-13 16:26:40 +0000 | [diff] [blame] | 17 | __all__ = ["filter", "fnmatch", "fnmatchcase", "translate"] |
Brett Cannon | cc14320 | 2010-07-23 16:22:25 +0000 | [diff] [blame] | 18 | |
Guido van Rossum | 762c39e | 1991-01-01 18:11:14 +0000 | [diff] [blame] | 19 | def fnmatch(name, pat): |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 20 | """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 Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 34 | name = os.path.normcase(name) |
| 35 | pat = os.path.normcase(pat) |
| 36 | return fnmatchcase(name, pat) |
Guido van Rossum | 7e4b2de | 1995-01-27 02:41:45 +0000 | [diff] [blame] | 37 | |
Raymond Hettinger | d0dbb20 | 2013-02-17 01:33:37 -0800 | [diff] [blame] | 38 | @functools.lru_cache(maxsize=256, typed=True) |
Raymond Hettinger | db84803 | 2011-10-20 09:22:10 -0700 | [diff] [blame] | 39 | def _compile_pattern(pat): |
| 40 | if isinstance(pat, bytes): |
Antoine Pitrou | 6fdb74f | 2010-08-13 16:26:40 +0000 | [diff] [blame] | 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 Cannon | cc14320 | 2010-07-23 16:22:25 +0000 | [diff] [blame] | 47 | |
Martin v. Löwis | b5d4d2a | 2001-06-06 06:24:38 +0000 | [diff] [blame] | 48 | def filter(names, pat): |
Brett Cannon | cc14320 | 2010-07-23 16:22:25 +0000 | [diff] [blame] | 49 | """Return the subset of the list NAMES that match PAT.""" |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 50 | result = [] |
| 51 | pat = os.path.normcase(pat) |
Raymond Hettinger | db84803 | 2011-10-20 09:22:10 -0700 | [diff] [blame] | 52 | match = _compile_pattern(pat) |
Martin v. Löwis | b5d4d2a | 2001-06-06 06:24:38 +0000 | [diff] [blame] | 53 | 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 Rossum | 7e4b2de | 1995-01-27 02:41:45 +0000 | [diff] [blame] | 64 | def fnmatchcase(name, pat): |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 65 | """Test whether FILENAME matches PATTERN, including case. |
| 66 | |
| 67 | This is a version of fnmatch() which doesn't case-normalize |
| 68 | its arguments. |
| 69 | """ |
Raymond Hettinger | db84803 | 2011-10-20 09:22:10 -0700 | [diff] [blame] | 70 | match = _compile_pattern(pat) |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 71 | return match(name) is not None |
Guido van Rossum | 762c39e | 1991-01-01 18:11:14 +0000 | [diff] [blame] | 72 | |
Brett Cannon | cc14320 | 2010-07-23 16:22:25 +0000 | [diff] [blame] | 73 | |
Guido van Rossum | 05e5219 | 1992-01-12 23:29:29 +0000 | [diff] [blame] | 74 | def translate(pat): |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 75 | """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 Drake | 46d9fda | 2001-03-21 18:05:48 +0000 | [diff] [blame] | 100 | stuff = pat[i:j].replace('\\','\\\\') |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 101 | i = j+1 |
| 102 | if stuff[0] == '!': |
Fred Drake | 46d9fda | 2001-03-21 18:05:48 +0000 | [diff] [blame] | 103 | stuff = '^' + stuff[1:] |
| 104 | elif stuff[0] == '^': |
| 105 | stuff = '\\' + stuff |
| 106 | res = '%s[%s]' % (res, stuff) |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 107 | else: |
| 108 | res = res + re.escape(c) |
Gregory P. Smith | 0109970 | 2009-08-16 18:58:46 +0000 | [diff] [blame] | 109 | return res + '\Z(?ms)' |