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
3.8 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
  1. #!/usr/bin/env python2
  2. """Youtubedlg module that contains util functions.
  3. Attributes:
  4. YOUTUBEDL_BIN (string): Youtube-dl binary filename.
  5. """
  6. import os
  7. import sys
  8. import subprocess
  9. YOUTUBEDL_BIN = 'youtube-dl'
  10. if os.name == 'nt':
  11. YOUTUBEDL_BIN += '.exe'
  12. def remove_shortcuts(path):
  13. """Return given path after removing the shortcuts. """
  14. path = path.replace('~', os.path.expanduser('~'))
  15. return path
  16. def absolute_path(filename):
  17. """Return absolute path of the given file. """
  18. path = os.path.realpath(os.path.abspath(filename))
  19. return os.path.dirname(path)
  20. def open_dir(path):
  21. """Open path using default file navigator. """
  22. path = remove_shortcuts(path)
  23. if os.name == 'nt':
  24. os.startfile(path)
  25. else:
  26. subprocess.call(('xdg-open', path))
  27. def check_path(path):
  28. """Create path if not exist. """
  29. if not os.path.exists(path):
  30. os.makedirs(path)
  31. def get_config_path():
  32. """Return user config path.
  33. Note:
  34. Windows = %AppData%
  35. Linux = ~/.config
  36. """
  37. if os.name == 'nt':
  38. path = os.getenv('APPDATA')
  39. else:
  40. path = os.path.join(os.path.expanduser('~'), '.config')
  41. return path
  42. def shutdown_sys(password=''):
  43. """Shuts down the system.
  44. Args:
  45. password (string): SUDO password for linux.
  46. Note:
  47. On Linux you need to provide sudo password if you don't
  48. have admin prev.
  49. """
  50. if os.name == 'nt':
  51. subprocess.call(['shutdown', '/s', '/t', '1'])
  52. else:
  53. if not password:
  54. subprocess.call(['/sbin/shutdown', '-h', 'now'])
  55. else:
  56. subprocess.Popen(['sudo', '-S', '/sbin/shutdown', '-h', 'now'],
  57. stdin=subprocess.PIPE).communicate(password + '\n')
  58. def get_time(seconds):
  59. """Convert given seconds to days, hours, minutes and seconds.
  60. Args:
  61. seconds (float): Time in seconds.
  62. Returns:
  63. Dictionary that contains the corresponding days, hours, minutes
  64. and seconds of the given seconds.
  65. """
  66. dtime = dict(seconds=0, minutes=0, hours=0, days=0)
  67. dtime['days'] = int(seconds / 86400)
  68. dtime['hours'] = int(seconds % 86400 / 3600)
  69. dtime['minutes'] = int(seconds % 86400 % 3600 / 60)
  70. dtime['seconds'] = round((seconds % 86400 % 3600 % 60), 2)
  71. return dtime
  72. def get_icon_file():
  73. """Search for youtube-dlg app icon.
  74. Returns:
  75. The path to youtube-dlg icon file if exists. Else returns None.
  76. Note:
  77. Paths that get_icon_file() function searches.
  78. Windows: __main__ directory.
  79. Linux: __main__ directory, $XDG_DATA_DIRS and /usr/share/pixmaps.
  80. """
  81. SIZES = ('256x256', '128x128', '64x64', '48x48', '32x32', '16x16')
  82. ICON_NAME = 'youtube-dl-gui_%s.png'
  83. ICONS_LIST = [ICON_NAME % size for size in SIZES]
  84. # __main__ dir
  85. path = os.path.join(absolute_path(sys.argv[0]), 'icons')
  86. for icon in ICONS_LIST:
  87. icon_file = os.path.join(path, icon)
  88. if os.path.exists(icon_file):
  89. return icon_file
  90. if os.name != 'nt':
  91. # $XDG_DATA_DIRS/icons
  92. path = os.getenv('XDG_DATA_DIRS')
  93. if path is not None:
  94. for xdg_path in path.split(':'):
  95. xdg_path = os.path.join(xdg_path, 'icons', 'hicolor')
  96. for size in SIZES:
  97. icon_name = ICON_NAME % size
  98. icon_file = os.path.join(xdg_path, size, 'apps', icon_name)
  99. if os.path.exists(icon_file):
  100. return icon_file
  101. # /usr/share/pixmaps
  102. path = '/usr/share/pixmaps'
  103. for icon in ICONS_LIST:
  104. icon_file = os.path.join(path, icon)
  105. if os.path.exists(icon_file):
  106. return icon_file
  107. return None