settings.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import configparser
  2. import os
  3. from pathlib import Path
  4. from helper import console
  5. class Settings:
  6. # Collect all the setting instances
  7. __list__ = []
  8. __config__ = None
  9. __file__ = Path('settings.ini')
  10. def initialize():
  11. from .settingsentry import SettingEntry
  12. # Directory to permanently save the file (ENV: MD_SAVEDIR)
  13. Settings.SaveDir = SettingEntry('saveDir', 'MD_SAVEDIR', '~/Downloads', 'music-downloader', os.path.expanduser)
  14. # Directory to temporary download the file (ENV: MD_TMP)
  15. Settings.tmpDir = SettingEntry('tmpDir', 'MD_TMP', '~/tmp', 'music-downloader', os.path.expanduser)
  16. # Minimal debug level (ENV: MD_LOGGING)
  17. Settings.Debuglvl = SettingEntry('debuglvl', 'MD_LOGGING', 0, 'music-downloader', int)
  18. # Minimal bitrate to auto download the file (ENV: MD_QUALITY)
  19. Settings.MinQuality = SettingEntry('minQuality', 'MD_QUALITY', 300, 'music-downloader', int)
  20. # Format for downloaded file ID3 comment
  21. Settings.CommentFormat = SettingEntry('commentFormat', 'MD_COMMENT', '', 'music-downloader')
  22. # Spotify settings
  23. Settings.SpotifyUsername = SettingEntry(namespace='spotify', name='username')
  24. Settings.SpotifyClientID = SettingEntry(namespace='spotify', name='clientid')
  25. Settings.SpotifySecret = SettingEntry(namespace='spotify', name='secret')
  26. Settings.SpotifyScope = SettingEntry(namespace='spotify', name='scope', default='playlist-read-private,playlist-read-collaborative')
  27. def write():
  28. for entry in Settings.__list__:
  29. entry.push_config()
  30. console.output('Writing settings to \'{0}\''.format(Settings.__file__.absolute()), console.DBG_INFO)
  31. Settings.__config__.write(Settings.__file__.open('w'))
  32. def read():
  33. _config = configparser.RawConfigParser()
  34. if Settings.__file__.exists():
  35. console.output('Reading settings from \'{0}\''.format(Settings.__file__.absolute()), console.DBG_INFO)
  36. _config.read(Settings.__file__)
  37. Settings.__config__ = _config
  38. else:
  39. console.output('Settings not found at \'{0}\''.format(Settings.__file__.absolute()), console.DBG_INFO)
  40. SaveDir = None
  41. tmpDir = None
  42. Debuglvl = None
  43. MinQuality = None
  44. CommentFormat = None
  45. SpotifyUsername = None
  46. SpotifyClientID = None
  47. SpotifySecret = None
  48. SpotifyScope = None