]> git.ipfire.org Git - thirdparty/babel.git/commitdiff
add support to util.pathmatch for matching the start of a string 600/head
authorBrian Cappello <briancappello@gmail.com>
Wed, 15 Aug 2018 01:21:16 +0000 (19:21 -0600)
committerBrian Cappello <briancappello@gmail.com>
Wed, 15 Aug 2018 01:21:16 +0000 (19:21 -0600)
babel/util.py
tests/test_util.py

index 0150827de479b7ce8a997b92100ddde540d712ec..fb93f16d5790114011e1fab625f66459eec4858f 100644 (file)
@@ -151,6 +151,16 @@ def pathmatch(pattern, filename):
     >>> pathmatch('**.py', 'templates/index.html')
     False
 
+    >>> pathmatch('./foo/**.py', 'foo/bar/baz.py')
+    True
+    >>> pathmatch('./foo/**.py', 'bar/baz.py')
+    False
+
+    >>> pathmatch('^foo/**.py', 'foo/bar/baz.py')
+    True
+    >>> pathmatch('^foo/**.py', 'bar/baz.py')
+    False
+
     >>> pathmatch('**/templates/*.html', 'templates/index.html')
     True
     >>> pathmatch('**/templates/*.html', 'templates/foo/bar.html')
@@ -167,7 +177,16 @@ def pathmatch(pattern, filename):
         '**/': '(?:.+/)*?',
         '**': '(?:.+/)*?[^/]+',
     }
-    buf = []
+
+    if pattern.startswith('^'):
+        buf = ['^']
+        pattern = pattern[1:]
+    elif pattern.startswith('./'):
+        buf = ['^']
+        pattern = pattern[2:]
+    else:
+        buf = []
+
     for idx, part in enumerate(re.split('([?*]+/?)', pattern)):
         if idx % 2:
             buf.append(symbols[part])
index a3607d5fc68d421f5df0592c741b08d553528b1e..ef591102e8b57e1995a741e9745fbe7068ae7cb3 100644 (file)
@@ -31,6 +31,11 @@ def test_pathmatch():
     assert not util.pathmatch('**.py', 'templates/index.html')
     assert util.pathmatch('**/templates/*.html', 'templates/index.html')
     assert not util.pathmatch('**/templates/*.html', 'templates/foo/bar.html')
+    assert util.pathmatch('^foo/**.py', 'foo/bar/baz/blah.py')
+    assert not util.pathmatch('^foo/**.py', 'blah/foo/bar/baz.py')
+    assert util.pathmatch('./foo/**.py', 'foo/bar/baz/blah.py')
+    assert util.pathmatch('./blah.py', 'blah.py')
+    assert not util.pathmatch('./foo/**.py', 'blah/foo/bar/baz.py')
 
 
 def test_odict_pop():