Mercurial > cpython-lru_cache-weakref
annotate Lib/glob.py @ 58925:eecf1bef052d 2.6
Merged revisions 78207 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r78207 | ezio.melotti | 2010-02-17 01:26:09 +0200 (Wed, 17 Feb 2010) | 1 line
#7930: fix stripid
........
| author | Ezio Melotti <ezio.melotti@gmail.com> |
|---|---|
| date | Tue, 16 Feb 2010 23:29:44 +0000 |
| parents | 4acbbb3e65e2 |
| children | 787b969d37f0 |
| rev | line source |
|---|---|
| 5242 | 1 """Filename globbing utility.""" |
| 107 | 2 |
|
41050
850c32db27e6
Patch #1001604: glob.glob() now returns unicode filenames if it was
Georg Brandl <georg@python.org>
parents:
37545
diff
changeset
|
3 import sys |
| 570 | 4 import os |
|
41050
850c32db27e6
Patch #1001604: glob.glob() now returns unicode filenames if it was
Georg Brandl <georg@python.org>
parents:
37545
diff
changeset
|
5 import re |
| 107 | 6 import fnmatch |
| 7 | |
| 34365 | 8 __all__ = ["glob", "iglob"] |
| 570 | 9 |
| 107 | 10 def glob(pathname): |
|
15868
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
6519
diff
changeset
|
11 """Return a list of paths matching a pathname pattern. |
| 5242 | 12 |
|
15868
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
6519
diff
changeset
|
13 The pattern may contain simple shell-style wildcards a la fnmatch. |
| 5242 | 14 |
|
15868
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
6519
diff
changeset
|
15 """ |
| 34365 | 16 return list(iglob(pathname)) |
| 17 | |
| 18 def iglob(pathname): | |
|
52900
4acbbb3e65e2
Merged revisions 70866-70868,70870-70871,70893,70896,70902,70905,70907,70912,70915,70927,70933,70940,70944,70954,70963,70998,71056 via svnmerge from
Georg Brandl <georg@python.org>
parents:
41250
diff
changeset
|
19 """Return an iterator which yields the paths matching a pathname pattern. |
| 34365 | 20 |
| 21 The pattern may contain simple shell-style wildcards a la fnmatch. | |
| 22 | |
| 23 """ | |
|
15868
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
6519
diff
changeset
|
24 if not has_magic(pathname): |
|
33332
062177de8685
Patch #941486: add os.path.lexists(). Also fix bug #940578 by using lexists in glob.glob.
Johannes Gijsbers <jlg@dds.nl>
parents:
18040
diff
changeset
|
25 if os.path.lexists(pathname): |
| 34365 | 26 yield pathname |
| 27 return | |
|
15868
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
6519
diff
changeset
|
28 dirname, basename = os.path.split(pathname) |
|
18040
fb93dfbd8052
Patch #409973: Speedup glob.glob, add fnmatch.filter.
Martin v. Löwis <martin@v.loewis.de>
parents:
16072
diff
changeset
|
29 if not dirname: |
| 34365 | 30 for name in glob1(os.curdir, basename): |
| 31 yield name | |
| 32 return | |
| 33 if has_magic(dirname): | |
| 34 dirs = iglob(dirname) | |
|
15868
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
6519
diff
changeset
|
35 else: |
| 34365 | 36 dirs = [dirname] |
| 37 if has_magic(basename): | |
| 38 glob_in_dir = glob1 | |
|
15868
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
6519
diff
changeset
|
39 else: |
| 34365 | 40 glob_in_dir = glob0 |
| 41 for dirname in dirs: | |
| 42 for name in glob_in_dir(dirname, basename): | |
| 43 yield os.path.join(dirname, name) | |
| 44 | |
| 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` | |
| 47 # takes a literal basename (so it only has to check for its existence). | |
| 107 | 48 |
| 49 def glob1(dirname, pattern): | |
| 34365 | 50 if not dirname: |
| 51 dirname = os.curdir | |
|
41050
850c32db27e6
Patch #1001604: glob.glob() now returns unicode filenames if it was
Georg Brandl <georg@python.org>
parents:
37545
diff
changeset
|
52 if isinstance(pattern, unicode) and not isinstance(dirname, unicode): |
|
41250
9d0c05411734
Fix for glob.py if filesystem encoding is None.
Georg Brandl <georg@python.org>
parents:
41050
diff
changeset
|
53 dirname = unicode(dirname, sys.getfilesystemencoding() or |
|
9d0c05411734
Fix for glob.py if filesystem encoding is None.
Georg Brandl <georg@python.org>
parents:
41050
diff
changeset
|
54 sys.getdefaultencoding()) |
|
15868
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
6519
diff
changeset
|
55 try: |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
6519
diff
changeset
|
56 names = os.listdir(dirname) |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
6519
diff
changeset
|
57 except os.error: |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
6519
diff
changeset
|
58 return [] |
|
41050
850c32db27e6
Patch #1001604: glob.glob() now returns unicode filenames if it was
Georg Brandl <georg@python.org>
parents:
37545
diff
changeset
|
59 if pattern[0] != '.': |
|
850c32db27e6
Patch #1001604: glob.glob() now returns unicode filenames if it was
Georg Brandl <georg@python.org>
parents:
37545
diff
changeset
|
60 names = filter(lambda x: x[0] != '.', names) |
|
850c32db27e6
Patch #1001604: glob.glob() now returns unicode filenames if it was
Georg Brandl <georg@python.org>
parents:
37545
diff
changeset
|
61 return fnmatch.filter(names, pattern) |
| 107 | 62 |
| 34365 | 63 def glob0(dirname, basename): |
| 64 if basename == '': | |
| 65 # `os.path.split()` returns an empty basename for paths ending with a | |
| 66 # directory separator. 'q*x/' should match only directories. | |
|
37545
a8d4928292d8
glob('anything*/') would fail because isdir is in os.path, not os.
Neal Norwitz <nnorwitz@gmail.com>
parents:
34365
diff
changeset
|
67 if os.path.isdir(dirname): |
| 34365 | 68 return [basename] |
| 69 else: | |
| 70 if os.path.lexists(os.path.join(dirname, basename)): | |
| 71 return [basename] | |
| 72 return [] | |
| 73 | |
|
574
03b9f152adb6
Never return a non-existing pathname.
Guido van Rossum <guido@python.org>
parents:
570
diff
changeset
|
74 |
|
6519
24da035ceb6e
Convert all remaining *simple* cases of regex usage to re usage.
Guido van Rossum <guido@python.org>
parents:
5242
diff
changeset
|
75 magic_check = re.compile('[*?[]') |
|
574
03b9f152adb6
Never return a non-existing pathname.
Guido van Rossum <guido@python.org>
parents:
570
diff
changeset
|
76 |
| 107 | 77 def has_magic(s): |
|
15868
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
6519
diff
changeset
|
78 return magic_check.search(s) is not None |
