platform.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. import os
  11. import sys
  12. import time
  13. import codecs
  14. import urllib
  15. if os.name == 'nt':
  16. #import BTL.likewin32api as win32api
  17. import win32api
  18. from win32com.shell import shellcon, shell
  19. is_frozen_exe = getattr(sys, 'frozen', '') == 'windows_exe'
  20. def get_module_filename():
  21. if is_frozen_exe:
  22. return os.path.abspath(win32api.GetModuleFileName(0))
  23. else:
  24. return os.path.abspath(sys.argv[0])
  25. try:
  26. from __main__ import app_name
  27. except:
  28. # ok, I'm sick of this. Everyone gets BTL if they don't
  29. # specify otherwise.
  30. app_name = "BTL"
  31. if sys.platform.startswith('win'):
  32. bttime = time.clock
  33. else:
  34. bttime = time.time
  35. def urlquote_error(error):
  36. s = error.object[error.start:error.end]
  37. s = s.encode('utf8')
  38. s = urllib.quote(s)
  39. s = s.decode('ascii')
  40. return (s, error.end)
  41. codecs.register_error('urlquote', urlquote_error)
  42. def get_filesystem_encoding(errorfunc=None):
  43. def dummy_log(e):
  44. print e
  45. pass
  46. if not errorfunc:
  47. errorfunc = dummy_log
  48. default_encoding = 'utf8'
  49. if os.path.supports_unicode_filenames:
  50. encoding = None
  51. else:
  52. try:
  53. encoding = sys.getfilesystemencoding()
  54. except AttributeError:
  55. errorfunc("This version of Python cannot detect filesystem encoding.")
  56. if encoding is None:
  57. encoding = default_encoding
  58. errorfunc("Python failed to detect filesystem encoding. "
  59. "Assuming '%s' instead." % default_encoding)
  60. else:
  61. try:
  62. 'a1'.decode(encoding)
  63. except:
  64. errorfunc("Filesystem encoding '%s' is not supported. Using '%s' instead." %
  65. (encoding, default_encoding))
  66. encoding = default_encoding
  67. return encoding
  68. def encode_for_filesystem(path):
  69. assert isinstance(path, unicode), "Path should be unicode not %s" % type(path)
  70. bad = False
  71. encoding = get_filesystem_encoding()
  72. if encoding == None:
  73. encoded_path = path
  74. else:
  75. try:
  76. encoded_path = path.encode(encoding)
  77. except:
  78. bad = True
  79. path.replace(u"%", urllib.quote(u"%"))
  80. encoded_path = path.encode(encoding, 'urlquote')
  81. return (encoded_path, bad)
  82. def decode_from_filesystem(path):
  83. encoding = get_filesystem_encoding()
  84. if encoding == None:
  85. assert isinstance(path, unicode), "Path should be unicode not %s" % type(path)
  86. decoded_path = path
  87. else:
  88. assert isinstance(path, str), "Path should be str not %s" % type(path)
  89. decoded_path = path.decode(encoding)
  90. return decoded_path
  91. efs = encode_for_filesystem
  92. def efs2(path):
  93. # same as encode_for_filesystem, but doesn't bother returning "bad"
  94. return encode_for_filesystem(path)[0]
  95. # this function is the preferred way to get windows' paths
  96. def get_shell_dir(value):
  97. dir = None
  98. if os.name == 'nt':
  99. try:
  100. dir = shell.SHGetFolderPath(0, value, 0, 0)
  101. except:
  102. pass
  103. return dir
  104. def get_cache_dir():
  105. dir = None
  106. if os.name == 'nt':
  107. dir = get_shell_dir(shellcon.CSIDL_INTERNET_CACHE)
  108. return dir