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.

136 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
  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. 'write_subs': False,
  53. 'write_all_subs': False,
  54. 'write_auto_subs': False,
  55. 'embed_subs': False,
  56. 'subs_lang': 'English',
  57. 'ignore_errors': True,
  58. 'open_dl_dir': True,
  59. 'write_description': False,
  60. 'write_info': False,
  61. 'write_thumbnail': False,
  62. 'retries': 10,
  63. 'user_agent': '',
  64. 'referer': '',
  65. 'proxy': '',
  66. 'shutdown': False,
  67. 'sudo_password': '',
  68. 'username': '',
  69. 'password': '',
  70. 'video_password': '',
  71. 'youtubedl_path': self.config_path,
  72. 'cmd_args': '',
  73. 'enable_log': True,
  74. 'log_time': False
  75. }
  76. def load_from_file(self):
  77. ''' Load options from settings file. '''
  78. if not os.path.exists(self.settings_file):
  79. return
  80. with open(self.settings_file, 'rb') as settings_file:
  81. try:
  82. options = json.load(settings_file)
  83. except:
  84. self.load_default()
  85. if self._settings_are_valid(options):
  86. self.options = options
  87. def save_to_file(self):
  88. ''' Save options to settings file. '''
  89. check_path(self.config_path)
  90. with open(self.settings_file, 'wb') as settings_file:
  91. options = self._get_options()
  92. json.dump(options,
  93. settings_file,
  94. indent=4,
  95. separators=(',', ': '))
  96. def _settings_are_valid(self, settings_dictionary):
  97. ''' Check settings.json dictionary. Return True if
  98. settings.json dictionary is valid, else return False.
  99. '''
  100. for key in self.options:
  101. if key not in settings_dictionary:
  102. return False
  103. return True
  104. def _get_options(self):
  105. ''' Return options dictionary without SENSITIVE_KEYS. '''
  106. temp_options = self.options.copy()
  107. for key in self.SENSITIVE_KEYS:
  108. temp_options[key] = ''
  109. return temp_options