(py-indent-right, py-outdent-left): new commands, bound to C-c C-r and
[python/dscho.git] / Lib / glob.py
blobbacaf183e1f6bd0fa681c292d9f21c0124f2a3aa
1 # Module 'glob' -- filename globbing.
3 import os
4 import fnmatch
5 import regex
8 def glob(pathname):
9 if not has_magic(pathname):
10 if os.path.exists(pathname):
11 return [pathname]
12 else:
13 return []
14 dirname, basename = os.path.split(pathname)
15 if has_magic(dirname):
16 list = glob(dirname)
17 else:
18 list = [dirname]
19 if not has_magic(basename):
20 result = []
21 for dirname in list:
22 if basename or os.path.isdir(dirname):
23 name = os.path.join(dirname, basename)
24 if os.path.exists(name):
25 result.append(name)
26 else:
27 result = []
28 for dirname in list:
29 sublist = glob1(dirname, basename)
30 for name in sublist:
31 result.append(os.path.join(dirname, name))
32 return result
34 def glob1(dirname, pattern):
35 if not dirname: dirname = os.curdir
36 try:
37 names = os.listdir(dirname)
38 except os.error:
39 return []
40 result = []
41 for name in names:
42 if name[0] != '.' or pattern[0] == '.':
43 if fnmatch.fnmatch(name, pattern):
44 result.append(name)
45 return result
48 magic_check = regex.compile('[*?[]')
50 def has_magic(s):
51 return magic_check.search(s) >= 0