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.

120 lines
3.2 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
  1. #!/usr/bin/env python2
  2. ''' Contains youtube-dlG util functions. '''
  3. import os
  4. import sys
  5. import subprocess
  6. YOUTUBEDL_BIN = 'youtube-dl'
  7. if os.name == 'nt':
  8. YOUTUBEDL_BIN += '.exe'
  9. def remove_shortcuts(path):
  10. ''' Return the path after removing the shortcuts. '''
  11. path = path.replace('~', os.path.expanduser('~'))
  12. return path
  13. def absolute_path(filename):
  14. ''' Return absolute path. '''
  15. path = os.path.realpath(os.path.abspath(filename))
  16. return os.path.dirname(path)
  17. def open_dir(path):
  18. ''' Open path using default file navigator. '''
  19. path = remove_shortcuts(path)
  20. if os.name == 'nt':
  21. os.startfile(path)
  22. else:
  23. subprocess.call(('xdg-open', path))
  24. def check_path(path):
  25. ''' Create path if not exist. '''
  26. if not os.path.exists(path):
  27. os.makedirs(path)
  28. def get_config_path():
  29. ''' Return user config path. Windows=AppData, Linux=~/.config. '''
  30. if os.name == 'nt':
  31. path = os.getenv('APPDATA')
  32. else:
  33. path = os.path.join(os.path.expanduser('~'), '.config')
  34. return path
  35. def shutdown_sys(password=''):
  36. ''' Shutdown system. !!!On Linux you need to provide
  37. password for sudo if you dont have admin prev.
  38. '''
  39. if os.name == 'nt':
  40. subprocess.call(['shutdown', '/s', '/t', '1'])
  41. else:
  42. if not password:
  43. subprocess.call(['/sbin/shutdown', '-h', 'now'])
  44. else:
  45. subprocess.Popen(['sudo', '-S', '/sbin/shutdown', '-h', 'now'],
  46. stdin=subprocess.PIPE).communicate(password + '\n')
  47. def get_time(seconds):
  48. ''' Return day, hours, minutes, seconds from given seconds. '''
  49. dtime = dict(seconds=0, minutes=0, hours=0, days=0)
  50. dtime['days'] = seconds / 86400
  51. dtime['hours'] = seconds % 86400 / 3600
  52. dtime['minutes'] = seconds % 86400 % 3600 / 60
  53. dtime['seconds'] = seconds % 86400 % 3600 % 60
  54. return dtime
  55. def get_icon_file():
  56. ''' Return path to the icon file if exist else return None.
  57. Search __main__ dir, $XDG_DATA_DIRS, /usr/share/pixmaps in that order. '''
  58. SIZES = ('256x256', '128x128', '64x64', '48x48', '32x32', '16x16')
  59. ICON_NAME = 'youtube-dl-gui_%s.png'
  60. ICONS_LIST = [ICON_NAME % size for size in SIZES]
  61. # __main__ dir
  62. path = os.path.join(absolute_path(sys.argv[0]), 'icons')
  63. for icon in ICONS_LIST:
  64. icon_file = os.path.join(path, icon)
  65. if os.path.exists(icon_file):
  66. return icon_file
  67. if os.name != 'nt':
  68. # $XDG_DATA_DIRS/icons
  69. path = os.getenv('XDG_DATA_DIRS')
  70. if path is not None:
  71. for xdg_path in path.split(':'):
  72. xdg_path = os.path.join(xdg_path, 'icons', 'hicolor')
  73. for size in SIZES:
  74. icon_name = ICON_NAME % size
  75. icon_file = os.path.join(xdg_path, size, 'apps', icon_name)
  76. if os.path.exists(icon_file):
  77. return icon_file
  78. # /usr/share/pixmaps
  79. path = '/usr/share/pixmaps'
  80. for icon in ICONS_LIST:
  81. icon_file = os.path.join(path, icon)
  82. if os.path.exists(icon_file):
  83. return icon_file
  84. return None