shellpattern: add match_end arg

match_end=r"\Z" is the default, same behaviour as before
(create a full match up to the string end from the globbing pattern).

match_end=otherregex can be flexibly used to match anything else
after the regex generated from the globbing pattern.
This commit is contained in:
Thomas Waldmann 2017-07-03 01:14:53 +02:00
parent 35a3a6adfb
commit d33b853f66
2 changed files with 16 additions and 2 deletions

View File

@ -2,7 +2,7 @@ import os
import re
def translate(pat):
def translate(pat, match_end=r"\Z"):
"""Translate a shell-style pattern to a regular expression.
The pattern may include ``**<sep>`` (<sep> stands for the platform-specific path separator; "/" on POSIX systems) for
@ -10,6 +10,9 @@ def translate(pat):
any path separator. Wrap meta-characters in brackets for a literal match (i.e. "[?]" to match the literal character
"?").
Using match_end=regex one can give a regular expression that is used to match after the regex that is generated from
the pattern. The default is to match the end of the string.
This function is derived from the "fnmatch" module distributed with the Python standard library.
Copyright (C) 2001-2017 Python Software Foundation. All rights reserved.
@ -59,4 +62,4 @@ def translate(pat):
else:
res += re.escape(c)
return res + r"\Z(?ms)"
return res + match_end + "(?ms)"

View File

@ -111,3 +111,14 @@ def test_match(path, patterns):
def test_mismatch(path, patterns):
for p in patterns:
assert not check(path, p)
def test_match_end():
regex = shellpattern.translate("*-home") # default is match_end == string end
assert re.match(regex, '2017-07-03-home')
assert not re.match(regex, '2017-07-03-home.checkpoint')
match_end = r'(%s)?\Z' % r'\.checkpoint(\.\d+)?' # with/without checkpoint ending
regex = shellpattern.translate("*-home", match_end=match_end)
assert re.match(regex, '2017-07-03-home')
assert re.match(regex, '2017-07-03-home.checkpoint')