settings.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import os
  2. import helper.console as console
  3. class SettingEntry:
  4. def __init__(self, name, env_name=None, default=None, namespace=None, type=str):
  5. self.name = name
  6. self.environment = env_name
  7. self.default = default
  8. self.value = default
  9. self.namespace = namespace
  10. self.read_from_env = False
  11. self.type = type
  12. if os.getenv(self.environment) is not None:
  13. self.read_from_env = True
  14. self.set(os.getenv(self.environment))
  15. def __str__(self):
  16. return '{} => {}'.format(self.name, self.value)
  17. def __eq__(self, other):
  18. return self.name == other
  19. def read_config(self, config):
  20. if self.namespace in config and self.name in config[self.namespace] and not self.read_from_env:
  21. self.set(config[self.namespace][self.name])
  22. def set(self, value):
  23. self.value = self.type(value)
  24. def get(self):
  25. return self.type(self.value)
  26. class Settings:
  27. # Directory to permanently save the file (ENV: MD_SAVEDIR)
  28. SaveDir = SettingEntry('saveDir', 'MD_SAVEDIR', '~/Downloads', 'music-downloader', os.path.expanduser)
  29. # Directory to temporary download the file (ENV: MD_TMP)
  30. tmpDir = SettingEntry('tmpDir', 'MD_TMP', '~/tmp', 'music-downloader', os.path.expanduser)
  31. # Minimal debug level (ENV: MD_LOGGING)
  32. Debuglvl = SettingEntry('debuglvl', 'MD_LOGGING', 0, 'music-downloader', int)
  33. # Minimal bitrate to auto download the file (ENV: MD_QUALITY)
  34. MinQuality = SettingEntry('minQuality', 'MD_QUALITY', 300, 'music-downloader', int)
  35. #Format for downloaded file ID3 comment
  36. CommentFormat = SettingEntry('saveDir', 'MD_SAVEDIR', '~/Downloads', 'music-downloader')
  37. @staticmethod
  38. def import_config(config):
  39. namespace = 'music-downloader'
  40. if namespace in config:
  41. if 'saveDir' in config[namespace]:
  42. Settings.SaveDir = config[namespace]['saveDir']
  43. if 'tmpDir' in config[namespace]:
  44. Settings.tmpDir = config[namespace]['tmpDir']
  45. if 'debuglvl' in config[namespace]:
  46. Settings.Debuglvl = config[namespace]['debuglvl']
  47. if 'minQuality' in config[namespace]:
  48. Settings.MinQuality = config[namespace]['minQuality']
  49. if 'commentFormat' in config[namespace]:
  50. Settings.CommentFormat = config[namespace]['commentFormat']
  51. if os.getenv('MD_SAVEDIR') is not None:
  52. Settings.SaveDir = os.getenv('MD_SAVEDIR')
  53. if os.getenv('MD_TMP') is not None:
  54. Settings.SaveDir = os.getenv('MD_TMP')
  55. if os.getenv('MD_LOGGING') is not None:
  56. Settings.SaveDir = os.getenv('MD_LOGGING')
  57. if os.getenv('MD_QUALITY') is not None:
  58. Settings.SaveDir = os.getenv('MD_QUALITY')
  59. Settings.SaveDir = os.path.expanduser(Settings.SaveDir)
  60. Settings.tmpDir = os.path.expanduser(Settings.tmpDir)
  61. Settings.Debuglvl = int(Settings.Debuglvl)