Explorar el Código

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.
Thomas Waldmann hace 8 años
padre
commit
d33b853f66
Se han modificado 2 ficheros con 16 adiciones y 2 borrados
  1. 5 2
      src/borg/shellpattern.py
  2. 11 0
      src/borg/testsuite/shellpattern.py

+ 5 - 2
src/borg/shellpattern.py

@@ -2,7 +2,7 @@ import os
 import re
 import re
 
 
 
 
-def translate(pat):
+def translate(pat, match_end=r"\Z"):
     """Translate a shell-style pattern to a regular expression.
     """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
     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
     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.
     This function is derived from the "fnmatch" module distributed with the Python standard library.
 
 
     Copyright (C) 2001-2017 Python Software Foundation. All rights reserved.
     Copyright (C) 2001-2017 Python Software Foundation. All rights reserved.
@@ -59,4 +62,4 @@ def translate(pat):
         else:
         else:
             res += re.escape(c)
             res += re.escape(c)
 
 
-    return res + r"\Z(?ms)"
+    return res + match_end + "(?ms)"

+ 11 - 0
src/borg/testsuite/shellpattern.py

@@ -111,3 +111,14 @@ def test_match(path, patterns):
 def test_mismatch(path, patterns):
 def test_mismatch(path, patterns):
     for p in patterns:
     for p in patterns:
         assert not check(path, p)
         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')