]>
git.proxmox.com Git - mirror_edk2.git/blob - AppPkg/Applications/Python/Python-2.7.10/Lib/fnmatch.py
1 """Filename matching with shell patterns.
3 fnmatch(FILENAME, PATTERN) matches according to the local convention.
4 fnmatchcase(FILENAME, PATTERN) always takes case in account.
6 The functions operate by translating the pattern into a regular
7 expression. They cache the compiled regular expressions for speed.
9 The function translate(PATTERN) returns a regular expression
10 corresponding to PATTERN. (It does not compile it.)
15 __all__
= ["filter", "fnmatch", "fnmatchcase", "translate"]
21 """Clear the pattern cache"""
24 def fnmatch(name
, pat
):
25 """Test whether FILENAME matches PATTERN.
27 Patterns are Unix shell style:
30 ? matches any single character
31 [seq] matches any character in seq
32 [!seq] matches any char not in seq
34 An initial period in FILENAME is not special.
35 Both FILENAME and PATTERN are first case-normalized
36 if the operating system requires it.
37 If you don't want this, use fnmatchcase(FILENAME, PATTERN).
41 name
= os
.path
.normcase(name
)
42 pat
= os
.path
.normcase(pat
)
43 return fnmatchcase(name
, pat
)
45 def filter(names
, pat
):
46 """Return the subset of the list NAMES that match PAT"""
49 pat
=os
.path
.normcase(pat
)
54 if len(_cache
) >= _MAXCACHE
:
56 _cache
[pat
] = re_pat
= re
.compile(res
)
58 if os
.path
is posixpath
:
59 # normcase on posix is NOP. Optimize it away from the loop.
65 if match(os
.path
.normcase(name
)):
69 def fnmatchcase(name
, pat
):
70 """Test whether FILENAME matches PATTERN, including case.
72 This is a version of fnmatch() which doesn't case-normalize
80 if len(_cache
) >= _MAXCACHE
:
82 _cache
[pat
] = re_pat
= re
.compile(res
)
83 return re_pat
.match(name
) is not None
86 """Translate a shell PATTERN to a regular expression.
88 There is no way to quote meta-characters.
102 if j
< n
and pat
[j
] == '!':
104 if j
< n
and pat
[j
] == ']':
106 while j
< n
and pat
[j
] != ']':
111 stuff
= pat
[i
:j
].replace('\\','\\\\')
114 stuff
= '^' + stuff
[1:]
115 elif stuff
[0] == '^':
117 res
= '%s[%s]' % (res
, stuff
)
119 res
= res
+ re
.escape(c
)
120 return res
+ '\Z(?ms)'