From 30249771b724ea469b45a6d8699742d46f3500fc Mon Sep 17 00:00:00 2001 From: Olivier Mehani Date: Mon, 19 Nov 2018 22:15:28 +1100 Subject: [PATCH] Replace straight difflib with fuzzywuzzy Signed-off-by: Olivier Mehani --- gnome-pass-search-provider.py | 20 +++++--------------- install.sh | 3 +++ 2 files changed, 8 insertions(+), 15 deletions(-) diff --git a/gnome-pass-search-provider.py b/gnome-pass-search-provider.py index 548f55c..3513e7c 100755 --- a/gnome-pass-search-provider.py +++ b/gnome-pass-search-provider.py @@ -25,7 +25,7 @@ # Copyright (C) 2012 Red Hat, Inc. # Author: Luke Macken -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( diff --git a/install.sh b/install.sh index 122f182..95005f3 100755 --- a/install.sh +++ b/install.sh @@ -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