| LEFT | RIGHT |
|---|---|
| 1 """Filename matching with shell patterns. | 1 """Filename matching with shell patterns. |
|
GvR
2008/09/30 18:10:41
Please complete do your TODO tasks:
- support non-
| |
| 2 | 2 |
| 3 fnmatch(FILENAME, PATTERN) matches according to the local convention. | 3 fnmatch(FILENAME, PATTERN) matches according to the local convention. |
| 4 fnmatchcase(FILENAME, PATTERN) always takes case in account. | 4 fnmatchcase(FILENAME, PATTERN) always takes case in account. |
| 5 | 5 |
| 6 The functions operate by translating the pattern into a regular | 6 The functions operate by translating the pattern into a regular |
| 7 expression. They cache the compiled regular expressions for speed. | 7 expression. They cache the compiled regular expressions for speed. |
| 8 | 8 |
| 9 The function translate(PATTERN) returns a regular expression | 9 The function translate(PATTERN) returns a regular expression |
| 10 corresponding to PATTERN. (It does not compile it.) | 10 corresponding to PATTERN. (It does not compile it.) |
| 11 """ | 11 """ |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 30 Both FILENAME and PATTERN are first case-normalized | 30 Both FILENAME and PATTERN are first case-normalized |
| 31 if the operating system requires it. | 31 if the operating system requires it. |
| 32 If you don't want this, use fnmatchcase(FILENAME, PATTERN). | 32 If you don't want this, use fnmatchcase(FILENAME, PATTERN). |
| 33 """ | 33 """ |
| 34 | 34 |
| 35 import os | 35 import os |
| 36 name = os.path.normcase(name) | 36 name = os.path.normcase(name) |
| 37 pat = os.path.normcase(pat) | 37 pat = os.path.normcase(pat) |
| 38 return fnmatchcase(name, pat) | 38 return fnmatchcase(name, pat) |
| 39 | 39 |
| 40 def _compile_pattern(pat): | |
| 41 regex = _cache.get(pat) | |
| 42 if regex is None: | |
| 43 if isinstance(pat, bytes): | |
| 44 pat_str = str(pat, 'ISO-8859-1') | |
| 45 res_str = translate(pat_str) | |
| 46 res = bytes(res_str, 'ISO-8859-1') | |
| 47 else: | |
| 48 res = translate(pat) | |
| 49 _cache[pat] = regex = re.compile(res) | |
| 50 return regex.match | |
| 51 | |
| 40 def filter(names, pat): | 52 def filter(names, pat): |
| 41 """Return the subset of the list NAMES that match PAT""" | 53 """Return the subset of the list NAMES that match PAT""" |
| 42 import os,posixpath | 54 import os,posixpath |
| 43 result=[] | 55 result = [] |
| 44 pat=os.path.normcase(pat) | 56 pat = os.path.normcase(pat) |
| 45 if not pat in _cache: | 57 match = _compile_pattern(pat) |
| 46 if isinstance(pat, bytes): | |
|
GvR
2008/09/30 18:10:41
I wish we could also allow bytearray, without havi
| |
| 47 pat_str = str(pat, "ASCII") | |
|
GvR
2008/09/30 18:10:41
I'd use 'ASCII' here and below, matching the quoti
| |
| 48 res_str = translate(pat_str) | |
| 49 res = res_str.encode("ASCII") | |
| 50 else: | |
| 51 res = translate(pat) | |
| 52 _cache[pat] = re.compile(res) | |
| 53 match=_cache[pat].match | |
| 54 if os.path is posixpath: | 58 if os.path is posixpath: |
| 55 # normcase on posix is NOP. Optimize it away from the loop. | 59 # normcase on posix is NOP. Optimize it away from the loop. |
| 56 for name in names: | 60 for name in names: |
| 57 if match(name): | 61 if match(name): |
| 58 result.append(name) | 62 result.append(name) |
| 59 else: | 63 else: |
| 60 for name in names: | 64 for name in names: |
| 61 if match(os.path.normcase(name)): | 65 if match(os.path.normcase(name)): |
| 62 result.append(name) | 66 result.append(name) |
| 63 return result | 67 return result |
| 64 | 68 |
| 65 def fnmatchcase(name, pat): | 69 def fnmatchcase(name, pat): |
| 66 """Test whether FILENAME matches PATTERN, including case. | 70 """Test whether FILENAME matches PATTERN, including case. |
| 67 | 71 |
| 68 This is a version of fnmatch() which doesn't case-normalize | 72 This is a version of fnmatch() which doesn't case-normalize |
| 69 its arguments. | 73 its arguments. |
| 70 """ | 74 """ |
| 71 | 75 |
| 72 if not pat in _cache: | 76 match = _compile_pattern(pat) |
| 73 res = translate(pat) | 77 return match(name) is not None |
| 74 _cache[pat] = re.compile(res) | |
| 75 return _cache[pat].match(name) is not None | |
| 76 | 78 |
| 77 def translate(pat): | 79 def translate(pat): |
| 78 """Translate a shell PATTERN to a regular expression. | 80 """Translate a shell PATTERN to a regular expression. |
| 79 | 81 |
| 80 There is no way to quote meta-characters. | 82 There is no way to quote meta-characters. |
| 81 """ | 83 """ |
| 82 | 84 |
| 83 i, n = 0, len(pat) | 85 i, n = 0, len(pat) |
| 84 res = '' | 86 res = '' |
| 85 while i < n: | 87 while i < n: |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 103 stuff = pat[i:j].replace('\\','\\\\') | 105 stuff = pat[i:j].replace('\\','\\\\') |
| 104 i = j+1 | 106 i = j+1 |
| 105 if stuff[0] == '!': | 107 if stuff[0] == '!': |
| 106 stuff = '^' + stuff[1:] | 108 stuff = '^' + stuff[1:] |
| 107 elif stuff[0] == '^': | 109 elif stuff[0] == '^': |
| 108 stuff = '\\' + stuff | 110 stuff = '\\' + stuff |
| 109 res = '%s[%s]' % (res, stuff) | 111 res = '%s[%s]' % (res, stuff) |
| 110 else: | 112 else: |
| 111 res = res + re.escape(c) | 113 res = res + re.escape(c) |
| 112 return res + "$" | 114 return res + "$" |
| LEFT | RIGHT |