Explorar el Código

halfway there

Deben Oldert hace 6 años
padre
commit
3aee1c9ed7
Se han modificado 3 ficheros con 38 adiciones y 11 borrados
  1. 1 1
      helper/console.py
  2. 36 9
      helper/settings.py
  3. 1 1
      main.py

+ 1 - 1
helper/console.py

@@ -19,7 +19,7 @@ def output(text='', level=OUTPUT, end='\n'):
         CACHE.append((text, level, end))
         return
 
-    if level <= Settings.Debuglvl:
+    if level <= Settings.Debuglvl.get():
 
         if level == 1:
             text = color.Fore.RED + '[ERROR] ' + text

+ 36 - 9
helper/settings.py

@@ -1,21 +1,51 @@
 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 = None
+    SaveDir = SettingEntry('saveDir', 'MD_SAVEDIR', '~/Downloads', 'music-downloader', os.path.expanduser)
 
     # Directory to temporary download the file (ENV: MD_TMP)
-    tmpDir = '/tmp'
+    tmpDir = SettingEntry('tmpDir', 'MD_TMP', '~/tmp', 'music-downloader', os.path.expanduser)
 
     # Minimal debug level (ENV: MD_LOGGING)
-    Debuglvl = 0
+    Debuglvl = SettingEntry('debuglvl', 'MD_LOGGING', 0, 'music-downloader', int)
 
     # Minimal bitrate to auto download the file (ENV: MD_QUALITY)
-    MinQuality = 300
+    MinQuality = SettingEntry('minQuality', 'MD_QUALITY', 300, 'music-downloader', int)
 
     #Format for downloaded file ID3 comment
-    CommentFormat = None
+    CommentFormat = SettingEntry('saveDir', 'MD_SAVEDIR', '~/Downloads', 'music-downloader')
 
     @staticmethod
     def import_config(config):
@@ -33,17 +63,14 @@ class Settings:
                 Settings.CommentFormat = config[namespace]['commentFormat']
 
         if os.getenv('MD_SAVEDIR') is not None:
-            console.output('Overrule MD_SAVEDIR', console.DBG_INFO)
             Settings.SaveDir = os.getenv('MD_SAVEDIR')
         if os.getenv('MD_TMP') is not None:
-            console.output('Overrule MD_TMP', console.DBG_INFO)
             Settings.SaveDir = os.getenv('MD_TMP')
         if os.getenv('MD_LOGGING') is not None:
-            console.output('Overrule MD_LOGGING', console.DBG_INFO)
             Settings.SaveDir = os.getenv('MD_LOGGING')
         if os.getenv('MD_QUALITY') is not None:
-            console.output('Overrule MD_QUALITY', console.DBG_INFO)
             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)

+ 1 - 1
main.py

@@ -7,7 +7,7 @@ from helper.settings import Settings
 
 def init():
     if os.path.isfile('settings.ini'):
-        console.output('Settings file found', console.DBG_INFO)
+        #console.output('Settings file found', console.DBG_INFO)
         config = configparser.ConfigParser()
         config.read('settings.ini')
         Settings.import_config(config)