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.

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