]> git.proxmox.com Git - mirror_edk2.git/blame - AppPkg/Applications/Python/Python-2.7.2/Lib/glob.py
AppPkg/Applications/Python: Add Python 2.7.2 sources since the release of Python...
[mirror_edk2.git] / AppPkg / Applications / Python / Python-2.7.2 / Lib / glob.py
CommitLineData
4710c53d 1"""Filename globbing utility."""\r
2\r
3import sys\r
4import os\r
5import re\r
6import fnmatch\r
7\r
8__all__ = ["glob", "iglob"]\r
9\r
10def glob(pathname):\r
11 """Return a list of paths matching a pathname pattern.\r
12\r
13 The pattern may contain simple shell-style wildcards a la fnmatch.\r
14\r
15 """\r
16 return list(iglob(pathname))\r
17\r
18def iglob(pathname):\r
19 """Return an iterator which yields the paths matching a pathname pattern.\r
20\r
21 The pattern may contain simple shell-style wildcards a la fnmatch.\r
22\r
23 """\r
24 if not has_magic(pathname):\r
25 if os.path.lexists(pathname):\r
26 yield pathname\r
27 return\r
28 dirname, basename = os.path.split(pathname)\r
29 if not dirname:\r
30 for name in glob1(os.curdir, basename):\r
31 yield name\r
32 return\r
33 if has_magic(dirname):\r
34 dirs = iglob(dirname)\r
35 else:\r
36 dirs = [dirname]\r
37 if has_magic(basename):\r
38 glob_in_dir = glob1\r
39 else:\r
40 glob_in_dir = glob0\r
41 for dirname in dirs:\r
42 for name in glob_in_dir(dirname, basename):\r
43 yield os.path.join(dirname, name)\r
44\r
45# These 2 helper functions non-recursively glob inside a literal directory.\r
46# They return a list of basenames. `glob1` accepts a pattern while `glob0`\r
47# takes a literal basename (so it only has to check for its existence).\r
48\r
49def glob1(dirname, pattern):\r
50 if not dirname:\r
51 dirname = os.curdir\r
52 if isinstance(pattern, unicode) and not isinstance(dirname, unicode):\r
53 dirname = unicode(dirname, sys.getfilesystemencoding() or\r
54 sys.getdefaultencoding())\r
55 try:\r
56 names = os.listdir(dirname)\r
57 except os.error:\r
58 return []\r
59 if pattern[0] != '.':\r
60 names = filter(lambda x: x[0] != '.', names)\r
61 return fnmatch.filter(names, pattern)\r
62\r
63def glob0(dirname, basename):\r
64 if basename == '':\r
65 # `os.path.split()` returns an empty basename for paths ending with a\r
66 # directory separator. 'q*x/' should match only directories.\r
67 if os.path.isdir(dirname):\r
68 return [basename]\r
69 else:\r
70 if os.path.lexists(os.path.join(dirname, basename)):\r
71 return [basename]\r
72 return []\r
73\r
74\r
75magic_check = re.compile('[*?[]')\r
76\r
77def has_magic(s):\r
78 return magic_check.search(s) is not None\r