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.

205 lines
4.8 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 icon path else return None. Search package_path/icons,
  108. $HOME/.icons, $XDG_DATA_DIRS/icons, /usr/share/pixmaps in that order'''
  109. SIZES = ('256x256', '128x128', '64x64', '32x32', '16x16')
  110. ICON_NAME = 'youtube-dl-gui_'
  111. ICON_EXTENSION = '.png'
  112. ICONS_LIST = [ICON_NAME + s + ICON_EXTENSION for s in SIZES]
  113. # Package path backwards 2 times
  114. # e.g. /home/user/test/t1/t2
  115. # /home/user/test/t1/t2/icons
  116. # /home/user/test/t1/icons
  117. path = abs_path(__file__)
  118. for i in range(2):
  119. temp_path = fix_path(path) + 'icons'
  120. for icon in ICONS_LIST:
  121. p = fix_path(temp_path) + icon
  122. if file_exist(p):
  123. return p
  124. path = path.split(path_seperator())
  125. path.pop()
  126. path = path_seperator().join(path)
  127. # $HOME/.icons
  128. path = fix_path(get_home()) + '.icons'
  129. for icon in ICONS_LIST:
  130. temp_path = fix_path(path) + icon
  131. if file_exist(temp_path):
  132. return path
  133. # $XDG_DATA_DIRS/icons
  134. path = os.getenv('XDG_DATA_DIRS')
  135. for temp_path in path.split(':'):
  136. temp_path = fix_path(temp_path) + 'icons'
  137. temp_path = fix_path(temp_path) + 'hicolor'
  138. for size in SIZES:
  139. p = fix_path(temp_path) + size
  140. p = fix_path(p) + 'apps'
  141. p = fix_path(p) + ICON_NAME + size + ICON_EXTENSION
  142. if file_exist(p):
  143. return p
  144. # /usr/share/pixmaps
  145. path = '/usr/share/pixmaps/'
  146. for icon in ICONS_LIST:
  147. temp_path = path + icon
  148. if file_exist(temp_path):
  149. return temp_path
  150. return None