Make fuzzy matching happen on every path element instead of on whole

path.
This commit is contained in:
Jonathan Lestrelin 2017-09-04 15:08:17 +02:00
parent 119a5c42f0
commit 7b1a5189c7

View File

@ -25,11 +25,12 @@
# Copyright (C) 2012 Red Hat, Inc. # Copyright (C) 2012 Red Hat, Inc.
# Author: Luke Macken <lmacken@redhat.com> # Author: Luke Macken <lmacken@redhat.com>
import difflib
from os import getenv from os import getenv
from os import walk from os import walk
from os.path import expanduser, join as path_join from os.path import expanduser
from os.path import join as path_join
import re import re
import difflib
import subprocess import subprocess
import dbus import dbus
@ -84,7 +85,7 @@ class SearchPassService(dbus.service.Object):
def get_result_set(self, terms): def get_result_set(self, terms):
name = ' '.join(terms) name = ' '.join(terms)
matcher = difflib.SequenceMatcher(b=name, autojunk=False) matcher = difflib.SequenceMatcher(b=name, autojunk=False)
matches = [] matches = {}
for root, dirs, files in walk(self.password_store): for root, dirs, files in walk(self.password_store):
dir_path = root[len(self.password_store) + 1:] dir_path = root[len(self.password_store) + 1:]
@ -93,14 +94,15 @@ class SearchPassService(dbus.service.Object):
continue continue
for filename in files: for filename in files:
path = path_join(dir_path, filename) path = path_join(dir_path, filename)[:-4]
matcher.set_seq1(path[:-4]) for name in path.split('/'):
score = matcher.ratio() matcher.set_seq1(name)
score = matcher.ratio()
if score >= 0.5: if score >= 0.5 and (path not in matches or score > matches[path]):
matches.append((score, path[:-4])) matches[path] = score
return [x[1] for x in sorted(matches, key=lambda x: x[0], reverse=True)] return sorted(matches, key=matches.__getitem__, reverse=True)
def send_password_to_gpaste(self, name): def send_password_to_gpaste(self, name):
pass_cmd = subprocess.run( pass_cmd = subprocess.run(