annotate Lib/fnmatch.py @ 54294:dba767bc3155 legacy-trunk

remove mention of old ctypes version
author Benjamin Peterson <benjamin@python.org>
date Sat, 23 May 2009 20:59:09 +0000
parents 2bb523ad9b76
children 93c7f650937f 8a7c930abab6
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
6519
24da035ceb6e Convert all remaining *simple* cases of regex usage to re usage.
Guido van Rossum <guido@python.org>
parents: 2260
diff changeset
13 import re
24da035ceb6e Convert all remaining *simple* cases of regex usage to re usage.
Guido van Rossum <guido@python.org>
parents: 2260
diff changeset
14
29847
2bb523ad9b76 SF patch #765238: fix fnmatch.__all__
Raymond Hettinger <python@rcn.com>
parents: 23620
diff changeset
15 __all__ = ["filter", "fnmatch","fnmatchcase","translate"]
16072
bae889f44cd1 more __all__ updates
Skip Montanaro <skip@pobox.com>
parents: 15867
diff changeset
16
2260
a81b6db6d33c changes for the Mac
Guido van Rossum <guido@python.org>
parents: 578
diff changeset
17 _cache = {}
106
64373c79c099 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
18
64373c79c099 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
19 def fnmatch(name, pat):
15867
f5a3d98a35bc Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13050
diff changeset
20 """Test whether FILENAME matches PATTERN.
f5a3d98a35bc Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13050
diff changeset
21
f5a3d98a35bc Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13050
diff changeset
22 Patterns are Unix shell style:
f5a3d98a35bc Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13050
diff changeset
23
f5a3d98a35bc Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13050
diff changeset
24 * matches everything
f5a3d98a35bc Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13050
diff changeset
25 ? matches any single character
f5a3d98a35bc Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13050
diff changeset
26 [seq] matches any character in seq
f5a3d98a35bc Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13050
diff changeset
27 [!seq] matches any char not in seq
f5a3d98a35bc Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13050
diff changeset
28
f5a3d98a35bc Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13050
diff changeset
29 An initial period in FILENAME is not special.
f5a3d98a35bc Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13050
diff changeset
30 Both FILENAME and PATTERN are first case-normalized
f5a3d98a35bc Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13050
diff changeset
31 if the operating system requires it.
f5a3d98a35bc Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13050
diff changeset
32 If you don't want this, use fnmatchcase(FILENAME, PATTERN).
f5a3d98a35bc Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13050
diff changeset
33 """
f5a3d98a35bc Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13050
diff changeset
34
f5a3d98a35bc Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13050
diff changeset
35 import os
f5a3d98a35bc Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13050
diff changeset
36 name = os.path.normcase(name)
f5a3d98a35bc Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13050
diff changeset
37 pat = os.path.normcase(pat)
f5a3d98a35bc Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13050
diff changeset
38 return fnmatchcase(name, pat)
2260
a81b6db6d33c changes for the Mac
Guido van Rossum <guido@python.org>
parents: 578
diff changeset
39
18040
fb93dfbd8052 Patch #409973: Speedup glob.glob, add fnmatch.filter.
Martin v. Löwis <martin@v.loewis.de>
parents: 17107
diff changeset
40 def filter(names, pat):
fb93dfbd8052 Patch #409973: Speedup glob.glob, add fnmatch.filter.
Martin v. Löwis <martin@v.loewis.de>
parents: 17107
diff changeset
41 """Return the subset of the list NAMES that match PAT"""
fb93dfbd8052 Patch #409973: Speedup glob.glob, add fnmatch.filter.
Martin v. Löwis <martin@v.loewis.de>
parents: 17107
diff changeset
42 import os,posixpath
fb93dfbd8052 Patch #409973: Speedup glob.glob, add fnmatch.filter.
Martin v. Löwis <martin@v.loewis.de>
parents: 17107
diff changeset
43 result=[]
fb93dfbd8052 Patch #409973: Speedup glob.glob, add fnmatch.filter.
Martin v. Löwis <martin@v.loewis.de>
parents: 17107
diff changeset
44 pat=os.path.normcase(pat)
23620
a1de0bab5475 SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents: 18040
diff changeset
45 if not pat in _cache:
18040
fb93dfbd8052 Patch #409973: Speedup glob.glob, add fnmatch.filter.
Martin v. Löwis <martin@v.loewis.de>
parents: 17107
diff changeset
46 res = translate(pat)
fb93dfbd8052 Patch #409973: Speedup glob.glob, add fnmatch.filter.
Martin v. Löwis <martin@v.loewis.de>
parents: 17107
diff changeset
47 _cache[pat] = re.compile(res)
fb93dfbd8052 Patch #409973: Speedup glob.glob, add fnmatch.filter.
Martin v. Löwis <martin@v.loewis.de>
parents: 17107
diff changeset
48 match=_cache[pat].match
fb93dfbd8052 Patch #409973: Speedup glob.glob, add fnmatch.filter.
Martin v. Löwis <martin@v.loewis.de>
parents: 17107
diff changeset
49 if os.path is posixpath:
fb93dfbd8052 Patch #409973: Speedup glob.glob, add fnmatch.filter.
Martin v. Löwis <martin@v.loewis.de>
parents: 17107
diff changeset
50 # normcase on posix is NOP. Optimize it away from the loop.
fb93dfbd8052 Patch #409973: Speedup glob.glob, add fnmatch.filter.
Martin v. Löwis <martin@v.loewis.de>
parents: 17107
diff changeset
51 for name in names:
fb93dfbd8052 Patch #409973: Speedup glob.glob, add fnmatch.filter.
Martin v. Löwis <martin@v.loewis.de>
parents: 17107
diff changeset
52 if match(name):
fb93dfbd8052 Patch #409973: Speedup glob.glob, add fnmatch.filter.
Martin v. Löwis <martin@v.loewis.de>
parents: 17107
diff changeset
53 result.append(name)
fb93dfbd8052 Patch #409973: Speedup glob.glob, add fnmatch.filter.
Martin v. Löwis <martin@v.loewis.de>
parents: 17107
diff changeset
54 else:
fb93dfbd8052 Patch #409973: Speedup glob.glob, add fnmatch.filter.
Martin v. Löwis <martin@v.loewis.de>
parents: 17107
diff changeset
55 for name in names:
fb93dfbd8052 Patch #409973: Speedup glob.glob, add fnmatch.filter.
Martin v. Löwis <martin@v.loewis.de>
parents: 17107
diff changeset
56 if match(os.path.normcase(name)):
fb93dfbd8052 Patch #409973: Speedup glob.glob, add fnmatch.filter.
Martin v. Löwis <martin@v.loewis.de>
parents: 17107
diff changeset
57 result.append(name)
fb93dfbd8052 Patch #409973: Speedup glob.glob, add fnmatch.filter.
Martin v. Löwis <martin@v.loewis.de>
parents: 17107
diff changeset
58 return result
fb93dfbd8052 Patch #409973: Speedup glob.glob, add fnmatch.filter.
Martin v. Löwis <martin@v.loewis.de>
parents: 17107
diff changeset
59
2260
a81b6db6d33c changes for the Mac
Guido van Rossum <guido@python.org>
parents: 578
diff changeset
60 def fnmatchcase(name, pat):
15867
f5a3d98a35bc Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13050
diff changeset
61 """Test whether FILENAME matches PATTERN, including case.
f5a3d98a35bc Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13050
diff changeset
62
f5a3d98a35bc Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13050
diff changeset
63 This is a version of fnmatch() which doesn't case-normalize
f5a3d98a35bc Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13050
diff changeset
64 its arguments.
f5a3d98a35bc Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13050
diff changeset
65 """
f5a3d98a35bc Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13050
diff changeset
66
23620
a1de0bab5475 SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents: 18040
diff changeset
67 if not pat in _cache:
15867
f5a3d98a35bc Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13050
diff changeset
68 res = translate(pat)
f5a3d98a35bc Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13050
diff changeset
69 _cache[pat] = re.compile(res)
f5a3d98a35bc Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13050
diff changeset
70 return _cache[pat].match(name) is not None
106
64373c79c099 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
71
573
4cb55356706f Rewritten using regex.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
72 def translate(pat):
15867
f5a3d98a35bc Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13050
diff changeset
73 """Translate a shell PATTERN to a regular expression.
f5a3d98a35bc Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13050
diff changeset
74
f5a3d98a35bc Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13050
diff changeset
75 There is no way to quote meta-characters.
f5a3d98a35bc Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13050
diff changeset
76 """
f5a3d98a35bc Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13050
diff changeset
77
f5a3d98a35bc Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13050
diff changeset
78 i, n = 0, len(pat)
f5a3d98a35bc Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13050
diff changeset
79 res = ''
f5a3d98a35bc Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13050
diff changeset
80 while i < n:
f5a3d98a35bc Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13050
diff changeset
81 c = pat[i]
f5a3d98a35bc Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13050
diff changeset
82 i = i+1
f5a3d98a35bc Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13050
diff changeset
83 if c == '*':
f5a3d98a35bc Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13050
diff changeset
84 res = res + '.*'
f5a3d98a35bc Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13050
diff changeset
85 elif c == '?':
f5a3d98a35bc Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13050
diff changeset
86 res = res + '.'
f5a3d98a35bc Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13050
diff changeset
87 elif c == '[':
f5a3d98a35bc Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13050
diff changeset
88 j = i
f5a3d98a35bc Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13050
diff changeset
89 if j < n and pat[j] == '!':
f5a3d98a35bc Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13050
diff changeset
90 j = j+1
f5a3d98a35bc Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13050
diff changeset
91 if j < n and pat[j] == ']':
f5a3d98a35bc Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13050
diff changeset
92 j = j+1
f5a3d98a35bc Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13050
diff changeset
93 while j < n and pat[j] != ']':
f5a3d98a35bc Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13050
diff changeset
94 j = j+1
f5a3d98a35bc Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13050
diff changeset
95 if j >= n:
f5a3d98a35bc Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13050
diff changeset
96 res = res + '\\['
f5a3d98a35bc Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13050
diff changeset
97 else:
17107
adc8a41bee9f Donovan Baarda <abo@users.sourceforge.net>:
Fred Drake <fdrake@acm.org>
parents: 16072
diff changeset
98 stuff = pat[i:j].replace('\\','\\\\')
15867
f5a3d98a35bc Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13050
diff changeset
99 i = j+1
f5a3d98a35bc Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13050
diff changeset
100 if stuff[0] == '!':
17107
adc8a41bee9f Donovan Baarda <abo@users.sourceforge.net>:
Fred Drake <fdrake@acm.org>
parents: 16072
diff changeset
101 stuff = '^' + stuff[1:]
adc8a41bee9f Donovan Baarda <abo@users.sourceforge.net>:
Fred Drake <fdrake@acm.org>
parents: 16072
diff changeset
102 elif stuff[0] == '^':
adc8a41bee9f Donovan Baarda <abo@users.sourceforge.net>:
Fred Drake <fdrake@acm.org>
parents: 16072
diff changeset
103 stuff = '\\' + stuff
adc8a41bee9f Donovan Baarda <abo@users.sourceforge.net>:
Fred Drake <fdrake@acm.org>
parents: 16072
diff changeset
104 res = '%s[%s]' % (res, stuff)
15867
f5a3d98a35bc Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13050
diff changeset
105 else:
f5a3d98a35bc Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13050
diff changeset
106 res = res + re.escape(c)
f5a3d98a35bc Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13050
diff changeset
107 return res + "$"