forked from albertlauncher/python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpass.py
100 lines (78 loc) · 2.55 KB
/
pass.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# -*- coding: utf-8 -*-
"""Manage passwords.
This is a 'pass' wrapper extension.
Synopsis:
<trigger> generate <location>
<trigger> <filter>"""
import fnmatch
import os
from shutil import which
from albertv0 import *
__iid__ = "PythonInterface/v0.3"
__prettyname__ = "Pass"
__version__ = "1.1"
__trigger__ = "pass "
__author__ = "Benedict Dudel"
__dependencies__ = ["pass"]
if which("pass") is None:
raise Exception("'pass' is not in $PATH.")
HOME_DIR = os.environ["HOME"]
PASS_DIR = os.environ.get("PASSWORD_STORE_DIR", os.path.join(HOME_DIR, ".password-store/"))
ICON_PATH = iconLookup("dialog-password")
def handleQuery(query):
if query.isTriggered:
query.disableSort()
if query.string.strip().startswith("generate"):
return generatePassword(query)
return showPasswords(query)
def generatePassword(query):
location = query.string.strip()[9:]
return [Item(
id=__prettyname__,
icon=ICON_PATH,
text="Generate a new password",
subtext="The new password will be located at %s" % location,
completion="pass %s" % query.string,
actions=[
ProcAction("Generate", ["pass", "generate", "--clip", location, "20"])
]
)]
def showPasswords(query):
passwords = []
if query.string.strip():
passwords = getPasswordsFromSearch(query)
else:
passwords = getPasswords()
results = []
for password in passwords:
name = password.split("/")[-1]
results.append(
Item(
id=password,
icon=ICON_PATH,
text=name,
subtext=password,
completion="pass %s" % password,
actions=[
ProcAction("Copy", ["pass", "--clip", password]),
ProcAction("Edit", ["pass", "edit", password]),
ProcAction("Remove", ["pass", "rm", "--force", password]),
]
),
)
return results
def getPasswords():
passwords = []
for root, dirnames, filenames in os.walk(PASS_DIR):
for filename in fnmatch.filter(filenames, "*.gpg"):
passwords.append(
os.path.join(root, filename.replace(".gpg", "")).replace(PASS_DIR, "")
)
return sorted(passwords, key=lambda s: s.lower())
def getPasswordsFromSearch(query):
passwords = []
for password in getPasswords():
if query.string.strip().lower() not in password.lower():
continue
passwords.append(password)
return passwords