blob: c40f50035dcf9f8583c2a6e56c94749f7f2ceaf3 [file] [log] [blame]
Guido van Rossum7e4b2de1995-01-27 02:41:45 +00001"""Filename matching with shell patterns.
Guido van Rossum05e52191992-01-12 23:29:29 +00002
Guido van Rossum7e4b2de1995-01-27 02:41:45 +00003fnmatch(FILENAME, PATTERN) matches according to the local convention.
4fnmatchcase(FILENAME, PATTERN) always takes case in account.
Guido van Rossum05e52191992-01-12 23:29:29 +00005
Guido van Rossum7e4b2de1995-01-27 02:41:45 +00006The functions operate by translating the pattern into a regular
7expression. They cache the compiled regular expressions for speed.
8
9The function translate(PATTERN) returns a regular expression
10corresponding to PATTERN. (It does not compile it.)
11"""
12
Guido van Rossum9694fca1997-10-22 21:00:49 +000013import re
14
Skip Montanaroeccd02a2001-01-20 23:34:12 +000015__all__ = ["fnmatch","fnmatchcase","translate"]
16
Guido van Rossum7e4b2de1995-01-27 02:41:45 +000017_cache = {}
Guido van Rossum762c39e1991-01-01 18:11:14 +000018
Guido van Rossum762c39e1991-01-01 18:11:14 +000019def fnmatch(name, pat):
Tim Peters88869f92001-01-14 23:36:06 +000020 """Test whether FILENAME matches PATTERN.
21
22 Patterns are Unix shell style:
23
24 * matches everything
25 ? matches any single character
26 [seq] matches any character in seq
27 [!seq] matches any char not in seq
28
29 An initial period in FILENAME is not special.
30 Both FILENAME and PATTERN are first case-normalized
31 if the operating system requires it.
32 If you don't want this, use fnmatchcase(FILENAME, PATTERN).
33 """
34
35 import os
36 name = os.path.normcase(name)
37 pat = os.path.normcase(pat)
38 return fnmatchcase(name, pat)
Guido van Rossum7e4b2de1995-01-27 02:41:45 +000039
40def fnmatchcase(name, pat):
Tim Peters88869f92001-01-14 23:36:06 +000041 """Test whether FILENAME matches PATTERN, including case.
42
43 This is a version of fnmatch() which doesn't case-normalize
44 its arguments.
45 """
46
47 if not _cache.has_key(pat):
48 res = translate(pat)
49 _cache[pat] = re.compile(res)
50 return _cache[pat].match(name) is not None
Guido van Rossum762c39e1991-01-01 18:11:14 +000051
Guido van Rossum05e52191992-01-12 23:29:29 +000052def translate(pat):
Tim Peters88869f92001-01-14 23:36:06 +000053 """Translate a shell PATTERN to a regular expression.
54
55 There is no way to quote meta-characters.
56 """
57
58 i, n = 0, len(pat)
59 res = ''
60 while i < n:
61 c = pat[i]
62 i = i+1
63 if c == '*':
64 res = res + '.*'
65 elif c == '?':
66 res = res + '.'
67 elif c == '[':
68 j = i
69 if j < n and pat[j] == '!':
70 j = j+1
71 if j < n and pat[j] == ']':
72 j = j+1
73 while j < n and pat[j] != ']':
74 j = j+1
75 if j >= n:
76 res = res + '\\['
77 else:
78 stuff = pat[i:j]
79 i = j+1
80 if stuff[0] == '!':
81 stuff = '[^' + stuff[1:] + ']'
82 elif stuff == '^'*len(stuff):
83 stuff = '\\^'
84 else:
85 while stuff[0] == '^':
86 stuff = stuff[1:] + stuff[0]
87 stuff = '[' + stuff + ']'
88 res = res + stuff
89 else:
90 res = res + re.escape(c)
91 return res + "$"