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.

138 lines
3.9 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
  1. #!/usr/bin/env python2
  2. ''' Youtube-dlG module to handle settings. '''
  3. import json
  4. import os.path
  5. from .utils import check_path
  6. class OptionsManager(object):
  7. '''
  8. Manage youtube-dlG settings.
  9. Params:
  10. config_path: Absolute path where OptionsManager
  11. should store settings file.
  12. Accessible Methods
  13. load_default()
  14. Params: None
  15. Return: None
  16. load_from_file()
  17. Params: None
  18. Return: None
  19. save_to_file()
  20. Params: None
  21. Return: None
  22. Accessible Variables
  23. settings_file: Absolute path to settings file.
  24. options: Python dictionary that contains all the options.
  25. '''
  26. SETTINGS_FILENAME = 'settings.json'
  27. SENSITIVE_KEYS = ('sudo_password', 'password', 'video_password')
  28. def __init__(self, config_path):
  29. self.config_path = config_path
  30. self.settings_file = os.path.join(config_path, self.SETTINGS_FILENAME)
  31. self.options = dict()
  32. self.load_default()
  33. self.load_from_file()
  34. def load_default(self):
  35. ''' Load default options. '''
  36. self.options = {
  37. 'save_path': os.path.expanduser('~'),
  38. 'video_format': 'default',
  39. 'second_video_format': 'none',
  40. 'to_audio': False,
  41. 'keep_video': False,
  42. 'audio_format': 'mp3',
  43. 'audio_quality': 'mid',
  44. 'restrict_filenames': False,
  45. 'output_format': 'title',
  46. 'output_template': '%(uploader)s/%(title)s.%(ext)s',
  47. 'playlist_start': 1,
  48. 'playlist_end': 0,
  49. 'max_downloads': 0,
  50. 'min_filesize': 0,
  51. 'max_filesize': 0,
  52. 'min_filesize_unit': 'Bytes',
  53. 'max_filesize_unit': 'Bytes',
  54. 'write_subs': False,
  55. 'write_all_subs': False,
  56. 'write_auto_subs': False,
  57. 'embed_subs': False,
  58. 'subs_lang': 'English',
  59. 'ignore_errors': True,
  60. 'open_dl_dir': True,
  61. 'write_description': False,
  62. 'write_info': False,
  63. 'write_thumbnail': False,
  64. 'retries': 10,
  65. 'user_agent': '',
  66. 'referer': '',
  67. 'proxy': '',
  68. 'shutdown': False,
  69. 'sudo_password': '',
  70. 'username': '',
  71. 'password': '',
  72. 'video_password': '',
  73. 'youtubedl_path': self.config_path,
  74. 'cmd_args': '',
  75. 'enable_log': True,
  76. 'log_time': False
  77. }
  78. def load_from_file(self):
  79. ''' Load options from settings file. '''
  80. if not os.path.exists(self.settings_file):
  81. return
  82. with open(self.settings_file, 'rb') as settings_file:
  83. try:
  84. options = json.load(settings_file)
  85. except:
  86. self.load_default()
  87. if self._settings_are_valid(options):
  88. self.options = options
  89. def save_to_file(self):
  90. ''' Save options to settings file. '''
  91. check_path(self.config_path)
  92. with open(self.settings_file, 'wb') as settings_file:
  93. options = self._get_options()
  94. json.dump(options,
  95. settings_file,
  96. indent=4,
  97. separators=(',', ': '))
  98. def _settings_are_valid(self, settings_dictionary):
  99. ''' Check settings.json dictionary. Return True if
  100. settings.json dictionary is valid, else return False.
  101. '''
  102. for key in self.options:
  103. if key not in settings_dictionary:
  104. return False
  105. return True
  106. def _get_options(self):
  107. ''' Return options dictionary without SENSITIVE_KEYS. '''
  108. temp_options = self.options.copy()
  109. for key in self.SENSITIVE_KEYS:
  110. temp_options[key] = ''
  111. return temp_options