1 | n/a | """Filename globbing utility.""" |
---|
2 | n/a | |
---|
3 | n/a | import os |
---|
4 | n/a | import re |
---|
5 | n/a | import fnmatch |
---|
6 | n/a | |
---|
7 | n/a | __all__ = ["glob", "iglob", "escape"] |
---|
8 | n/a | |
---|
9 | n/a | def glob(pathname, *, recursive=False): |
---|
10 | n/a | """Return a list of paths matching a pathname pattern. |
---|
11 | n/a | |
---|
12 | n/a | The pattern may contain simple shell-style wildcards a la |
---|
13 | n/a | fnmatch. However, unlike fnmatch, filenames starting with a |
---|
14 | n/a | dot are special cases that are not matched by '*' and '?' |
---|
15 | n/a | patterns. |
---|
16 | n/a | |
---|
17 | n/a | If recursive is true, the pattern '**' will match any files and |
---|
18 | n/a | zero or more directories and subdirectories. |
---|
19 | n/a | """ |
---|
20 | n/a | return list(iglob(pathname, recursive=recursive)) |
---|
21 | n/a | |
---|
22 | n/a | def iglob(pathname, *, recursive=False): |
---|
23 | n/a | """Return an iterator which yields the paths matching a pathname pattern. |
---|
24 | n/a | |
---|
25 | n/a | The pattern may contain simple shell-style wildcards a la |
---|
26 | n/a | fnmatch. However, unlike fnmatch, filenames starting with a |
---|
27 | n/a | dot are special cases that are not matched by '*' and '?' |
---|
28 | n/a | patterns. |
---|
29 | n/a | |
---|
30 | n/a | If recursive is true, the pattern '**' will match any files and |
---|
31 | n/a | zero or more directories and subdirectories. |
---|
32 | n/a | """ |
---|
33 | n/a | it = _iglob(pathname, recursive, False) |
---|
34 | n/a | if recursive and _isrecursive(pathname): |
---|
35 | n/a | s = next(it) # skip empty string |
---|
36 | n/a | assert not s |
---|
37 | n/a | return it |
---|
38 | n/a | |
---|
39 | n/a | def _iglob(pathname, recursive, dironly): |
---|
40 | n/a | dirname, basename = os.path.split(pathname) |
---|
41 | n/a | if not has_magic(pathname): |
---|
42 | n/a | assert not dironly |
---|
43 | n/a | if basename: |
---|
44 | n/a | if os.path.lexists(pathname): |
---|
45 | n/a | yield pathname |
---|
46 | n/a | else: |
---|
47 | n/a | # Patterns ending with a slash should match only directories |
---|
48 | n/a | if os.path.isdir(dirname): |
---|
49 | n/a | yield pathname |
---|
50 | n/a | return |
---|
51 | n/a | if not dirname: |
---|
52 | n/a | if recursive and _isrecursive(basename): |
---|
53 | n/a | yield from _glob2(dirname, basename, dironly) |
---|
54 | n/a | else: |
---|
55 | n/a | yield from _glob1(dirname, basename, dironly) |
---|
56 | n/a | return |
---|
57 | n/a | # `os.path.split()` returns the argument itself as a dirname if it is a |
---|
58 | n/a | # drive or UNC path. Prevent an infinite recursion if a drive or UNC path |
---|
59 | n/a | # contains magic characters (i.e. r'\\?\C:'). |
---|
60 | n/a | if dirname != pathname and has_magic(dirname): |
---|
61 | n/a | dirs = _iglob(dirname, recursive, True) |
---|
62 | n/a | else: |
---|
63 | n/a | dirs = [dirname] |
---|
64 | n/a | if has_magic(basename): |
---|
65 | n/a | if recursive and _isrecursive(basename): |
---|
66 | n/a | glob_in_dir = _glob2 |
---|
67 | n/a | else: |
---|
68 | n/a | glob_in_dir = _glob1 |
---|
69 | n/a | else: |
---|
70 | n/a | glob_in_dir = _glob0 |
---|
71 | n/a | for dirname in dirs: |
---|
72 | n/a | for name in glob_in_dir(dirname, basename, dironly): |
---|
73 | n/a | yield os.path.join(dirname, name) |
---|
74 | n/a | |
---|
75 | n/a | # These 2 helper functions non-recursively glob inside a literal directory. |
---|
76 | n/a | # They return a list of basenames. _glob1 accepts a pattern while _glob0 |
---|
77 | n/a | # takes a literal basename (so it only has to check for its existence). |
---|
78 | n/a | |
---|
79 | n/a | def _glob1(dirname, pattern, dironly): |
---|
80 | n/a | names = list(_iterdir(dirname, dironly)) |
---|
81 | n/a | if not _ishidden(pattern): |
---|
82 | n/a | names = (x for x in names if not _ishidden(x)) |
---|
83 | n/a | return fnmatch.filter(names, pattern) |
---|
84 | n/a | |
---|
85 | n/a | def _glob0(dirname, basename, dironly): |
---|
86 | n/a | if not basename: |
---|
87 | n/a | # `os.path.split()` returns an empty basename for paths ending with a |
---|
88 | n/a | # directory separator. 'q*x/' should match only directories. |
---|
89 | n/a | if os.path.isdir(dirname): |
---|
90 | n/a | return [basename] |
---|
91 | n/a | else: |
---|
92 | n/a | if os.path.lexists(os.path.join(dirname, basename)): |
---|
93 | n/a | return [basename] |
---|
94 | n/a | return [] |
---|
95 | n/a | |
---|
96 | n/a | # Following functions are not public but can be used by third-party code. |
---|
97 | n/a | |
---|
98 | n/a | def glob0(dirname, pattern): |
---|
99 | n/a | return _glob0(dirname, pattern, False) |
---|
100 | n/a | |
---|
101 | n/a | def glob1(dirname, pattern): |
---|
102 | n/a | return _glob1(dirname, pattern, False) |
---|
103 | n/a | |
---|
104 | n/a | # This helper function recursively yields relative pathnames inside a literal |
---|
105 | n/a | # directory. |
---|
106 | n/a | |
---|
107 | n/a | def _glob2(dirname, pattern, dironly): |
---|
108 | n/a | assert _isrecursive(pattern) |
---|
109 | n/a | yield pattern[:0] |
---|
110 | n/a | yield from _rlistdir(dirname, dironly) |
---|
111 | n/a | |
---|
112 | n/a | # If dironly is false, yields all file names inside a directory. |
---|
113 | n/a | # If dironly is true, yields only directory names. |
---|
114 | n/a | def _iterdir(dirname, dironly): |
---|
115 | n/a | if not dirname: |
---|
116 | n/a | if isinstance(dirname, bytes): |
---|
117 | n/a | dirname = bytes(os.curdir, 'ASCII') |
---|
118 | n/a | else: |
---|
119 | n/a | dirname = os.curdir |
---|
120 | n/a | try: |
---|
121 | n/a | with os.scandir(dirname) as it: |
---|
122 | n/a | for entry in it: |
---|
123 | n/a | try: |
---|
124 | n/a | if not dironly or entry.is_dir(): |
---|
125 | n/a | yield entry.name |
---|
126 | n/a | except OSError: |
---|
127 | n/a | pass |
---|
128 | n/a | except OSError: |
---|
129 | n/a | return |
---|
130 | n/a | |
---|
131 | n/a | # Recursively yields relative pathnames inside a literal directory. |
---|
132 | n/a | def _rlistdir(dirname, dironly): |
---|
133 | n/a | names = list(_iterdir(dirname, dironly)) |
---|
134 | n/a | for x in names: |
---|
135 | n/a | if not _ishidden(x): |
---|
136 | n/a | yield x |
---|
137 | n/a | path = os.path.join(dirname, x) if dirname else x |
---|
138 | n/a | for y in _rlistdir(path, dironly): |
---|
139 | n/a | yield os.path.join(x, y) |
---|
140 | n/a | |
---|
141 | n/a | |
---|
142 | n/a | magic_check = re.compile('([*?[])') |
---|
143 | n/a | magic_check_bytes = re.compile(b'([*?[])') |
---|
144 | n/a | |
---|
145 | n/a | def has_magic(s): |
---|
146 | n/a | if isinstance(s, bytes): |
---|
147 | n/a | match = magic_check_bytes.search(s) |
---|
148 | n/a | else: |
---|
149 | n/a | match = magic_check.search(s) |
---|
150 | n/a | return match is not None |
---|
151 | n/a | |
---|
152 | n/a | def _ishidden(path): |
---|
153 | n/a | return path[0] in ('.', b'.'[0]) |
---|
154 | n/a | |
---|
155 | n/a | def _isrecursive(pattern): |
---|
156 | n/a | if isinstance(pattern, bytes): |
---|
157 | n/a | return pattern == b'**' |
---|
158 | n/a | else: |
---|
159 | n/a | return pattern == '**' |
---|
160 | n/a | |
---|
161 | n/a | def escape(pathname): |
---|
162 | n/a | """Escape all special characters. |
---|
163 | n/a | """ |
---|
164 | n/a | # Escaping is done by wrapping any of "*?[" between square brackets. |
---|
165 | n/a | # Metacharacters do not work in the drive part and shouldn't be escaped. |
---|
166 | n/a | drive, pathname = os.path.splitdrive(pathname) |
---|
167 | n/a | if isinstance(pathname, bytes): |
---|
168 | n/a | pathname = magic_check_bytes.sub(br'[\1]', pathname) |
---|
169 | n/a | else: |
---|
170 | n/a | pathname = magic_check.sub(r'[\1]', pathname) |
---|
171 | n/a | return drive + pathname |
---|