1991-01-01 19:11:14 +01:00
|
|
|
# module 'fnmatch' -- filename matching with shell patterns
|
|
|
|
|
|
|
|
def fnmatch(name, pat):
|
1991-04-07 15:40:02 +02:00
|
|
|
#
|
|
|
|
# Check for simple case: no special characters
|
|
|
|
#
|
|
|
|
if not ('*' in pat or '?' in pat or '[' in pat):
|
1992-01-01 20:35:13 +01:00
|
|
|
return name == pat
|
1991-04-07 15:40:02 +02:00
|
|
|
#
|
|
|
|
# Check for common cases: *suffix and prefix*
|
|
|
|
#
|
1992-01-01 20:35:13 +01:00
|
|
|
if pat[0] == '*':
|
1991-04-07 15:40:02 +02:00
|
|
|
p1 = pat[1:]
|
|
|
|
if not ('*' in p1 or '?' in p1 or '[' in p1):
|
|
|
|
start = len(name) - len(p1)
|
1992-01-01 20:35:13 +01:00
|
|
|
return start >= 0 and name[start:] == p1
|
|
|
|
elif pat[-1:] == '*':
|
1991-04-07 15:40:02 +02:00
|
|
|
p1 = pat[:-1]
|
|
|
|
if not ('*' in p1 or '?' in p1 or '[' in p1):
|
1992-01-01 20:35:13 +01:00
|
|
|
return name[:len(p1)] == p1
|
1991-04-07 15:40:02 +02:00
|
|
|
#
|
|
|
|
# General case
|
|
|
|
#
|
|
|
|
return fnmatch1(name, pat)
|
1991-01-01 19:11:14 +01:00
|
|
|
|
|
|
|
def fnmatch1(name, pat):
|
1992-01-01 20:35:13 +01:00
|
|
|
i, n = 0, len(pat)
|
|
|
|
while i < n:
|
1991-01-01 19:11:14 +01:00
|
|
|
c = pat[i]
|
1992-01-01 20:35:13 +01:00
|
|
|
if c == '*':
|
1991-04-07 15:40:02 +02:00
|
|
|
p1 = pat[i+1:]
|
|
|
|
if not ('*' in p1 or '?' in p1 or '[' in p1):
|
|
|
|
start = len(name) - len(p1)
|
1992-01-01 20:35:13 +01:00
|
|
|
return start >= 0 and name[start:] == p1
|
1991-04-07 15:40:02 +02:00
|
|
|
for i in range(i, len(name) + 1):
|
|
|
|
if fnmatch1(name[i:], p1):
|
|
|
|
return 1
|
|
|
|
return 0
|
1992-01-01 20:35:13 +01:00
|
|
|
elif c == '?':
|
1991-01-01 19:11:14 +01:00
|
|
|
if len(name) <= i : return 0
|
1992-01-01 20:35:13 +01:00
|
|
|
elif c == '[':
|
1991-04-07 15:40:02 +02:00
|
|
|
c, rest = name[i], name[i+1:]
|
|
|
|
i, n = i+1, len(pat) - 1
|
|
|
|
match = 0
|
|
|
|
exclude = 0
|
1992-01-01 20:35:13 +01:00
|
|
|
if i < n and pat[i] == '!':
|
1991-04-07 15:40:02 +02:00
|
|
|
exclude = 1
|
|
|
|
i = i+1
|
|
|
|
while i < n:
|
1992-01-01 20:35:13 +01:00
|
|
|
if pat[i] == c: match = 1
|
1991-04-07 15:40:02 +02:00
|
|
|
i = i+1
|
1992-01-01 20:35:13 +01:00
|
|
|
if i >= n or pat[i] == ']':
|
1991-04-07 15:40:02 +02:00
|
|
|
break
|
1992-01-01 20:35:13 +01:00
|
|
|
if pat[i] == '-':
|
1991-04-07 15:40:02 +02:00
|
|
|
i = i+1
|
1992-01-01 20:35:13 +01:00
|
|
|
if i >= n or pat[i] == ']':
|
1991-04-07 15:40:02 +02:00
|
|
|
break
|
1992-01-01 20:35:13 +01:00
|
|
|
if pat[i-2] <= c <= pat[i]:
|
|
|
|
match = 1
|
1991-04-07 15:40:02 +02:00
|
|
|
i = i+1
|
1992-01-01 20:35:13 +01:00
|
|
|
if i >= n or pat[i] == ']':
|
|
|
|
break
|
|
|
|
if match == exclude:
|
1991-04-07 15:40:02 +02:00
|
|
|
return 0
|
|
|
|
return fnmatch1(rest, pat[i+1:])
|
1991-01-01 19:11:14 +01:00
|
|
|
else:
|
|
|
|
if name[i:i+1] <> c:
|
|
|
|
return 0
|
1992-01-01 20:35:13 +01:00
|
|
|
i = i+1
|
|
|
|
# We don't get here if the pattern contained * or [...]
|
|
|
|
return i >= len(name)
|
1991-01-01 19:11:14 +01:00
|
|
|
|
|
|
|
def fnmatchlist(names, pat):
|
|
|
|
res = []
|
|
|
|
for name in names:
|
|
|
|
if fnmatch(name, pat): res.append(name)
|
|
|
|
return res
|