-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.py
208 lines (153 loc) · 7.77 KB
/
main.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
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
""" Yannis Huber 2019 """
from subprocess import check_output
from os import path as os_path
import re
from ulauncher.api.client.Extension import Extension
from ulauncher.api.client.EventListener import EventListener
from ulauncher.api.shared.event import KeywordQueryEvent
from ulauncher.api.shared.item.ExtensionResultItem import ExtensionResultItem
from ulauncher.api.shared.action.RenderResultListAction import RenderResultListAction
from ulauncher.api.shared.action.RunScriptAction import RunScriptAction
from ulauncher.api.shared.action.SetUserQueryAction import SetUserQueryAction
from ulauncher.api.shared.action.DoNothingAction import DoNothingAction
PASSWORD_ICON = 'images/icon.png'
FOLDER_ICON = 'images/folder.png'
MORE_ICON = 'images/more.png'
WARNING_ICON = 'images/warning.png'
PASSWORD_DESCRIPTION = 'Enter to copy to the clipboard'
FOLDER_DESCRIPTION = 'Enter to navigate to'
WRONG_PATH_ITEM = ExtensionResultItem(icon=WARNING_ICON,
name='Invalid path',
description='Please check your arguments.',
on_enter=DoNothingAction())
MORE_ELEMENTS_ITEM = ExtensionResultItem(icon=MORE_ICON,
name='Keep typing...',
description='More items are available.'
+ ' Narrow your search by entering a pattern.',
on_enter=DoNothingAction())
NO_FILENAME_ITEM = ExtensionResultItem(icon=WARNING_ICON,
name='Please enter a filename',
description='Please check your arguments.',
on_enter=DoNothingAction())
class PassExtension(Extension):
""" Extension class, does the searching """
def __init__(self):
super(PassExtension, self).__init__()
self.subscribe(KeywordQueryEvent, KeywordQueryEventListener())
def search(self, path=None, pattern=None, depth=None):
""" Launches a find command with the specified pattern """
store_location = os_path.expanduser(self.preferences['store-location'])
if not path:
path = ''
if not pattern:
pattern = ''
if depth:
max_depth = '-maxdepth {} '.format(str(depth))
else:
max_depth = ''
searching_path = os_path.join(store_location, path)
cmd_files = ("find {} {}-type f -not " +
"-path *.git* -not -name .* -iname *{}*").format(searching_path,
max_depth,
pattern)
cmd_dirs = ("find {} {}-type d -not " +
"-path *.git* -not -name .* -iname *{}*").format(searching_path,
max_depth,
pattern)
files = re.findall("{}/*(.+).gpg".format(searching_path),
check_output(cmd_files.split(" ")).decode("utf-8"))
dirs = re.findall("{}/*(.+)".format(searching_path),
check_output(cmd_dirs.split(" ")).decode("utf-8"))
files.sort()
dirs.sort()
return files, dirs
class KeywordQueryEventListener(EventListener):
""" KeywordQueryEventListener class used to manage user input"""
def __init__(self):
self.extension = None
def render_results(self, path, files, dirs, keyword):
""" Prepares the results for the UI """
items = []
limit = int(self.extension.preferences['max-results'])
if limit < len(dirs) + len(files):
items.append(MORE_ELEMENTS_ITEM)
for _dir in dirs:
limit -= 1
if limit < 0:
break
action = SetUserQueryAction("{0} {1}/".format(keyword,
os_path.join(path, _dir)))
items.append(ExtensionResultItem(icon=FOLDER_ICON,
name="{0}".format(_dir),
description=FOLDER_DESCRIPTION,
on_enter=action))
for _file in files:
limit -= 1
if limit < 0:
break
action = RunScriptAction("pass -c {0}/{1}".format(path, _file), None)
items.append(ExtensionResultItem(icon=PASSWORD_ICON,
name="{0}".format(_file),
description=PASSWORD_DESCRIPTION,
on_enter=action))
return items
def on_event(self, event, extension):
""" On user input """
self.extension = extension
# Get keyword and arguments from event
query_keyword = event.get_keyword()
query_args = event.get_argument()
# Initialize variables
path = ''
files = []
dirs = []
misc = []
no_filename = False
path_not_exists = False
if not query_args:
# No arguments specified, list folders and passwords in the password-store root
files, dirs = self.extension.search(depth=1)
else:
# Splitting arguments into path and pattern
path = os_path.split(query_args)[0]
pattern = os_path.split(query_args)[1]
# If the path begins with a slash we remove it
if path.startswith('/'):
path = path[1:]
store_location = os_path.expanduser(self.extension.preferences['store-location'])
depth = None
if not os_path.exists(os_path.join(store_location, path)):
path_not_exists = True
if query_keyword == extension.preferences['pass-search']:
# If specified folder does not exist and we are in searching mode,
# show the user an error
misc.append(WRONG_PATH_ITEM)
if not pattern:
# If the path exists and there is no pattern, only show files
# and dirs in the current location
depth = 1
if query_keyword == extension.preferences['pass-generate']:
# If we are in generation mode and no pattern is given,
# show the user an error
no_filename = True
misc.append(NO_FILENAME_ITEM)
if not no_filename and query_keyword == extension.preferences['pass-generate']:
# If the user specified a pattern and we are in generation mode
# give him the possibility to generate the password
action = RunScriptAction(
"pass generate -c {}".format(os_path.join(path, pattern)),
None)
misc.append(ExtensionResultItem(
icon=PASSWORD_ICON,
name='Generate /{}'.format(os_path.join(path, pattern)),
description='Enter to generate and save password',
on_enter=action))
# If the specified path doesn't exist it makes no sense to search for a password
if not path_not_exists:
files, dirs = self.extension.search(path=path, pattern=pattern, depth=depth)
if query_keyword == extension.preferences['pass-generate']:
# If we are in generation mode we can remove the files
files = []
return RenderResultListAction(misc + self.render_results(path, files, dirs, query_keyword))
if __name__ == '__main__':
PassExtension().run()