Staging
v0.5.1
v0.5.1
https://github.com/python/cpython
Revision 9253b8c98e8caac158f19fa580728823cbb6ec47 authored by Guido van Rossum on 20 August 2008, 14:59:20 UTC, committed by Guido van Rossum on 20 August 2008, 14:59:20 UTC
........ r65912 | guido.van.rossum | 2008-08-20 07:57:20 -0700 (Wed, 20 Aug 2008) | 2 lines News for the imageop fix. ........
1 parent c452362
Tip revision: 9253b8c98e8caac158f19fa580728823cbb6ec47 authored by Guido van Rossum on 20 August 2008, 14:59:20 UTC
Blocked revisions 65912 via svnmerge
Blocked revisions 65912 via svnmerge
Tip revision: 9253b8c
glob.py
"""Filename globbing utility."""
import sys
import os
import re
import fnmatch
__all__ = ["glob", "iglob"]
def glob(pathname):
"""Return a list of paths matching a pathname pattern.
The pattern may contain simple shell-style wildcards a la fnmatch.
"""
return list(iglob(pathname))
def iglob(pathname):
"""Return a list of paths matching a pathname pattern.
The pattern may contain simple shell-style wildcards a la fnmatch.
"""
if not has_magic(pathname):
if os.path.lexists(pathname):
yield pathname
return
dirname, basename = os.path.split(pathname)
if not dirname:
for name in glob1(os.curdir, basename):
yield name
return
if has_magic(dirname):
dirs = iglob(dirname)
else:
dirs = [dirname]
if has_magic(basename):
glob_in_dir = glob1
else:
glob_in_dir = glob0
for dirname in dirs:
for name in glob_in_dir(dirname, basename):
yield os.path.join(dirname, name)
# These 2 helper functions non-recursively glob inside a literal directory.
# They return a list of basenames. `glob1` accepts a pattern while `glob0`
# takes a literal basename (so it only has to check for its existence).
def glob1(dirname, pattern):
if not dirname:
dirname = os.curdir
if isinstance(pattern, str) and not isinstance(dirname, str):
dirname = str(dirname, sys.getfilesystemencoding() or
sys.getdefaultencoding())
try:
names = os.listdir(dirname)
except os.error:
return []
if pattern[0] != '.':
names = [x for x in names if x[0] != '.']
return fnmatch.filter(names, pattern)
def glob0(dirname, basename):
if basename == '':
# `os.path.split()` returns an empty basename for paths ending with a
# directory separator. 'q*x/' should match only directories.
if os.path.isdir(dirname):
return [basename]
else:
if os.path.lexists(os.path.join(dirname, basename)):
return [basename]
return []
magic_check = re.compile('[*?[]')
def has_magic(s):
return magic_check.search(s) is not None
Computing file changes ...