Replace straight difflib with fuzzywuzzy

Signed-off-by: Olivier Mehani <shtrom@ssji.net>
This commit is contained in:
Olivier Mehani 2018-11-19 22:15:28 +11:00
parent 80fc1e8609
commit 30249771b7
No known key found for this signature in database
GPG Key ID: 6F82C3C767D655AA
2 changed files with 8 additions and 15 deletions

View File

@ -25,7 +25,7 @@
# Copyright (C) 2012 Red Hat, Inc.
# Author: Luke Macken <lmacken@redhat.com>
import difflib
from fuzzywuzzy import process
from os import getenv
from os import walk
from os.path import expanduser
@ -83,9 +83,8 @@ class SearchPassService(dbus.service.Object):
pass
def get_result_set(self, terms):
name = ' '.join(terms)
matcher = difflib.SequenceMatcher(b=name, autojunk=False)
matches = {}
name = ''.join(terms)
password_list = []
for root, dirs, files in walk(self.password_store):
dir_path = root[len(self.password_store) + 1:]
@ -97,18 +96,9 @@ class SearchPassService(dbus.service.Object):
if filename[-4:] != '.gpg':
continue
path = path_join(dir_path, filename)[:-4]
for part in path.split('/'):
# smartcase: be case insensitive unless uppercase
# characters are used in the search
if name.islower():
part = part.lower()
matcher.set_seq1(part)
score = matcher.ratio()
if score >= 0.5 and \
(path not in matches or score > matches[path]):
matches[path] = score
password_list.append(path)
return sorted(matches, key=matches.__getitem__, reverse=True)
return [entry[0] for entry in process.extract(name, password_list, limit=5)]
def send_password_to_gpaste(self, name):
pass_cmd = subprocess.run(

View File

@ -6,6 +6,9 @@ DATADIR=${DATADIR:-/usr/share}
LIBDIR=${LIBDIR:-/usr/lib}
LIBEXECDIR=${LIBEXECDIR:-/usr/lib/}
SYSCONFDIR=${SYSCONFDIR:-/etc}
PIPARGS=${PIPARGS:-} # use --user for local install
pip install ${PIPARGS} fuzzywuzzy
install -Dm 0755 gnome-pass-search-provider.py "${LIBEXECDIR}"/gnome-pass-search-provider/gnome-pass-search-provider.py