1 """Filename globbing utility."""
8 __all__
= ["glob", "iglob"]
11 """Return a list of paths matching a pathname pattern.
13 The pattern may contain simple shell-style wildcards a la fnmatch.
16 return list(iglob(pathname
))
19 """Return an iterator which yields the paths matching a pathname pattern.
21 The pattern may contain simple shell-style wildcards a la fnmatch.
24 if not has_magic(pathname
):
25 if os
.path
.lexists(pathname
):
28 dirname
, basename
= os
.path
.split(pathname
)
30 for name
in glob1(None, basename
):
33 if has_magic(dirname
):
37 if has_magic(basename
):
42 for name
in glob_in_dir(dirname
, basename
):
43 yield os
.path
.join(dirname
, name
)
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).
49 def glob1(dirname
, pattern
):
51 if isinstance(pattern
, bytes
):
52 dirname
= bytes(os
.curdir
, 'ASCII')
56 names
= os
.listdir(dirname
)
60 names
= [x
for x
in names
if x
[0] != '.']
61 return fnmatch
.filter(names
, pattern
)
63 def glob0(dirname
, basename
):
65 # `os.path.split()` returns an empty basename for paths ending with a
66 # directory separator. 'q*x/' should match only directories.
67 if os
.path
.isdir(dirname
):
70 if os
.path
.lexists(os
.path
.join(dirname
, basename
)):
75 magic_check
= re
.compile('[*?[]')
76 magic_check_bytes
= re
.compile(b
'[*?[]')
79 if isinstance(s
, bytes
):
80 match
= magic_check_bytes
.search(s
)
82 match
= magic_check
.search(s
)
83 return match
is not None