annotate Lib/fnmatch.py @ 5817:48b94de85bf3 legacy-trunk

Extensive changes by AMK.
author Guido van Rossum <guido@python.org>
date Thu, 17 Jul 1997 16:29:42 +0000
parents a81b6db6d33c
children 24da035ceb6e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2260
a81b6db6d33c changes for the Mac
Guido van Rossum <guido@python.org>
parents: 578
diff changeset
1 """Filename matching with shell patterns.
a81b6db6d33c changes for the Mac
Guido van Rossum <guido@python.org>
parents: 578
diff changeset
2
a81b6db6d33c changes for the Mac
Guido van Rossum <guido@python.org>
parents: 578
diff changeset
3 fnmatch(FILENAME, PATTERN) matches according to the local convention.
a81b6db6d33c changes for the Mac
Guido van Rossum <guido@python.org>
parents: 578
diff changeset
4 fnmatchcase(FILENAME, PATTERN) always takes case in account.
573
4cb55356706f Rewritten using regex.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
5
2260
a81b6db6d33c changes for the Mac
Guido van Rossum <guido@python.org>
parents: 578
diff changeset
6 The functions operate by translating the pattern into a regular
a81b6db6d33c changes for the Mac
Guido van Rossum <guido@python.org>
parents: 578
diff changeset
7 expression. They cache the compiled regular expressions for speed.
573
4cb55356706f Rewritten using regex.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
8
2260
a81b6db6d33c changes for the Mac
Guido van Rossum <guido@python.org>
parents: 578
diff changeset
9 The function translate(PATTERN) returns a regular expression
a81b6db6d33c changes for the Mac
Guido van Rossum <guido@python.org>
parents: 578
diff changeset
10 corresponding to PATTERN. (It does not compile it.)
a81b6db6d33c changes for the Mac
Guido van Rossum <guido@python.org>
parents: 578
diff changeset
11 """
a81b6db6d33c changes for the Mac
Guido van Rossum <guido@python.org>
parents: 578
diff changeset
12
a81b6db6d33c changes for the Mac
Guido van Rossum <guido@python.org>
parents: 578
diff changeset
13 _cache = {}
106
64373c79c099 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
14
64373c79c099 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
15 def fnmatch(name, pat):
2260
a81b6db6d33c changes for the Mac
Guido van Rossum <guido@python.org>
parents: 578
diff changeset
16 """Test whether FILENAME matches PATTERN.
a81b6db6d33c changes for the Mac
Guido van Rossum <guido@python.org>
parents: 578
diff changeset
17
a81b6db6d33c changes for the Mac
Guido van Rossum <guido@python.org>
parents: 578
diff changeset
18 Patterns are Unix shell style:
a81b6db6d33c changes for the Mac
Guido van Rossum <guido@python.org>
parents: 578
diff changeset
19
a81b6db6d33c changes for the Mac
Guido van Rossum <guido@python.org>
parents: 578
diff changeset
20 * matches everything
a81b6db6d33c changes for the Mac
Guido van Rossum <guido@python.org>
parents: 578
diff changeset
21 ? matches any single character
a81b6db6d33c changes for the Mac
Guido van Rossum <guido@python.org>
parents: 578
diff changeset
22 [seq] matches any character in seq
a81b6db6d33c changes for the Mac
Guido van Rossum <guido@python.org>
parents: 578
diff changeset
23 [!seq] matches any char not in seq
a81b6db6d33c changes for the Mac
Guido van Rossum <guido@python.org>
parents: 578
diff changeset
24
a81b6db6d33c changes for the Mac
Guido van Rossum <guido@python.org>
parents: 578
diff changeset
25 An initial period in FILENAME is not special.
a81b6db6d33c changes for the Mac
Guido van Rossum <guido@python.org>
parents: 578
diff changeset
26 Both FILENAME and PATTERN are first case-normalized
a81b6db6d33c changes for the Mac
Guido van Rossum <guido@python.org>
parents: 578
diff changeset
27 if the operating system requires it.
a81b6db6d33c changes for the Mac
Guido van Rossum <guido@python.org>
parents: 578
diff changeset
28 If you don't want this, use fnmatchcase(FILENAME, PATTERN).
a81b6db6d33c changes for the Mac
Guido van Rossum <guido@python.org>
parents: 578
diff changeset
29 """
a81b6db6d33c changes for the Mac
Guido van Rossum <guido@python.org>
parents: 578
diff changeset
30
a81b6db6d33c changes for the Mac
Guido van Rossum <guido@python.org>
parents: 578
diff changeset
31 import os
573
4cb55356706f Rewritten using regex.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
32 name = os.path.normcase(name)
4cb55356706f Rewritten using regex.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
33 pat = os.path.normcase(pat)
2260
a81b6db6d33c changes for the Mac
Guido van Rossum <guido@python.org>
parents: 578
diff changeset
34 return fnmatchcase(name, pat)
a81b6db6d33c changes for the Mac
Guido van Rossum <guido@python.org>
parents: 578
diff changeset
35
a81b6db6d33c changes for the Mac
Guido van Rossum <guido@python.org>
parents: 578
diff changeset
36 def fnmatchcase(name, pat):
a81b6db6d33c changes for the Mac
Guido van Rossum <guido@python.org>
parents: 578
diff changeset
37 """Test wheter FILENAME matches PATTERN, including case.
a81b6db6d33c changes for the Mac
Guido van Rossum <guido@python.org>
parents: 578
diff changeset
38
a81b6db6d33c changes for the Mac
Guido van Rossum <guido@python.org>
parents: 578
diff changeset
39 This is a version of fnmatch() which doesn't case-normalize
a81b6db6d33c changes for the Mac
Guido van Rossum <guido@python.org>
parents: 578
diff changeset
40 its arguments.
a81b6db6d33c changes for the Mac
Guido van Rossum <guido@python.org>
parents: 578
diff changeset
41 """
a81b6db6d33c changes for the Mac
Guido van Rossum <guido@python.org>
parents: 578
diff changeset
42
a81b6db6d33c changes for the Mac
Guido van Rossum <guido@python.org>
parents: 578
diff changeset
43 if not _cache.has_key(pat):
573
4cb55356706f Rewritten using regex.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
44 res = translate(pat)
2260
a81b6db6d33c changes for the Mac
Guido van Rossum <guido@python.org>
parents: 578
diff changeset
45 import regex
573
4cb55356706f Rewritten using regex.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
46 save_syntax = regex.set_syntax(0)
2260
a81b6db6d33c changes for the Mac
Guido van Rossum <guido@python.org>
parents: 578
diff changeset
47 _cache[pat] = regex.compile(res)
573
4cb55356706f Rewritten using regex.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
48 save_syntax = regex.set_syntax(save_syntax)
2260
a81b6db6d33c changes for the Mac
Guido van Rossum <guido@python.org>
parents: 578
diff changeset
49 return _cache[pat].match(name) == len(name)
106
64373c79c099 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
50
573
4cb55356706f Rewritten using regex.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
51 def translate(pat):
2260
a81b6db6d33c changes for the Mac
Guido van Rossum <guido@python.org>
parents: 578
diff changeset
52 """Translate a shell PATTERN to a regular expression.
a81b6db6d33c changes for the Mac
Guido van Rossum <guido@python.org>
parents: 578
diff changeset
53
a81b6db6d33c changes for the Mac
Guido van Rossum <guido@python.org>
parents: 578
diff changeset
54 There is no way to quote meta-characters.
a81b6db6d33c changes for the Mac
Guido van Rossum <guido@python.org>
parents: 578
diff changeset
55 """
a81b6db6d33c changes for the Mac
Guido van Rossum <guido@python.org>
parents: 578
diff changeset
56
559
683edb0c4463 New == syntax
Guido van Rossum <guido@python.org>
parents: 193
diff changeset
57 i, n = 0, len(pat)
573
4cb55356706f Rewritten using regex.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
58 res = ''
559
683edb0c4463 New == syntax
Guido van Rossum <guido@python.org>
parents: 193
diff changeset
59 while i < n:
106
64373c79c099 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
60 c = pat[i]
573
4cb55356706f Rewritten using regex.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
61 i = i+1
559
683edb0c4463 New == syntax
Guido van Rossum <guido@python.org>
parents: 193
diff changeset
62 if c == '*':
573
4cb55356706f Rewritten using regex.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
63 res = res + '.*'
559
683edb0c4463 New == syntax
Guido van Rossum <guido@python.org>
parents: 193
diff changeset
64 elif c == '?':
573
4cb55356706f Rewritten using regex.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
65 res = res + '.'
559
683edb0c4463 New == syntax
Guido van Rossum <guido@python.org>
parents: 193
diff changeset
66 elif c == '[':
573
4cb55356706f Rewritten using regex.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
67 j = i
4cb55356706f Rewritten using regex.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
68 if j < n and pat[j] == '!':
4cb55356706f Rewritten using regex.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
69 j = j+1
4cb55356706f Rewritten using regex.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
70 if j < n and pat[j] == ']':
4cb55356706f Rewritten using regex.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
71 j = j+1
4cb55356706f Rewritten using regex.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
72 while j < n and pat[j] != ']':
4cb55356706f Rewritten using regex.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
73 j = j+1
4cb55356706f Rewritten using regex.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
74 if j >= n:
4cb55356706f Rewritten using regex.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
75 res = res + '\\['
4cb55356706f Rewritten using regex.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
76 else:
4cb55356706f Rewritten using regex.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
77 stuff = pat[i:j]
4cb55356706f Rewritten using regex.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
78 i = j+1
4cb55356706f Rewritten using regex.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
79 if stuff[0] == '!':
4cb55356706f Rewritten using regex.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
80 stuff = '[^' + stuff[1:] + ']'
4cb55356706f Rewritten using regex.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
81 elif stuff == '^'*len(stuff):
4cb55356706f Rewritten using regex.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
82 stuff = '\\^'
4cb55356706f Rewritten using regex.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
83 else:
4cb55356706f Rewritten using regex.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
84 while stuff[0] == '^':
4cb55356706f Rewritten using regex.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
85 stuff = stuff[1:] + stuff[0]
4cb55356706f Rewritten using regex.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
86 stuff = '[' + stuff + ']'
4cb55356706f Rewritten using regex.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
87 res = res + stuff
4cb55356706f Rewritten using regex.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
88 elif c in '\\.+^$':
4cb55356706f Rewritten using regex.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
89 res = res + ('\\' + c)
106
64373c79c099 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
90 else:
573
4cb55356706f Rewritten using regex.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
91 res = res + c
106
64373c79c099 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
92 return res