settings.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import os
  2. import configparser
  3. CONFIG = configparser.ConfigParser()
  4. if os.path.isfile('settings.ini'):
  5. CONFIG.read('settings.ini')
  6. class SettingEntry:
  7. def __init__(self, name, env_name=None, default=None, namespace='DEFAULT', type=str):
  8. self.name = name
  9. self.environment = env_name
  10. self.default = default
  11. self.value = default
  12. self.namespace = namespace
  13. self.read_from_env = False
  14. self.type = type
  15. self.read_config()
  16. if os.getenv(self.environment) is not None:
  17. self.read_from_env = True
  18. self.set(os.getenv(self.environment))
  19. def __str__(self):
  20. return '{} => {}'.format(self.name, self.value)
  21. def __eq__(self, other):
  22. return self.name == other
  23. def read_config(self):
  24. if self.namespace in CONFIG and self.name in CONFIG[self.namespace] and not self.read_from_env:
  25. self.set(CONFIG[self.namespace][self.name])
  26. def set(self, value):
  27. self.value = self.type(value)
  28. def get(self):
  29. return self.type(self.value)
  30. class Settings:
  31. # Directory to permanently save the file (ENV: MD_SAVEDIR)
  32. SaveDir = SettingEntry('saveDir', 'MD_SAVEDIR', '~/Downloads', 'music-downloader', os.path.expanduser)
  33. # Directory to temporary download the file (ENV: MD_TMP)
  34. tmpDir = SettingEntry('tmpDir', 'MD_TMP', '~/tmp', 'music-downloader', os.path.expanduser)
  35. # Minimal debug level (ENV: MD_LOGGING)
  36. Debuglvl = SettingEntry('debuglvl', 'MD_LOGGING', 0, 'music-downloader', int)
  37. # Minimal bitrate to auto download the file (ENV: MD_QUALITY)
  38. MinQuality = SettingEntry('minQuality', 'MD_QUALITY', 300, 'music-downloader', int)
  39. #Format for downloaded file ID3 comment
  40. CommentFormat = SettingEntry('saveDir', 'MD_SAVEDIR', '~/Downloads', 'music-downloader')