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.

158 lines
4.0 KiB

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