Merge pull request #3 from jle64/fuzzy-search

Fuzzy search
This commit is contained in:
jle64 2017-09-04 15:57:16 +02:00 committed by GitHub
commit ca79f47f47

View File

@ -25,9 +25,11 @@
# 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 from os.path import expanduser
from os.path import join as path_join
import re import re
import subprocess import subprocess
@ -81,22 +83,26 @@ class SearchPassService(dbus.service.Object):
pass pass
def get_result_set(self, terms): def get_result_set(self, terms):
names = [] name = ' '.join(terms)
for term in terms: matcher = difflib.SequenceMatcher(b=name, autojunk=False)
names += self.get_password_names(term) matches = {}
return set(names)
def get_password_names(self, name):
names = []
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:]
for file in files:
file_path = '{0}/{1}'.format(dir_path, file) if dir_path.startswith('.'):
if re.match(r'.*{0}.*\.gpg$'.format(name), continue
file_path,
re.IGNORECASE): for filename in files:
names.append(file_path[:-4]) path = path_join(dir_path, filename)[:-4]
return names for name in path.split('/'):
matcher.set_seq1(name)
score = matcher.ratio()
if score >= 0.5 and (path not in matches or score > matches[path]):
matches[path] = score
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(