import os import helper.console as console class SettingEntry: def __init__(self, name, env_name=None, default=None, namespace=None, type=str): self.name = name self.environment = env_name self.default = default self.value = default self.namespace = namespace self.read_from_env = False self.type = type if os.getenv(self.environment) is not None: self.read_from_env = True self.set(os.getenv(self.environment)) def __str__(self): return '{} => {}'.format(self.name, self.value) def __eq__(self, other): return self.name == other def read_config(self, config): if self.namespace in config and self.name in config[self.namespace] and not self.read_from_env: self.set(config[self.namespace][self.name]) def set(self, value): self.value = self.type(value) def get(self): return self.type(self.value) class Settings: # Directory to permanently save the file (ENV: MD_SAVEDIR) SaveDir = SettingEntry('saveDir', 'MD_SAVEDIR', '~/Downloads', 'music-downloader', os.path.expanduser) # Directory to temporary download the file (ENV: MD_TMP) tmpDir = SettingEntry('tmpDir', 'MD_TMP', '~/tmp', 'music-downloader', os.path.expanduser) # Minimal debug level (ENV: MD_LOGGING) Debuglvl = SettingEntry('debuglvl', 'MD_LOGGING', 0, 'music-downloader', int) # Minimal bitrate to auto download the file (ENV: MD_QUALITY) MinQuality = SettingEntry('minQuality', 'MD_QUALITY', 300, 'music-downloader', int) #Format for downloaded file ID3 comment CommentFormat = SettingEntry('saveDir', 'MD_SAVEDIR', '~/Downloads', 'music-downloader') @staticmethod def import_config(config): namespace = 'music-downloader' if namespace in config: if 'saveDir' in config[namespace]: Settings.SaveDir = config[namespace]['saveDir'] if 'tmpDir' in config[namespace]: Settings.tmpDir = config[namespace]['tmpDir'] if 'debuglvl' in config[namespace]: Settings.Debuglvl = config[namespace]['debuglvl'] if 'minQuality' in config[namespace]: Settings.MinQuality = config[namespace]['minQuality'] if 'commentFormat' in config[namespace]: Settings.CommentFormat = config[namespace]['commentFormat'] if os.getenv('MD_SAVEDIR') is not None: Settings.SaveDir = os.getenv('MD_SAVEDIR') if os.getenv('MD_TMP') is not None: Settings.SaveDir = os.getenv('MD_TMP') if os.getenv('MD_LOGGING') is not None: Settings.SaveDir = os.getenv('MD_LOGGING') if os.getenv('MD_QUALITY') is not None: Settings.SaveDir = os.getenv('MD_QUALITY') Settings.SaveDir = os.path.expanduser(Settings.SaveDir) Settings.tmpDir = os.path.expanduser(Settings.tmpDir) Settings.Debuglvl = int(Settings.Debuglvl)