openfiles.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. """openfiles.py -- open files/folders."""
  2. import logging
  3. import os
  4. import subprocess
  5. import sys
  6. # To open paths we use an OS-specific command. The approach is from:
  7. # http://stackoverflow.com/questions/6631299/python-opening-a-folder-in-explorer-nautilus-mac-thingie
  8. def check_kde():
  9. return os.environ.get("KDE_FULL_SESSION", None) is not None
  10. def check_xorg():
  11. return os.environ.get("XDG_SESSION_ID", None) is not None
  12. def _open_path_osx(path):
  13. subprocess.call(['open', '--', path])
  14. def _open_path_kde(path):
  15. # kfmclient is part of konqueror
  16. subprocess.call(["kfmclient", "exec", "file://" + path])
  17. def _open_path_xorg(path):
  18. subprocess.call(['xdg-open', path])
  19. def _open_path_gnome(path):
  20. subprocess.call(['gnome-open', '--', path])
  21. def _open_path_windows(path):
  22. subprocess.call(['explorer', path])
  23. def _open_path(path):
  24. if sys.platform == 'darwin':
  25. _open_path_osx(path)
  26. elif sys.platform == 'linux2':
  27. if check_kde():
  28. _open_path_kde(path)
  29. elif check_xorg():
  30. _open_path_xorg(path)
  31. else:
  32. _open_path_gnome(path)
  33. elif sys.platform == 'win32':
  34. _open_path_windows(path)
  35. else:
  36. logging.warn("unknown platform: %s", sys.platform)
  37. def reveal_folder(path):
  38. """Show a folder in the desktop shell (finder/explorer/nautilous, etc)."""
  39. logging.info("reveal_folder: %s", path)
  40. if os.path.isdir(path):
  41. _open_path(path)
  42. else:
  43. _open_path(os.path.dirname(path))