| OLD | NEW |
|---|---|
| 1 """Filename globbing utility.""" | 1 """Filename globbing utility.""" |
| 2 | 2 |
| 3 import sys | 3 import sys |
| 4 import os | 4 import os |
| 5 import re | 5 import re |
| 6 import fnmatch | 6 import fnmatch |
| 7 | 7 |
| 8 __all__ = ["glob", "iglob"] | 8 __all__ = ["glob", "iglob"] |
| 9 | 9 |
| 10 def glob(pathname): | 10 def glob(pathname): |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 43 yield os.path.join(dirname, name) | 43 yield os.path.join(dirname, name) |
| 44 | 44 |
| 45 # These 2 helper functions non-recursively glob inside a literal directory. | 45 # These 2 helper functions non-recursively glob inside a literal directory. |
| 46 # They return a list of basenames. `glob1` accepts a pattern while `glob0` | 46 # They return a list of basenames. `glob1` accepts a pattern while `glob0` |
| 47 # takes a literal basename (so it only has to check for its existence). | 47 # takes a literal basename (so it only has to check for its existence). |
| 48 | 48 |
| 49 def glob1(dirname, pattern): | 49 def glob1(dirname, pattern): |
| 50 if not dirname: | 50 if not dirname: |
| 51 dirname = os.curdir | 51 dirname = os.curdir |
| 52 if isinstance(pattern, str) and not isinstance(dirname, str): | 52 if isinstance(pattern, str) and not isinstance(dirname, str): |
| 53 dirname = str(dirname, sys.getfilesystemencoding() or | 53 try: |
| 54 sys.getdefaultencoding()) | 54 dirname = str(dirname, sys.getfilesystemencoding() or |
| 55 sys.getdefaultencoding()) | |
| 56 except UnicodeDecodeError: | |
|
GvR
2008/08/22 19:04:51
Why catch this exception? Again, ISTM that this A
| |
| 57 pass | |
| 55 try: | 58 try: |
| 56 names = os.listdir(dirname) | 59 names = os.listdir(dirname) |
| 57 except os.error: | 60 except os.error: |
| 58 return [] | 61 return [] |
| 59 if pattern[0] != '.': | 62 if pattern[0] != '.': |
| 60 names = [x for x in names if x[0] != '.'] | 63 names = [x for x in names if x[0] != '.'] |
| 61 return fnmatch.filter(names, pattern) | 64 return fnmatch.filter(names, pattern) |
| 62 | 65 |
| 63 def glob0(dirname, basename): | 66 def glob0(dirname, basename): |
| 64 if basename == '': | 67 if basename == '': |
| 65 # `os.path.split()` returns an empty basename for paths ending with a | 68 # `os.path.split()` returns an empty basename for paths ending with a |
| 66 # directory separator. 'q*x/' should match only directories. | 69 # directory separator. 'q*x/' should match only directories. |
| 67 if os.path.isdir(dirname): | 70 if os.path.isdir(dirname): |
| 68 return [basename] | 71 return [basename] |
| 69 else: | 72 else: |
| 70 if os.path.lexists(os.path.join(dirname, basename)): | 73 if os.path.lexists(os.path.join(dirname, basename)): |
| 71 return [basename] | 74 return [basename] |
| 72 return [] | 75 return [] |
| 73 | 76 |
| 74 | 77 |
| 75 magic_check = re.compile('[*?[]') | 78 magic_check = re.compile('[*?[]') |
| 76 | 79 |
| 77 def has_magic(s): | 80 def has_magic(s): |
| 78 return magic_check.search(s) is not None | 81 return magic_check.search(s) is not None |
| OLD | NEW |