Repository navigation
Expand file tree
/
Copy path__init__.py
More file actions
182 lines (156 loc) · 5.9 KB
/
Copy path__init__.py
File metadata and controls
182 lines (156 loc) · 5.9 KB
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# -*- coding: utf-8 -*-
# Copyright (c) 2017 Benedict Dudel
# Copyright (c) 2023 Max
# Copyright (c) 2023 Pete-Hamlin
import fnmatch
import os
from albert import *
md_iid = "5.0"
md_version = "2.0.4"
md_name = "Pass"
md_description = "Manage passwords in pass"
md_license = "MIT"
md_url = "https://github.com/albertlauncher/albert-plugin-python-pass"
md_authors = ["@benedictdudel", "@maxmil", "@Pete-Hamlin", "@okaestne"]
md_maintainers = ["@maxmil", "@okaestne", "@Pete-Hamlin"]
md_bin_dependencies = ["pass"]
HOME_DIR = os.environ["HOME"]
PASS_DIR = os.environ.get("PASSWORD_STORE_DIR", os.path.join(HOME_DIR, ".password-store/"))
class Plugin(PluginInstance, GeneratorQueryHandler):
def __init__(self):
PluginInstance.__init__(self)
GeneratorQueryHandler.__init__(self)
self._use_otp = self.readConfig("use_otp", bool) or False
self._otp_glob = self.readConfig("otp_glob", str) or "*-otp.gpg"
@staticmethod
def makeIcon():
return Icon.theme("dialog-password")
@property
def use_otp(self):
return self._use_otp
@use_otp.setter
def use_otp(self, value):
print(f"Setting _use_otp to {value}")
self._use_otp = value
self.writeConfig("use_otp", value)
@property
def otp_glob(self):
return self._otp_glob
@otp_glob.setter
def otp_glob(self, value):
print(f"Setting _otp_glob to {value}")
self._otp_glob = value
self.writeConfig("otp_glob", value)
def defaultTrigger(self):
return "pass "
def synopsis(self, query):
return "<pass-name>"
def configWidget(self):
return [
{"type": "checkbox", "property": "use_otp", "label": "Enable pass OTP extension"},
{
"type": "lineedit",
"property": "otp_glob",
"label": "Glob pattern for OTP passwords",
"widget_properties": {"placeholderText": "*-otp.gpg"},
},
]
def items(self, context: QueryContext):
q = context.query.strip()
if q.startswith("generate"):
yield [self.generatePassword(q)]
elif q.startswith("otp") and self._use_otp:
yield self.showOtp(q)
else:
yield self.showPasswords(q)
def generatePassword(self, query) -> Item:
location = query.strip()[9:].strip()
if not location:
return StandardItem(
id="generate_password",
icon_factory=Plugin.makeIcon,
text="Can't generate a new password",
subtext="The location for the new password is invalid",
)
return StandardItem(
id="generate_password",
icon_factory=Plugin.makeIcon,
text="Generate a new password",
subtext="The new password will be located at %s" % location,
input_action_text="pass %s" % query,
actions=[
Action(
"generate",
"Generate",
lambda: runDetachedProcess(["pass", "generate", "--clip", location, "20"]),
)
],
)
def showOtp(self, query) -> list[Item]:
otp_query = query.strip()[4:]
if otp_query:
passwords = self.getPasswordsFromSearch(otp_query, otp=True)
else:
passwords = self.getPasswords(otp=True)
results = []
for password in passwords:
results.append(
StandardItem(
id=password,
icon_factory=Plugin.makeIcon,
text=password.split("/")[-1],
subtext=password,
actions=[
Action(
"copy",
"Copy",
lambda pwd=password: runDetachedProcess(["pass", "otp", "--clip", pwd]),
),
],
),
)
return results
def showPasswords(self, query) -> list[Item]:
if query.strip():
passwords = self.getPasswordsFromSearch(query)
else:
passwords = self.getPasswords()
results = []
for password in passwords:
name = password.split("/")[-1]
results.append(
StandardItem(
id=password,
text=name,
subtext=password,
icon_factory=Plugin.makeIcon,
input_action_text="pass %s" % password,
actions=[
Action(
"copy",
"Copy",
lambda pwd=password: runDetachedProcess(["pass", "--clip", pwd]),
),
Action(
"edit",
"Edit",
lambda pwd=password: runDetachedProcess(["pass", "edit", pwd]),
),
Action(
"remove",
"Remove",
lambda pwd=password: runDetachedProcess(["pass", "rm", "--force", pwd]),
),
],
),
)
return results
def getPasswords(self, otp=False):
passwords = []
for root, dirnames, filenames in os.walk(PASS_DIR, followlinks=True):
for filename in fnmatch.filter(filenames, self._otp_glob if otp else "*.gpg"):
passwords.append(os.path.join(root, filename.replace(".gpg", "")).replace(PASS_DIR, ""))
return sorted(passwords, key=lambda s: s.lower())
def getPasswordsFromSearch(self, otp_query, otp=False):
passwords = [password for password in self.getPasswords(otp) if otp_query.strip().lower() in password.lower()]
return passwords