|
|
@@ -1,8 +1,14 @@
|
|
|
import os
|
|
|
-import helper.console as console
|
|
|
+import configparser
|
|
|
+
|
|
|
+
|
|
|
+CONFIG = configparser.ConfigParser()
|
|
|
+if os.path.isfile('settings.ini'):
|
|
|
+ CONFIG.read('settings.ini')
|
|
|
+
|
|
|
|
|
|
class SettingEntry:
|
|
|
- def __init__(self, name, env_name=None, default=None, namespace=None, type=str):
|
|
|
+ def __init__(self, name, env_name=None, default=None, namespace='DEFAULT', type=str):
|
|
|
self.name = name
|
|
|
self.environment = env_name
|
|
|
self.default = default
|
|
|
@@ -11,6 +17,8 @@ class SettingEntry:
|
|
|
self.read_from_env = False
|
|
|
self.type = type
|
|
|
|
|
|
+ self.read_config()
|
|
|
+
|
|
|
if os.getenv(self.environment) is not None:
|
|
|
self.read_from_env = True
|
|
|
self.set(os.getenv(self.environment))
|
|
|
@@ -21,9 +29,9 @@ class SettingEntry:
|
|
|
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 read_config(self):
|
|
|
+ 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)
|
|
|
@@ -31,6 +39,7 @@ class SettingEntry:
|
|
|
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)
|
|
|
@@ -46,31 +55,3 @@ class Settings:
|
|
|
|
|
|
#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)
|