| LEFT | RIGHT |
|---|---|
| 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 30 matching lines...) Expand all Loading... | |
| 41 for dirname in dirs: | 41 for dirname in dirs: |
| 42 for name in glob_in_dir(dirname, basename): | 42 for name in glob_in_dir(dirname, basename): |
| 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 if isinstance(pattern, bytes): |
| 52 if isinstance(pattern, (bytes, bytearray)): | 52 dirname = bytes(os.curdir, 'ASCII') |
| 53 dirname = dirname.encode('ASCII') | 53 else: |
|
GvR
2008/10/01 00:00:31
It might be more readable to write
dirname = by
| |
| 54 dirname = os.curdir | |
| 54 try: | 55 try: |
| 55 names = os.listdir(dirname) | 56 names = os.listdir(dirname) |
| 56 except os.error: | 57 except os.error: |
| 57 return [] | 58 return [] |
| 58 if pattern[0] != '.': | 59 if pattern[0] != '.': |
| 59 names = [x for x in names if x[0] != '.'] | 60 names = [x for x in names if x[0] != '.'] |
| 60 return fnmatch.filter(names, pattern) | 61 return fnmatch.filter(names, pattern) |
| 61 | 62 |
| 62 def glob0(dirname, basename): | 63 def glob0(dirname, basename): |
| 63 if basename == '': | 64 if basename == '': |
| 64 # `os.path.split()` returns an empty basename for paths ending with a | 65 # `os.path.split()` returns an empty basename for paths ending with a |
| 65 # directory separator. 'q*x/' should match only directories. | 66 # directory separator. 'q*x/' should match only directories. |
| 66 if os.path.isdir(dirname): | 67 if os.path.isdir(dirname): |
| 67 return [basename] | 68 return [basename] |
| 68 else: | 69 else: |
| 69 if os.path.lexists(os.path.join(dirname, basename)): | 70 if os.path.lexists(os.path.join(dirname, basename)): |
| 70 return [basename] | 71 return [basename] |
| 71 return [] | 72 return [] |
| 72 | 73 |
| 73 | 74 |
| 74 magic_check = re.compile('[*?[]') | 75 magic_check = re.compile('[*?[]') |
| 75 magic_check_bytes = re.compile(b'[*?[]') | 76 magic_check_bytes = re.compile(b'[*?[]') |
| 76 | 77 |
| 77 def has_magic(s): | 78 def has_magic(s): |
| 78 if isinstance(s, (bytes, bytearray)): | 79 if isinstance(s, bytes): |
| 79 match = magic_check_bytes.search(s) | 80 match = magic_check_bytes.search(s) |
| 80 else: | 81 else: |
| 81 match = magic_check.search(s) | 82 match = magic_check.search(s) |
| 82 return match is not None | 83 return match is not None |
| LEFT | RIGHT |