Skip to content

Commit

Permalink
Apply CGenie#30
Browse files Browse the repository at this point in the history
  • Loading branch information
chipsenkbeil committed Jun 12, 2022
1 parent c2195d1 commit 1343203
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 34 deletions.
2 changes: 1 addition & 1 deletion info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@
<key>runningsubtext</key>
<string></string>
<key>script</key>
<string>python pass-filter.py "{query}"</string>
<string>python3 pass-filter.py "{query}"</string>
<key>scriptargtype</key>
<integer>0</integer>
<key>scriptfile</key>
Expand Down
42 changes: 9 additions & 33 deletions pass-filter.py
Original file line number Diff line number Diff line change
@@ -1,68 +1,44 @@
#!/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


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 ""

Expand All @@ -88,5 +64,5 @@ def xmlize_items(items, query):


items = search_passwords(QUERY)
print xmlize_items(items, QUERY)
print (xmlize_items(items, QUERY))

0 comments on commit 1343203

Please sign in to comment.