xdg.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. #!/usr/bin/python
  2. from gi.repository import Gio # pylint: disable=import-error
  3. from gi.repository import GLib # pylint: disable=import-error
  4. import sys
  5. import os
  6. def pid_callback(launcher, pid, pid_list):
  7. pid_list.append(pid)
  8. def launch(desktop, *files, **kwargs):
  9. wait = kwargs.pop('wait', True)
  10. launcher = Gio.DesktopAppInfo.new_from_filename(desktop)
  11. try:
  12. import dbus
  13. if hasattr(launcher, 'get_boolean'):
  14. activatable = launcher.get_boolean('DBusActivatable')
  15. if activatable:
  16. bus = dbus.SessionBus()
  17. service_id = launcher.get_id()
  18. # cut the .desktop suffix
  19. service_id = service_id[:-8]
  20. try:
  21. bus.start_service_by_name(service_id)
  22. except dbus.DBusException:
  23. pass
  24. except ImportError:
  25. pass
  26. if wait:
  27. pid_list = []
  28. flags = GLib.SpawnFlags.SEARCH_PATH | GLib.SpawnFlags.DO_NOT_REAP_CHILD
  29. launcher.launch_uris_as_manager(files, None, flags, None, None,
  30. pid_callback, pid_list)
  31. for pid in pid_list:
  32. os.waitpid(pid, 0)
  33. else:
  34. launcher.launch(files, None)
  35. if __name__ == "__main__":
  36. launch(*sys.argv[1:])