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