-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdmenu_launch.py
executable file
·214 lines (178 loc) · 7.72 KB
/
dmenu_launch.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
209
210
211
212
213
214
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""\
Simple dmenu launcher for passwords, docs, notes and application shortcuts.
Requirements
---------------
- dmenu, gpg, pass, xclip, exo-open, pkill
Usage
---------------
$ dmenu_launch.py [-h] [--pass | --apps | --notes]
Arguments
---------------
-h, --help show this help message and exit
--pass Chooses a password from password store.
--apps Quick launches a desktop application with exo-open.
--notes Opens a text/markdown note from a given directory with exo-open.
--search Quick search and launch from a given directory with exo-open.
Contact / Links
---------------
author : Felipe Silveira <[email protected]>
link : https://github.com/fsilveir/dmenu-launch
"""
import os
import sys
import argparse
import subprocess
from collections import namedtuple
from distutils.spawn import find_executable
def main():
check_req_utils()
args = get_args()
scheme = dmenu_setup(args)
choice = dmenu_input(scheme)
take_action(scheme, choice)
def check_req_utils():
"""Checks if dmenu and other mandatory utilities can be found on target machine."""
utils = (['dmenu', 'gpg', 'pass', 'xclip', 'exo-open', 'pkill'])
for util in utils:
if find_executable(util) is None:
print("ERROR: Util '{}' is missing, install it before proceeding! Exiting!".format(util))
sys.exit(1)
def check_dir_exist(scheme):
"""Checks required directories are present."""
if os.path.exists(scheme.prefix) is False:
print("ERROR: Required directory '{}' is missing! Exiting!").format(scheme.prefix)
sys.exit(1)
def get_args():
"""Return arguments from stdin or print usage instructions."""
parser = argparse.ArgumentParser(description='Simple dmenu launcher for passwords, notes and application shortcuts.')
group = parser.add_mutually_exclusive_group()
group.add_argument('--pass', dest='passw', action='store_true',
help='Copy password from password store.')
group.add_argument('--apps', action='store_true',
help='Quick launches a desktop application with exo-open.')
group.add_argument('--notes', action='store_true',
help='Opens a text/markdown note from a given directory with exo-open.')
group.add_argument('--search', action='store_true',
help='Quick search and launch from a given directory with exo-open.')
if not len(sys.argv) > 1:
parser.print_help()
sys.exit(1)
return parser.parse_args()
def dmenu_setup(args):
"""Setup dmenu font, color and size based on user's input."""
scheme = namedtuple(
'dmenu',
[
'target', # pass / apps/ notes / search
'prefix', # location prefix (base dir)
'suffix', # file extension to look for
'font', # dmenu font name and size
'nb','nf','sb','sf', # dmenu color:
# n=normal / s=selected,
# b=background, f=foreground
])
dmenu = ""
if args.passw:
dmenu = scheme(
target='pass',
prefix = os.getenv\
('PASSWORD_STORE_DIR',os.path.normpath
(os.path.expanduser\
('~/.password-store')\
)\
),
suffix=".gpg",
font='Dejavu Sans Mono:medium:size=18',
nb='#191919', nf='#ff0000', sb='#ff9318', sf='#191919',
)
if args.apps:
dmenu = scheme(
target='apps',
prefix="/usr/share/applications",
suffix=".desktop",
font='Dejavu Sans Mono:medium:size=18',
nb='#191919', nf='#2e9ef4', sb='#2e9ef4', sf='#191919',
)
if args.notes:
dmenu = scheme(
target='notes',
prefix=os.path.expanduser('~/git/notes'),
suffix=".md",
font='Dejavu Sans Mono:medium:size=18',
nb='#191919', nf='#2aa198', sb='#2aa198', sf='#191919',
)
if args.search:
dmenu = scheme(
target='search',
prefix=os.path.expanduser('~/work'),
suffix="",
font='Dejavu Sans Mono:medium:size=18',
nb='#191919', nf='#2aa198', sb='#11D91E', sf='#191919',
)
check_dir_exist(dmenu)
return dmenu
def dmenu_input(scheme):
"""Builds dmenu list of options and returns the value selected by user."""
choices = []
for basedir, dirs , files in os.walk(scheme.prefix, followlinks=True):
dirs.sort()
files.sort()
dirsubpath = basedir[len(scheme.prefix):].lstrip('/')
for f in files:
if f.endswith(scheme.suffix):
full_path = os.path.join(dirsubpath, f.replace(scheme.suffix, '', -1))
choices += [full_path]
args = ["-fn", scheme.font, \
"-nb", scheme.nb, \
"-nf", scheme.nf, \
"-sb", scheme.sb, \
"-sf", scheme.sf, \
"-i" ]
dmenu = subprocess.Popen(['dmenu'] + args,
stdin=subprocess.PIPE,
stderr=subprocess.PIPE,
stdout=subprocess.PIPE)
choice_lines = '\n'.join(map(str, choices))
choice, errors = dmenu.communicate(choice_lines.encode('utf-8'))
if dmenu.returncode not in [0, 1] \
or (dmenu.returncode == 1 and len(errors) != 0):
print("'{} {}' returned {} and error:\n{}"
.format(['dmenu'], ' '.join(args), dmenu.returncode,
errors.decode('utf-8')))
sys.exit(1)
choice = choice.decode('utf-8').rstrip()
return (scheme.prefix + "/" + choice + scheme.suffix) if choice in choices else sys.exit(1)
def take_action(scheme, choice):
"""Copies password to clipboard, launch app / notes depending on input."""
xclip_cleanup()
if (scheme.target == "pass"):
_target = (str(choice).replace(scheme.prefix,'',-1))
target = (_target[1:].replace(scheme.suffix,'',-1))
# Copies the username to middle button clipboard
username = target.rsplit('/',1)[1].strip('\n')
run_subprocess('echo "{}" | xclip'.format(username))
# Copy password to clipboard for 45 seconds
run_subprocess('pass show -c "{}"'.format(target))
if (scheme.target == "notes"):
run_subprocess('exo-open "{}"'.format(choice))
if (scheme.target == "apps") or (scheme.target == "search"):
run_subprocess('exo-open "{}"'.format(choice))
def run_subprocess(cmd):
"""Handler for shortening subprocess execution."""
subprocess.Popen(cmd, stdin =subprocess.PIPE,
stderr=subprocess.PIPE,
stdout=subprocess.PIPE,
shell=True,)
def xclip_cleanup():
"""Clean-up xclip processes that might be hung."""
run_subprocess('pkill xclip')
# ------------------------------------------------------------------------------
# Main
# ------------------------------------------------------------------------------
if __name__ == "__main__":
main()
# ------------------------------------------------------------------------------
# EOF
# ------------------------------------------------------------------------------