You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

189 lines
4.3 KiB

11 years ago
11 years ago
11 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  1. #! /usr/bin/env python
  2. import os
  3. import sys
  4. import locale
  5. import subprocess
  6. from os import (
  7. remove as remove_file,
  8. makedirs as makedir,
  9. name as os_type,
  10. )
  11. from os.path import (
  12. getsize as get_filesize,
  13. exists as file_exist
  14. )
  15. def get_encoding():
  16. if sys.version_info >= (3, 0):
  17. return None
  18. if sys.platform == 'win32':
  19. try:
  20. enc = locale.getpreferredencoding()
  21. u'TEST'.encode(enc)
  22. except:
  23. enc = 'UTF-8'
  24. return enc
  25. return None
  26. def encode_list(lst, encoding):
  27. return [item.encode(encoding, 'ignore') for item in lst]
  28. def video_is_dash(video):
  29. return "DASH" in video
  30. def audio_is_dash(audio):
  31. return audio != "none"
  32. def path_seperator():
  33. ''' Return path seperator for current OS '''
  34. return '\\' if os_type == 'nt' else '/'
  35. def fix_path(path):
  36. ''' Add path seperator at the end of the path
  37. if not exist and replace ~ with user $HOME '''
  38. if path == '':
  39. return path
  40. if path[-1:] != path_seperator():
  41. path += path_seperator()
  42. path_list = path.split(path_seperator())
  43. for index, item in enumerate(path_list):
  44. if item == '~':
  45. path_list[index] = get_home()
  46. path = path_seperator().join(path_list)
  47. return path
  48. def get_home():
  49. return os.path.expanduser("~")
  50. def abs_path(filename):
  51. path = os.path.realpath(os.path.abspath(filename))
  52. path = path.split(path_seperator())
  53. path.pop()
  54. return path_seperator().join(path)
  55. def get_filename(path):
  56. return path.split(path_seperator())[-1]
  57. def open_dir(path):
  58. if os_type == 'nt':
  59. os.startfile(path)
  60. else:
  61. subprocess.call(('xdg-open', path))
  62. def check_path(path):
  63. if not file_exist(path):
  64. makedir(path)
  65. def get_youtubedl_filename():
  66. youtubedl_fl = 'youtube-dl'
  67. if os_type == 'nt':
  68. youtubedl_fl += '.exe'
  69. return youtubedl_fl
  70. def get_user_config_path():
  71. if os_type == 'nt':
  72. path = os.getenv('APPDATA')
  73. else:
  74. path = fix_path(get_home()) + '.config'
  75. return path
  76. def shutdown_sys(password=''):
  77. if os_type == 'nt':
  78. subprocess.call(['shutdown', '/s', '/t', '1'])
  79. else:
  80. if password == '':
  81. subprocess.call(['/sbin/shutdown', '-h', 'now'])
  82. else:
  83. p = subprocess.Popen(
  84. ['sudo', '-S', '/sbin/shutdown', '-h', 'now'],
  85. stdin=subprocess.PIPE
  86. )
  87. p.communicate(password + '\n')
  88. def get_time(seconds):
  89. ''' Return day, hours, minutes, seconds from given seconds'''
  90. dtime = {'seconds': 0, 'minutes': 0, 'hours': 0, 'days': 0}
  91. if seconds < 60:
  92. dtime['seconds'] = seconds
  93. elif seconds < 3600:
  94. dtime['minutes'] = seconds / 60
  95. dtime['seconds'] = seconds % 60
  96. elif seconds < 86400:
  97. dtime['hours'] = seconds / 3600
  98. dtime['minutes'] = seconds % 3600 / 60
  99. dtime['seconds'] = seconds % 3600 % 60
  100. else:
  101. dtime['days'] = seconds / 86400
  102. dtime['hours'] = seconds % 86400 / 3600
  103. dtime['minutes'] = seconds % 86400 % 3600 / 60
  104. dtime['seconds'] = seconds % 86400 % 3600 % 60
  105. return dtime
  106. def get_icon_path():
  107. ''' Return path to the icon file if exist else return None.
  108. Search __main__ dir, $XDG_DATA_DIRS, /usr/share/pixmaps in that order '''
  109. SIZES = ('256x256', '128x128', '64x64', '32x32', '16x16')
  110. ICON_NAME = 'youtube-dl-gui_%s.png'
  111. ICONS_LIST = [ICON_NAME % size for size in SIZES]
  112. # __main__ dir
  113. path = abs_path(sys.argv[0])
  114. path = fix_path(path) + 'icons'
  115. for icon in ICONS_LIST:
  116. temp_path = fix_path(path) + icon
  117. if file_exist(temp_path):
  118. return temp_path
  119. # $XDG_DATA_DIRS/icons
  120. path = os.getenv('XDG_DATA_DIRS')
  121. if path is not None:
  122. for temp_path in path.split(':'):
  123. temp_path = fix_path(temp_path) + 'icons'
  124. temp_path = fix_path(temp_path) + 'hicolor'
  125. for size in SIZES:
  126. p = fix_path(temp_path) + size
  127. p = fix_path(p) + 'apps'
  128. p = fix_path(p) + ICON_NAME % size
  129. if file_exist(p):
  130. return p
  131. # /usr/share/pixmaps
  132. path = '/usr/share/pixmaps/'
  133. for icon in ICONS_LIST:
  134. temp_path = path + icon
  135. if file_exist(temp_path):
  136. return temp_path
  137. return None