Improved handling multiple directory needles

pull/444/head
Calum Smith 8 years ago
parent 0ecf1b4e3b
commit 575b14a13f

@ -20,14 +20,15 @@ else:
def match_anywhere(needles, haystack, ignore_case=False):
"""
Matches needles anywhere in the path as long as they're in the same (but
not necessary consecutive) order.
not necessary consecutive) order, and the last needle is at the end.
For example:
needles = ['foo', 'baz']
regex needle = r'.*foo.*baz.*'
regex needle = r'foo.*/.*baz[^/]*$'
haystack = [
(path='/foo/bar/baz', weight=10),
(path='/baz/foo/bar', weight=10),
(path='/foo/baz/bar', weight=11),
(path='/foo/baz', weight=10),
]
@ -36,8 +37,9 @@ def match_anywhere(needles, haystack, ignore_case=False):
(path='/foo/baz', weight=10),
]
"""
regex_needle = '.*' + '.*'.join(imap(re.escape, needles)) + '.*'
regex_flags = re.IGNORECASE | re.UNICODE if ignore_case else re.UNICODE
regex_some_seps = '.*' + os.sep + '.*'
regex_no_sep_end = '[^' + os.sep + ']*$'
regex_needle = regex_some_seps.join(imap(re.escape, needles)) + regex_no_sep_end
found = lambda haystack: re.search(
regex_needle,
haystack.path,

Loading…
Cancel
Save