1991-01-01 14:17:49 -04:00
|
|
|
# Module 'glob' -- filename globbing.
|
|
|
|
|
1992-01-12 19:26:24 -04:00
|
|
|
import os
|
1991-01-01 14:17:49 -04:00
|
|
|
import fnmatch
|
1992-01-12 19:32:11 -04:00
|
|
|
import regex
|
1991-01-01 14:17:49 -04:00
|
|
|
|
1992-01-12 19:26:24 -04:00
|
|
|
|
1991-01-01 14:17:49 -04:00
|
|
|
def glob(pathname):
|
1992-01-12 19:32:11 -04:00
|
|
|
if not has_magic(pathname):
|
|
|
|
if os.path.exists(pathname):
|
|
|
|
return [pathname]
|
|
|
|
else:
|
|
|
|
return []
|
1992-01-12 19:26:24 -04:00
|
|
|
dirname, basename = os.path.split(pathname)
|
1991-01-01 14:17:49 -04:00
|
|
|
if has_magic(dirname):
|
|
|
|
list = glob(dirname)
|
|
|
|
else:
|
|
|
|
list = [dirname]
|
|
|
|
if not has_magic(basename):
|
|
|
|
result = []
|
|
|
|
for dirname in list:
|
1992-01-12 19:26:24 -04:00
|
|
|
if basename or os.path.isdir(dirname):
|
|
|
|
name = os.path.join(dirname, basename)
|
|
|
|
if os.path.exists(name):
|
1991-01-01 14:17:49 -04:00
|
|
|
result.append(name)
|
|
|
|
else:
|
|
|
|
result = []
|
|
|
|
for dirname in list:
|
|
|
|
sublist = glob1(dirname, basename)
|
|
|
|
for name in sublist:
|
1992-01-12 19:26:24 -04:00
|
|
|
result.append(os.path.join(dirname, name))
|
1991-01-01 14:17:49 -04:00
|
|
|
return result
|
|
|
|
|
|
|
|
def glob1(dirname, pattern):
|
1992-01-12 19:26:24 -04:00
|
|
|
if not dirname: dirname = os.curdir
|
1991-01-01 14:17:49 -04:00
|
|
|
try:
|
1992-01-12 19:26:24 -04:00
|
|
|
names = os.listdir(dirname)
|
|
|
|
except os.error:
|
1991-01-01 14:17:49 -04:00
|
|
|
return []
|
|
|
|
result = []
|
|
|
|
for name in names:
|
1992-01-12 19:32:11 -04:00
|
|
|
if name[0] != '.' or pattern[0] == '.':
|
|
|
|
if fnmatch.fnmatch(name, pattern):
|
|
|
|
result.append(name)
|
1991-01-01 14:17:49 -04:00
|
|
|
return result
|
|
|
|
|
1992-01-12 19:32:11 -04:00
|
|
|
|
|
|
|
magic_check = regex.compile('[*?[]')
|
|
|
|
|
1991-01-01 14:17:49 -04:00
|
|
|
def has_magic(s):
|
1992-01-12 19:32:11 -04:00
|
|
|
return magic_check.search(s) >= 0
|