diff --git a/info.plist b/info.plist index a8e629a..36eb496 100644 --- a/info.plist +++ b/info.plist @@ -95,7 +95,7 @@ runningsubtext script - python pass-filter.py "{query}" + python3 pass-filter.py "{query}" scriptargtype 0 scriptfile diff --git a/pass-filter.py b/pass-filter.py index 4728377..e2a2311 100755 --- a/pass-filter.py +++ b/pass-filter.py @@ -1,60 +1,36 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 import fnmatch import os import sys -import string - -fuzzysearch = True -try: - from fuzzywuzzy import process -except: - fuzzysearch = False +import re +# import string QUERY = sys.argv[1] HOME = os.environ['HOME'] -PASS_DIR = os.environ.get('PASSWORD_STORE_DIR',os.path.join(HOME, '.password-store/')) +PASS_DIR = os.environ.get('PASSWORD_STORE_DIR', os.path.join(HOME, '.password-store/')) # TODO: list_passwords creates cache of passwords for first time def list_passwords(): ret = [] - for root, dirnames, filenames in os.walk(PASS_DIR, True, None, True): + for root, _, filenames in os.walk(PASS_DIR): for filename in fnmatch.filter(filenames, '*.gpg'): ret.append(os.path.join(root, filename.replace('.gpg','')).replace(PASS_DIR, '')) return sorted(ret, key=lambda s: s.lower()) def search_passwords(query): - ''' Search passwords using the Fuzzy search method if fuzzywuzzy is available, - or default to the filter-based search otherwise''' - if fuzzysearch: - return search_passwords_fuzzy(query) - return search_passwords_filter(query) - - -def search_passwords_fuzzy(query): - ''' Search passwords using the Fuzzy search method using fuzzywuzzy''' - passwords = list_passwords() - return [entry[0] for entry in process.extract(query, passwords)] - - -def search_passwords_filter(query): - ''' Search passwords using the filter-based search, which doesn't require fuzzywuzzy''' ret = [] - terms = filter(lambda x: x, query.lower().split()) passwords = list_passwords() + regex = ".*{}.*".format(query) for password in passwords: - for t in terms: - if t not in password.lower(): - break - else: + if re.match(regex, password): ret.append(password) - return ret @@ -62,7 +38,7 @@ def xmlize_items(items, query): items_a = [] for item in items: - list = string.rsplit(item, "/", 1) + list = item.split("/", 1) name = list[-1] path = item if len(list) == 2 else "" @@ -88,5 +64,4 @@ def xmlize_items(items, query): items = search_passwords(QUERY) -print xmlize_items(items, QUERY) - +print (xmlize_items(items, QUERY))