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.

163 lines
4.1 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  1. #!/usr/bin/env python2
  2. ''' Contains youtube-dlG util functions. '''
  3. import os
  4. import sys
  5. import subprocess
  6. def is_dash(word):
  7. ''' Return True if "DASH" in word. '''
  8. return "DASH" in word
  9. def path_seperator():
  10. ''' Return path seperator for current OS. '''
  11. return '\\' if os.name == 'nt' else '/'
  12. def fix_path(path):
  13. ''' Add path seperator at the end of the path
  14. if not exist and replace ~ with user $HOME '''
  15. if path == '':
  16. return path
  17. if path[-1:] != path_seperator():
  18. path += path_seperator()
  19. path_list = path.split(path_seperator())
  20. for index, item in enumerate(path_list):
  21. if item == '~':
  22. path_list[index] = get_home()
  23. path = path_seperator().join(path_list)
  24. return path
  25. def get_home():
  26. ''' Return user $HOME path. '''
  27. return os.path.expanduser("~")
  28. def abs_path(filename):
  29. ''' Return absolute path. '''
  30. path = os.path.realpath(os.path.abspath(filename))
  31. return os.path.dirname(path)
  32. def open_dir(path):
  33. ''' Open path using default file navigator. '''
  34. if os.name == 'nt':
  35. os.startfile(path)
  36. else:
  37. subprocess.call(('xdg-open', path))
  38. def check_path(path):
  39. ''' Create path if not exist. '''
  40. if not os.path.exists(path):
  41. os.makedirs(path)
  42. def get_youtubedl_filename():
  43. ''' Return youtube-dl executable name. '''
  44. youtubedl_fl = 'youtube-dl'
  45. if os.name == 'nt':
  46. youtubedl_fl += '.exe'
  47. return youtubedl_fl
  48. def get_config_path():
  49. ''' Return user config path. Windows=AppData, Linux=~/.config. '''
  50. if os.name == 'nt':
  51. path = os.getenv('APPDATA')
  52. else:
  53. path = fix_path(get_home()) + '.config'
  54. return path
  55. def shutdown_sys(password=''):
  56. ''' Shutdown system. !!!On Linux you need to provide
  57. password for sudo if you dont have admin prev.
  58. '''
  59. if os.name == 'nt':
  60. subprocess.call(['shutdown', '/s', '/t', '1'])
  61. else:
  62. if password == '':
  63. subprocess.call(['/sbin/shutdown', '-h', 'now'])
  64. else:
  65. shutdown_proc = subprocess.Popen(
  66. ['sudo', '-S', '/sbin/shutdown', '-h', 'now'],
  67. stdin=subprocess.PIPE
  68. )
  69. shutdown_proc.communicate(password + '\n')
  70. def get_time(seconds):
  71. ''' Return day, hours, minutes, seconds from given seconds. '''
  72. dtime = {'seconds': 0, 'minutes': 0, 'hours': 0, 'days': 0}
  73. if seconds < 60:
  74. dtime['seconds'] = seconds
  75. elif seconds < 3600:
  76. dtime['minutes'] = seconds / 60
  77. dtime['seconds'] = seconds % 60
  78. elif seconds < 86400:
  79. dtime['hours'] = seconds / 3600
  80. dtime['minutes'] = seconds % 3600 / 60
  81. dtime['seconds'] = seconds % 3600 % 60
  82. else:
  83. dtime['days'] = seconds / 86400
  84. dtime['hours'] = seconds % 86400 / 3600
  85. dtime['minutes'] = seconds % 86400 % 3600 / 60
  86. dtime['seconds'] = seconds % 86400 % 3600 % 60
  87. return dtime
  88. def get_icon_path():
  89. ''' Return path to the icon file if exist else return None.
  90. Search __main__ dir, $XDG_DATA_DIRS, /usr/share/pixmaps in that order. '''
  91. SIZES = ('256x256', '128x128', '64x64', '48x48', '32x32', '16x16')
  92. ICON_NAME = 'youtube-dl-gui_%s.png'
  93. ICONS_LIST = [ICON_NAME % size for size in SIZES]
  94. # __main__ dir
  95. path = os.path.join(abs_path(sys.argv[0]), 'icons')
  96. for icon in ICONS_LIST:
  97. icon_path = os.path.join(path, icon)
  98. if os.path.exists(icon_path):
  99. return icon_path
  100. # $XDG_DATA_DIRS/icons
  101. path = os.getenv('XDG_DATA_DIRS')
  102. if path is not None:
  103. for temp_path in path.split(':'):
  104. temp_path = os.path.join(temp_path, 'icons', 'hicolor')
  105. for size in SIZES:
  106. icon_path = os.path.join(temp_path, size, 'apps')
  107. icon_path = fix_path(icon_path) + ICON_NAME % size
  108. if os.path.exists(icon_path):
  109. return icon_path
  110. # /usr/share/pixmaps
  111. path = '/usr/share/pixmaps/'
  112. for icon in ICONS_LIST:
  113. icon_path = path + icon
  114. if os.path.exists(icon_path):
  115. return icon_path
  116. return None