Skip to content

Commit

Permalink
Merge pull request #73 from effigies/fix/full_search
Browse files Browse the repository at this point in the history
ENH: Add full_search option to get_nearest
  • Loading branch information
effigies authored Jul 3, 2018
2 parents ebfdb14 + 926fc93 commit a4eb518
Showing 1 changed file with 26 additions and 22 deletions.
48 changes: 26 additions & 22 deletions grabbit/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -833,7 +833,7 @@ def as_data_frame(self, **kwargs):
return data

def get_nearest(self, path, return_type='file', strict=True, all_=False,
ignore_strict_entities=None, **kwargs):
ignore_strict_entities=None, full_search=False, **kwargs):
''' Walk up the file tree from the specified path and return the
nearest matching file(s).
Expand Down Expand Up @@ -882,29 +882,33 @@ def count_matches(f):

matches = []

search_paths = []
while True:
if path in folders and folders[path]:

# Sort by number of matching entities. Also store number of
# common entities, for filtering when strict=True.
num_ents = [[f] + count_matches(f) for f in folders[path]]
# Filter out imperfect matches (i.e., where number of common
# entities does not equal number of matching entities).
if strict:
num_ents = [f for f in num_ents if f[1] == f[2]]
num_ents.sort(key=lambda x: x[2], reverse=True)

if num_ents:
matches.append(num_ents[0][0])

if not all_:
break
try:
_path, _ = split(path)
if _path == path:
break
path = _path
except Exception:
search_paths.append(path)
parent = dirname(path)
if parent == path:
break
path = parent

if full_search:
unchecked = set(folders.keys()) - set(search_paths)
search_paths.extend(path for path in unchecked if folders[path])

for path in search_paths:
# Sort by number of matching entities. Also store number of
# common entities, for filtering when strict=True.
num_ents = [[f] + count_matches(f) for f in folders[path]]
# Filter out imperfect matches (i.e., where number of common
# entities does not equal number of matching entities).
if strict:
num_ents = [f for f in num_ents if f[1] == f[2]]
num_ents.sort(key=lambda x: x[2], reverse=True)

if num_ents:
matches.append(num_ents[0][0])

if not all_:
break

matches = [m.path if return_type == 'file' else m.as_named_tuple()
Expand Down

0 comments on commit a4eb518

Please sign in to comment.