dmenu_multicmd: some cleanup and config example

This commit is contained in:
Von Random 2019-03-13 15:28:28 +03:00
parent 6c778f9d1a
commit 6c3e745835

View file

@ -1,15 +1,26 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
#pylint: disable=C0111 #pylint: disable=C0111
# config example (cmd_pre and cmd_post are optional):
import os # ---
# name: test
# cmd_pre:
# - [/usr/bin/notify_send, 'I am precmd1!']
# - [/usr/bin/notify_send, 'I am precmd2!']
# multicmd:
# selection1:
# - [/usr/bin/notify-send, 'I am command1 for selection1!']
# - [/usr/bin/notify-send, 'I am command2 for selection1!']
# selection2:
# - [/usr/bin/notify-send, 'I am command1 for selection2!']
# cmd_post:
# - [/usr/bin/notify_send, 'I am postcmd1!']
# - [/usr/bin/notify_send, 'I am postcmd2!']
# ---
import subprocess import subprocess
import sys import sys
import yaml import yaml
CONFIG_PATH = os.path.join(os.environ['HOME'], '.displayrc.yaml')
def run_cmd(cmd, stdin=subprocess.PIPE, data=None): def run_cmd(cmd, stdin=subprocess.PIPE, data=None):
proc = subprocess.Popen( proc = subprocess.Popen(
cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=stdin cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=stdin
@ -27,7 +38,7 @@ def run_cmd(cmd, stdin=subprocess.PIPE, data=None):
def run_multicmd(cmds): def run_multicmd(cmds):
for cmd in cmds: for cmd in cmds:
run_cmd(cmds[cmd]) run_cmd(cmd)
def main(): def main():
@ -39,14 +50,9 @@ def main():
selection = run_cmd(dmenu_cmd, data=dmenu_opt).decode('UTF-8') selection = run_cmd(dmenu_cmd, data=dmenu_opt).decode('UTF-8')
if not selection in conf['multicmd']: if not selection in conf['multicmd']:
sys.exit(1) sys.exit(1)
run_multicmd(conf.get('cmd_pre', tuple()))
if 'cmd_pre' in conf:
run_multicmd(conf['cmd_pre'])
run_multicmd(conf['multicmd'][selection]) run_multicmd(conf['multicmd'][selection])
run_multicmd(conf.get('cmd_post', tuple()))
if 'cmd_post' in conf:
run_multicmd(conf['cmd_post'])
if __name__ == '__main__': if __name__ == '__main__':
main() main()