| OLD | NEW |
|---|---|
| 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 24 matching lines...) Expand all Loading... | |
| 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 filter(names, pat): | 40 def filter(names, pat): |
| 41 """Return the subset of the list NAMES that match PAT""" | 41 """Return the subset of the list NAMES that match PAT""" |
| 42 import os,posixpath | 42 import os,posixpath |
| 43 result=[] | 43 result=[] |
| 44 pat=os.path.normcase(pat) | 44 pat=os.path.normcase(pat) |
| 45 if not pat in _cache: | 45 if not pat in _cache: |
| 46 res = translate(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) | |
| 47 _cache[pat] = re.compile(res) | 52 _cache[pat] = re.compile(res) |
| 48 match=_cache[pat].match | 53 match=_cache[pat].match |
| 49 if os.path is posixpath: | 54 if os.path is posixpath: |
| 50 # normcase on posix is NOP. Optimize it away from the loop. | 55 # normcase on posix is NOP. Optimize it away from the loop. |
| 51 for name in names: | 56 for name in names: |
| 52 if match(name): | 57 if match(name): |
| 53 result.append(name) | 58 result.append(name) |
| 54 else: | 59 else: |
| 55 for name in names: | 60 for name in names: |
| 56 if match(os.path.normcase(name)): | 61 if match(os.path.normcase(name)): |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 98 stuff = pat[i:j].replace('\\','\\\\') | 103 stuff = pat[i:j].replace('\\','\\\\') |
| 99 i = j+1 | 104 i = j+1 |
| 100 if stuff[0] == '!': | 105 if stuff[0] == '!': |
| 101 stuff = '^' + stuff[1:] | 106 stuff = '^' + stuff[1:] |
| 102 elif stuff[0] == '^': | 107 elif stuff[0] == '^': |
| 103 stuff = '\\' + stuff | 108 stuff = '\\' + stuff |
| 104 res = '%s[%s]' % (res, stuff) | 109 res = '%s[%s]' % (res, stuff) |
| 105 else: | 110 else: |
| 106 res = res + re.escape(c) | 111 res = res + re.escape(c) |
| 107 return res + "$" | 112 return res + "$" |
| OLD | NEW |