settings.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. def write():
  23. for entry in Settings.__list__:
  24. entry.push_config()
  25. console.output('Writing settings to \'{0}\''.format(Settings.__file__.absolute()), console.DBG_INFO)
  26. Settings.__config__.write(Settings.__file__.open('w'))
  27. def read():
  28. _config = configparser.ConfigParser()
  29. if Settings.__file__.exists():
  30. console.output('Reading settings from \'{0}\''.format(Settings.__file__.absolute()), console.DBG_INFO)
  31. _config.read(Settings.__file__)
  32. Settings.__config__ = _config
  33. else:
  34. console.output('Settings not found at \'{0}\''.format(Settings.__file__.absolute()), console.DBG_INFO)
  35. SaveDir = None
  36. tmpDir = None
  37. Debuglvl = None
  38. MinQuality = None
  39. CommentFormat = None