1 | n/a | """Filename matching with shell patterns. |
---|
2 | n/a | |
---|
3 | n/a | fnmatch(FILENAME, PATTERN) matches according to the local convention. |
---|
4 | n/a | fnmatchcase(FILENAME, PATTERN) always takes case in account. |
---|
5 | n/a | |
---|
6 | n/a | The functions operate by translating the pattern into a regular |
---|
7 | n/a | expression. They cache the compiled regular expressions for speed. |
---|
8 | n/a | |
---|
9 | n/a | The function translate(PATTERN) returns a regular expression |
---|
10 | n/a | corresponding to PATTERN. (It does not compile it.) |
---|
11 | n/a | """ |
---|
12 | n/a | import os |
---|
13 | n/a | import posixpath |
---|
14 | n/a | import re |
---|
15 | n/a | import functools |
---|
16 | n/a | |
---|
17 | n/a | __all__ = ["filter", "fnmatch", "fnmatchcase", "translate"] |
---|
18 | n/a | |
---|
19 | n/a | def fnmatch(name, pat): |
---|
20 | n/a | """Test whether FILENAME matches PATTERN. |
---|
21 | n/a | |
---|
22 | n/a | Patterns are Unix shell style: |
---|
23 | n/a | |
---|
24 | n/a | * matches everything |
---|
25 | n/a | ? matches any single character |
---|
26 | n/a | [seq] matches any character in seq |
---|
27 | n/a | [!seq] matches any char not in seq |
---|
28 | n/a | |
---|
29 | n/a | An initial period in FILENAME is not special. |
---|
30 | n/a | Both FILENAME and PATTERN are first case-normalized |
---|
31 | n/a | if the operating system requires it. |
---|
32 | n/a | If you don't want this, use fnmatchcase(FILENAME, PATTERN). |
---|
33 | n/a | """ |
---|
34 | n/a | name = os.path.normcase(name) |
---|
35 | n/a | pat = os.path.normcase(pat) |
---|
36 | n/a | return fnmatchcase(name, pat) |
---|
37 | n/a | |
---|
38 | n/a | @functools.lru_cache(maxsize=256, typed=True) |
---|
39 | n/a | def _compile_pattern(pat): |
---|
40 | n/a | if isinstance(pat, bytes): |
---|
41 | n/a | pat_str = str(pat, 'ISO-8859-1') |
---|
42 | n/a | res_str = translate(pat_str) |
---|
43 | n/a | res = bytes(res_str, 'ISO-8859-1') |
---|
44 | n/a | else: |
---|
45 | n/a | res = translate(pat) |
---|
46 | n/a | return re.compile(res).match |
---|
47 | n/a | |
---|
48 | n/a | def filter(names, pat): |
---|
49 | n/a | """Return the subset of the list NAMES that match PAT.""" |
---|
50 | n/a | result = [] |
---|
51 | n/a | pat = os.path.normcase(pat) |
---|
52 | n/a | match = _compile_pattern(pat) |
---|
53 | n/a | if os.path is posixpath: |
---|
54 | n/a | # normcase on posix is NOP. Optimize it away from the loop. |
---|
55 | n/a | for name in names: |
---|
56 | n/a | if match(name): |
---|
57 | n/a | result.append(name) |
---|
58 | n/a | else: |
---|
59 | n/a | for name in names: |
---|
60 | n/a | if match(os.path.normcase(name)): |
---|
61 | n/a | result.append(name) |
---|
62 | n/a | return result |
---|
63 | n/a | |
---|
64 | n/a | def fnmatchcase(name, pat): |
---|
65 | n/a | """Test whether FILENAME matches PATTERN, including case. |
---|
66 | n/a | |
---|
67 | n/a | This is a version of fnmatch() which doesn't case-normalize |
---|
68 | n/a | its arguments. |
---|
69 | n/a | """ |
---|
70 | n/a | match = _compile_pattern(pat) |
---|
71 | n/a | return match(name) is not None |
---|
72 | n/a | |
---|
73 | n/a | |
---|
74 | n/a | def translate(pat): |
---|
75 | n/a | """Translate a shell PATTERN to a regular expression. |
---|
76 | n/a | |
---|
77 | n/a | There is no way to quote meta-characters. |
---|
78 | n/a | """ |
---|
79 | n/a | |
---|
80 | n/a | i, n = 0, len(pat) |
---|
81 | n/a | res = '' |
---|
82 | n/a | while i < n: |
---|
83 | n/a | c = pat[i] |
---|
84 | n/a | i = i+1 |
---|
85 | n/a | if c == '*': |
---|
86 | n/a | res = res + '.*' |
---|
87 | n/a | elif c == '?': |
---|
88 | n/a | res = res + '.' |
---|
89 | n/a | elif c == '[': |
---|
90 | n/a | j = i |
---|
91 | n/a | if j < n and pat[j] == '!': |
---|
92 | n/a | j = j+1 |
---|
93 | n/a | if j < n and pat[j] == ']': |
---|
94 | n/a | j = j+1 |
---|
95 | n/a | while j < n and pat[j] != ']': |
---|
96 | n/a | j = j+1 |
---|
97 | n/a | if j >= n: |
---|
98 | n/a | res = res + '\\[' |
---|
99 | n/a | else: |
---|
100 | n/a | stuff = pat[i:j].replace('\\','\\\\') |
---|
101 | n/a | i = j+1 |
---|
102 | n/a | if stuff[0] == '!': |
---|
103 | n/a | stuff = '^' + stuff[1:] |
---|
104 | n/a | elif stuff[0] == '^': |
---|
105 | n/a | stuff = '\\' + stuff |
---|
106 | n/a | res = '%s[%s]' % (res, stuff) |
---|
107 | n/a | else: |
---|
108 | n/a | res = res + re.escape(c) |
---|
109 | n/a | return r'(?s:%s)\Z' % res |
---|