From: Brian Cappello Date: Wed, 15 Aug 2018 01:21:16 +0000 (-0600) Subject: add support to util.pathmatch for matching the start of a string X-Git-Tag: v2.7.0~26^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=refs%2Fpull%2F600%2Fhead;p=thirdparty%2Fbabel.git add support to util.pathmatch for matching the start of a string --- diff --git a/babel/util.py b/babel/util.py index 0150827d..fb93f16d 100644 --- a/babel/util.py +++ b/babel/util.py @@ -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]) diff --git a/tests/test_util.py b/tests/test_util.py index a3607d5f..ef591102 100644 --- a/tests/test_util.py +++ b/tests/test_util.py @@ -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():