random script dump
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

dmenu_multicmd 1.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #!/usr/bin/env python3
  2. #pylint: disable=C0111
  3. # config example (cmd_pre and cmd_post are optional):
  4. # ---
  5. # name: test
  6. # cmd_pre:
  7. # - [/usr/bin/notify_send, 'I am precmd1!']
  8. # - [/usr/bin/notify_send, 'I am precmd2!']
  9. # multicmd:
  10. # selection1:
  11. # - [/usr/bin/notify-send, 'I am command1 for selection1!']
  12. # - [/usr/bin/notify-send, 'I am command2 for selection1!']
  13. # selection2:
  14. # - [/usr/bin/notify-send, 'I am command1 for selection2!']
  15. # cmd_post:
  16. # - [/usr/bin/notify_send, 'I am postcmd1!']
  17. # - [/usr/bin/notify_send, 'I am postcmd2!']
  18. # ---
  19. import subprocess
  20. import sys
  21. import yaml
  22. def run_cmd(cmd, stdin=subprocess.PIPE, data=None):
  23. proc = subprocess.Popen(
  24. cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=stdin
  25. )
  26. stdout, stderr = (i.strip() for i in proc.communicate(data))
  27. if proc.returncode:
  28. err_msg = ['ERROR (' + str(proc.returncode) + '): ' + ' '.join(cmd)]
  29. if stdout:
  30. err_msg.append('out:\n' + stdout.decode('UTF-8'))
  31. if stderr:
  32. err_msg.append('err:\n' + stderr.decode('UTF-8'))
  33. sys.stderr.write('\n'.join(err_msg) + '\n')
  34. return stdout
  35. def run_multicmd(cmds):
  36. for cmd in cmds:
  37. run_cmd(cmd)
  38. def main():
  39. config_path = sys.argv[1]
  40. with open(config_path, mode='r') as config:
  41. conf = yaml.load(config.read())
  42. dmenu_cmd = ['/usr/bin/dmenu', '-p', conf.get('name', 'N/A')] + sys.argv[2:]
  43. dmenu_opt = '\n'.join(conf['multicmd']).encode()
  44. selection = run_cmd(dmenu_cmd, data=dmenu_opt).decode('UTF-8')
  45. if not selection in conf['multicmd']:
  46. sys.exit(1)
  47. run_multicmd(conf.get('cmd_pre', tuple()))
  48. run_multicmd(conf['multicmd'][selection])
  49. run_multicmd(conf.get('cmd_post', tuple()))
  50. if __name__ == '__main__':
  51. main()