From 41140232602ce150756e417d22f98cac419789d6 Mon Sep 17 00:00:00 2001 From: Brian Cappello Date: Tue, 14 Aug 2018 19:21:16 -0600 Subject: [PATCH] add support to util.pathmatch for matching the start of a string --- babel/util.py | 21 ++++++++++++++++++++- tests/test_util.py | 5 +++++ 2 files changed, 25 insertions(+), 1 deletion(-) 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(): -- 2.47.2