1
0

LaunchPath.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. # The contents of this file are subject to the BitTorrent Open Source License
  2. # Version 1.1 (the License). You may not copy or use this file, in either
  3. # source code or executable form, except in compliance with the License. You
  4. # may obtain a copy of the License at http://www.bittorrent.com/license/.
  5. #
  6. # Software distributed under the License is distributed on an AS IS basis,
  7. # WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  8. # for the specific language governing rights and limitations under the
  9. # License.
  10. # LaunchPath -- a cross platform way to "open," "launch," or "start"
  11. # files and directories
  12. # written by Matt Chisholm
  13. import os
  14. import sys
  15. can_launch_dirs = False
  16. can_launch_files = False
  17. posix_browsers = ('gnome-open','konqueror',)
  18. posix_dir_browsers = ('gmc', 'gentoo',) # these only work on dirs
  19. default_posix_browser = ''
  20. def launchpath_nt(path):
  21. os.startfile(path)
  22. def launchfile_nt(path):
  23. do_launchdir = True
  24. if can_launch_files and not os.path.isdir(path):
  25. f, ext = os.path.splitext(path)
  26. ext = ext.upper()
  27. path_ext = os.environ.get('PATH_EXT')
  28. blacklist = []
  29. if path_ext:
  30. blacklist = path_ext.split(';')
  31. if ext not in blacklist:
  32. try:
  33. launchpath_nt(path)
  34. except: # WindowsError
  35. pass
  36. else:
  37. do_launchdir = False
  38. if do_launchdir:
  39. p, f = os.path.split(path)
  40. launchdir(p)
  41. def launchpath_mac(path):
  42. os.spawnlp(os.P_NOWAIT, 'open', 'open', path)
  43. def launchpath_posix(path):
  44. if default_posix_browser:
  45. os.spawnlp(os.P_NOWAIT, default_posix_browser,
  46. default_posix_browser, path)
  47. def launchpath(path):
  48. pass
  49. def launchdir(path):
  50. if can_launch_dirs and os.path.isdir(path):
  51. launchpath(path)
  52. def launchfile(path):
  53. if can_launch_files and not os.path.isdir(path):
  54. launchpath(path)
  55. else:
  56. p, f = os.path.split(path)
  57. launchdir(p)
  58. if os.name == 'nt':
  59. can_launch_dirs = True
  60. can_launch_files = True
  61. launchpath = launchpath_nt
  62. launchfile = launchfile_nt
  63. elif sys.platform == "darwin":
  64. can_launch_dirs = True
  65. can_launch_files = True
  66. launchpath = launchpath_mac
  67. elif os.name == 'posix':
  68. for b in posix_browsers:
  69. if os.system("which '%s' >/dev/null 2>&1" % b.replace("'","\\'")) == 0:
  70. can_launch_dirs = True
  71. can_launch_files = True
  72. default_posix_browser = b
  73. launchpath = launchpath_posix
  74. break
  75. else:
  76. for b in posix_dir_browsers:
  77. can_launch_dirs = True
  78. default_posix_browser = b
  79. launchpath = launchpath_posix
  80. break