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.

119 lines
3.4 KiB

10 years ago
10 years ago
10 years ago
10 years ago
  1. #! /usr/bin/env python
  2. import json
  3. from .Utils import (
  4. check_path,
  5. file_exist,
  6. get_home,
  7. fix_path
  8. )
  9. class OptionsHandler(object):
  10. SETTINGS_FILENAME = 'settings.json'
  11. SENSITIVE_KEYS = ('sudo_password', 'password', 'video_password')
  12. def __init__(self, config_path):
  13. self.config_path = config_path
  14. self.settings_file = self._get_settings_file()
  15. self.load_default()
  16. self.load_from_file()
  17. def load_default(self):
  18. self.options = {
  19. 'save_path': get_home(),
  20. 'video_format': 'default',
  21. 'dash_audio_format': 'none',
  22. 'clear_dash_files': False,
  23. 'to_audio': False,
  24. 'keep_video': False,
  25. 'audio_format': 'mp3',
  26. 'audio_quality': 'mid',
  27. 'restrict_filenames': False,
  28. 'output_format': 'title',
  29. 'output_template': '%(uploader)s/%(title)s.%(ext)s',
  30. 'playlist_start': 1,
  31. 'playlist_end': 0,
  32. 'max_downloads': 0,
  33. 'min_filesize': '0',
  34. 'max_filesize': '0',
  35. 'write_subs': False,
  36. 'write_all_subs': False,
  37. 'write_auto_subs': False,
  38. 'embed_subs': False,
  39. 'subs_lang': 'English',
  40. 'ignore_errors': True,
  41. 'open_dl_dir': True,
  42. 'write_description': False,
  43. 'write_info': False,
  44. 'write_thumbnail': False,
  45. 'retries': 10,
  46. 'user_agent': '',
  47. 'referer': '',
  48. 'proxy': '',
  49. 'shutdown': False,
  50. 'sudo_password': '',
  51. 'username': '',
  52. 'password': '',
  53. 'video_password': '',
  54. 'youtubedl_path': self.config_path,
  55. 'cmd_args': '',
  56. 'enable_log': True,
  57. 'log_time': False
  58. }
  59. def load_from_file(self):
  60. if not file_exist(self.settings_file):
  61. self.load_default()
  62. return
  63. with open(self.settings_file, 'rb') as f:
  64. try:
  65. options = json.load(f)
  66. # Raise WrongSettings Exception if NOT
  67. self._settings_are_valid(options)
  68. self.options = options
  69. except:
  70. self.load_default()
  71. def save_to_file(self):
  72. check_path(self.config_path)
  73. with open(self.settings_file, 'wb') as f:
  74. options = self._get_options()
  75. json.dump(options, f, indent=4, separators=(',', ': '))
  76. def _settings_are_valid(self, settings_dictionary):
  77. ''' Check settings.json dictionary and raise WrongSettings Exception '''
  78. if len(settings_dictionary) != len(self.options):
  79. raise WrongSettings()
  80. for key in self.options:
  81. if key not in settings_dictionary:
  82. raise WrongSettings()
  83. def _get_options(self):
  84. ''' Return options dictionary without SENSITIVE_KEYS '''
  85. temp_options = {}
  86. for key in self.options:
  87. if key in self.SENSITIVE_KEYS:
  88. temp_options[key] = ''
  89. else:
  90. temp_options[key] = self.options[key]
  91. return temp_options
  92. def _get_settings_file(self):
  93. ''' Return abs path to settings file '''
  94. return fix_path(self.config_path) + self.SETTINGS_FILENAME
  95. class WrongSettings(Exception):
  96. ''' Wrong settings exception.
  97. This exception will be raised if settings dictionary is not valid.
  98. '''
  99. pass