3
r""" mglob - enhanced file list expansion module
5
Use as stand-alone utility (for xargs, `backticks` etc.),
6
or a globbing library for own python programs. Globbing the sys.argv is something
7
that almost every Windows script has to perform manually, and this module is here
8
to help with that task. Also Unix users will benefit from enhanced modes
9
such as recursion, exclusion, directory omission...
11
Unlike glob.glob, directories are not included in the glob unless specified
14
'expand' is the function to use in python programs. Typical use
15
to expand argv (esp. in windows)::
19
files = mglob.expand(sys.argv[1:])
21
print "mglob not found; try 'easy_install mglob' for extra features"
24
Note that for unix, shell expands *normal* wildcards (*.cpp, etc.) in argv.
25
Therefore, you might want to use quotes with normal wildcards to prevent this
26
expansion, in order for mglob to see the wildcards and get the wanted behaviour.
27
Not quoting the wildcards is harmless and typically has equivalent results, though.
29
Author: Ville Vainio <vivainio@gmail.com>
30
License: MIT Open Source license
34
#Assigned in variable for "usage" printing convenience"
37
This program allows specifying filenames with "mglob" mechanism.
38
Supported syntax in globs (wilcard matching patterns)::
41
- obvious. Differs from normal glob in that dirs are not included.
42
Unix users might want to write this as: "*.cpp" "?ellowo*"
43
rec:/usr/share=*.txt,*.doc
44
- get all *.txt and *.doc under /usr/share,
47
- All files under /usr/share, recursively
49
- All .py files under current working dir, recursively
53
- readme*, exclude files ending with .bak
54
!.svn/ !.hg/ !*_Data/ rec:.
55
- Skip .svn, .hg, foo_Data dirs (and their subdirs) in recurse.
56
Trailing / is the key, \ does not work!
58
- the directory foo if it exists (not files in foo)
60
- all directories in current folder
61
foo.py bar.* !h* rec:*.py
62
- Obvious. !h* exclusion only applies for rec:*.py.
63
foo.py is *not* included twice.
65
- All files listed in 'filelist.txt' file, on separate lines.
72
import os,glob,fnmatch,sys
73
from sets import Set as set
76
def expand(flist,exp_dirs = False):
77
""" Expand the glob(s) in flist.
79
flist may be either a whitespace-separated list of globs/files
80
or an array of globs/files.
82
if exp_dirs is true, directory names in glob are expanded to the files
83
contained in them - otherwise, directory names are returned as is.
86
if isinstance(flist, basestring):
91
def recfind(p, pats = ["*"]):
92
denied_dirs = ["*" + d+"*" for d in denied_set if d.endswith("/")]
93
#print "de", denied_dirs
94
for (dp,dnames,fnames) in os.walk(p):
95
# see if we should ignore the whole directory
96
dp_norm = dp.replace("\\","/") + "/"
99
for deny_pat in denied_dirs:
100
if fnmatch.fnmatch( dp_norm, deny_pat):
110
if fnmatch.fnmatch(f,p):
114
yield os.path.join(dp,f)
116
def once_filter(seq):
118
p = os.path.abspath(it)
123
for deny_pat in denied_set:
124
if fnmatch.fnmatch(os.path.basename(p), deny_pat):
134
ent = os.path.expanduser(os.path.expandvars(ent))
135
if ent.lower().startswith('rec:'):
136
fields = ent[4:].split('=')
138
pth, patlist = fields
139
elif len(fields) == 1:
140
if os.path.isdir(fields[0]):
142
pth, patlist = fields[0], '*'
144
# single arg is pattern
145
pth, patlist = '.', fields[0]
147
elif len(fields) == 0:
148
pth, pathlist = '.','*'
150
pats = patlist.split(',')
151
res.extend(once_filter(recfind(pth, pats)))
153
elif ent.startswith('@') and os.path.isfile(ent[1:]):
154
res.extend(once_filter(open(ent[1:]).read().splitlines()))
156
elif ent.startswith('!'):
157
denied_set.add(ent[1:])
159
elif ent.lower().startswith('dir:'):
160
res.extend(once_filter(filter(os.path.isdir,glob.glob(ent[4:]))))
162
# get all files in the specified dir
163
elif os.path.isdir(ent) and exp_dirs:
164
res.extend(once_filter(filter(os.path.isfile,glob.glob(ent + os.sep+"*"))))
168
elif '*' in ent or '?' in ent:
169
res.extend(once_filter(filter(os.path.isfile,glob.glob(ent))))
172
res.extend(once_filter([ent]))
178
expand("*.py ~/.ipython/*.py rec:/usr/share/doc-base") ==
179
expand( ['*.py', '~/.ipython/*.py', 'rec:/usr/share/doc-base'] )
183
if len(sys.argv) < 2:
187
print "\n".join(expand(sys.argv[1:])),
189
def mglob_f(self, arg):
190
from IPython.genutils import SList
192
return SList(expand(arg))
193
print "Please specify pattern!"
196
def init_ipython(ip):
197
""" register %mglob for IPython """
198
mglob_f.__doc__ = globsyntax
199
ip.expose_magic("mglob",mglob_f)
202
if __name__ == "__main__":