match end for single arguments finished

pull/241/head
William Ting 11 years ago
parent d9b205c935
commit e51f595018

@ -43,6 +43,7 @@ from utils import first
from utils import has_uppercase
from utils import is_osx
from utils import print_entry
from utils import second
VERSION = 'release-v21.8.0'
Entry = namedtuple('Entry', ['path', 'weight'])
@ -201,16 +202,27 @@ def find_matches(config, needles, count=1):
if not needles:
return first(data).path
ignore_case = detect_smartcase(needles)
sanitize = lambda x: decode(x).rstrip(os.sep)
needles = imap(sanitize, needles)
needles = map(sanitize, needles)
ignore_case = detect_smartcase(needles)
exact_matches = match_exact_regex(needles, data, ignore_case)
if len(needles) == 1:
# if one needle, match only against the last directory of each path
exact_matches = match_end(first(needles), data, ignore_case)
else:
exact_matches = match_regex(needles, data, ignore_case)
return first(exact_matches).path
def match_exact_regex(needles, haystack, ignore_case=False):
def match_end(needle, haystack, ignore_case=False):
"""Matches needle against the last directory of path."""
get_last_dir = lambda entry: second(os.path.split(entry.path))
has_needle = lambda entry: needle in get_last_dir(entry)
return ifilter(has_needle, haystack)
def match_regex(needles, haystack, ignore_case=False):
"""
Performs an exact match by combining all arguments into a single regex
expression and finding matches.
@ -226,8 +238,8 @@ def match_exact_regex(needles, haystack, ignore_case=False):
"""
regex_needle = '.*' + '.*'.join(needles) + '.*'
regex_flags = re.IGNORECASE | re.UNICODE if ignore_case else re.UNICODE
find = lambda haystack: re.search(regex_needle, haystack.path, flags=regex_flags)
return ifilter(find, haystack)
has_needle = lambda haystack: re.search(regex_needle, haystack.path, flags=regex_flags)
return ifilter(has_needle, haystack)
def purge_missing_paths(config):

@ -88,6 +88,15 @@ def print_entry(path, weight):
print(encode_local("%.1f:\t%s" % (weight, path)))
def second(xs):
it = iter(xs)
try:
it.next()
return it.next()
except StopIteration:
return None
def take(n, iterable):
"""Return first n items of an iterable."""
return islice(iterable, n)

Loading…
Cancel
Save