ソースを参照

Finish settings file and structure

Deben Oldert 6 年 前
コミット
4b140ea5db
4 ファイル変更29 行追加71 行削除
  1. 2 2
      helper/console.py
  2. 14 33
      helper/settings.py
  3. 3 3
      helper/sites/download.py
  4. 10 33
      main.py

+ 2 - 2
helper/console.py

@@ -7,7 +7,7 @@ OUTPUT = 0
 DBG_ERROR = 1
 DBG_INFO = 2
 
-LOCKED = False
+LOCKED = True
 
 CACHE = []
 
@@ -16,7 +16,7 @@ color.init(True)
 
 def output(text='', level=OUTPUT, end='\n'):
     if LOCKED:
-        CACHE.append((text, level, end))
+        #CACHE.append((text, level, end))
         return
 
     if level <= Settings.Debuglvl.get():

+ 14 - 33
helper/settings.py

@@ -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)

+ 3 - 3
helper/sites/download.py

@@ -36,7 +36,7 @@ def download(item, type='GET', parameters=None, headers=None, cookies=None, stre
         console.output('Assuming it\'s a mp3 file', console.DBG_INFO)
         name = name + '.mp3'
 
-    full_name = os.path.abspath('{0}/{1}'.format(Settings.tmpDir, name))
+    full_name = os.path.abspath('{0}/{1}'.format(Settings.tmpDir.get(), name))
 
     if item.bitrate is Empty and item.bytes is None:
         size = file.headers.get('content-length')
@@ -50,7 +50,7 @@ def download(item, type='GET', parameters=None, headers=None, cookies=None, stre
 
                 console.output('Bitrate: {0}kbps'.format(int(item.bitrate)))
 
-            if item.duration_seconds is not None and int(item.bitrate) < Settings.MinQuality:
+            if item.duration_seconds is not None and int(item.bitrate) < Settings.MinQuality.get():
                 ans = console.ask_input('Continue downloading? [y/n]')
 
                 if ans != 'y':
@@ -87,7 +87,7 @@ def savefileprogress(name, full_name, file, item):
 
         name = urllib.parse.unquote('{x.artist} - {x.title}'.format(x=tags)) + '.mp3'
 
-    full_save_name = os.path.abspath('{0}/{1}'.format(Settings.SaveDir, name))
+    full_save_name = os.path.abspath('{0}/{1}'.format(Settings.SaveDir.get(), name))
     os.rename(full_name, full_save_name)
     console.output('Moved to {0}'.format(full_save_name), level=console.DBG_INFO)
 

+ 10 - 33
main.py

@@ -1,41 +1,18 @@
 import sites as sites
+from helper.settings import Settings
 from helper import console as console
 import os.path
-import configparser
-from helper.settings import Settings
-
-
-def init():
-    if os.path.isfile('settings.ini'):
-        #console.output('Settings file found', console.DBG_INFO)
-        config = configparser.ConfigParser()
-        config.read('settings.ini')
-        Settings.import_config(config)
-        console.output('Settings loaded', console.DBG_INFO)
-
-    dbg_lvl = os.getenv('MD_LOGGING', 'ERROR')
-
-    if dbg_lvl.lower() == 'info':
-        Settings.Debuglvl = console.DBG_INFO
-    else:
-        Settings.Debuglvl = console.DBG_ERROR
-
-    Settings.tmpDir = os.path.expanduser(os.getenv('MD_TMP', '/tmp'))
-
-    os.makedirs(Settings.tmpDir, exist_ok=True)
 
-    directory(os.getenv('MD_SAVEDIR', '~/Downloads'))
 
-    quality(os.getenv('MD_QUALITY', '300'))
+def main():
+    os.makedirs(Settings.tmpDir.get(), exist_ok=True)
+    console.unlock()
 
 
-def main():
     console.output('Music downloader by Deben Oldert')
     console.output()
 
-    init()
-
-    console.output('Download directory: {0}'.format(Settings.SaveDir))
+    console.output('Download directory: {0}'.format(Settings.SaveDir.get()))
     console.output()
     help()
     console.output()
@@ -139,17 +116,17 @@ def search(cache=None):
 def directory(dir=None):
     if dir is None:
         dir = console.ask_input('Enter new save directory')
-    Settings.SaveDir = os.path.expanduser(dir)
-    os.makedirs(Settings.SaveDir, exist_ok=True)
-    console.output('New save directory: {0}'.format(Settings.SaveDir), level=console.DBG_INFO)
+    Settings.SaveDir.set(dir)
+    os.makedirs(Settings.SaveDir.get(), exist_ok=True)
+    console.output('New save directory: {0}'.format(Settings.SaveDir.get()), level=console.DBG_INFO)
     return True
 
 
 def quality(quality=None):
     if quality is None:
         quality = console.ask_input('Enter minimal quality for auto download')
-    Settings.MinQuality = int(quality)
-    console.output('Minimal quality for auto downloading is set to {0}'.format(Settings.MinQuality), level=console.DBG_INFO)
+    Settings.MinQuality.set(quality)
+    console.output('Minimal quality for auto downloading is set to {0}'.format(Settings.MinQuality.get()), level=console.DBG_INFO)
     return True