Procházet zdrojové kódy

Update settings routine

debenoldert před 5 roky
rodič
revize
4562ce3796
5 změnil soubory, kde provedl 133 přidání a 58 odebrání
  1. 2 4
      command/__init__.py
  2. 37 0
      command/settings.py
  3. 47 53
      helper/settings.py
  4. 44 0
      helper/settingsentry.py
  5. 3 1
      main.py

+ 2 - 4
command/__init__.py

@@ -4,13 +4,11 @@ from .directory import directory
 from .help import help
 from .last import last
 from .quality import quality
+from .settings import settings
 
 commands = [
     ('help', help, 'Display this list'),
     ('last', last, 'See the last search'),
-    ('save', directory, 'Change the download directory'),
     ('search', search, 'Search the selected site'),
-    ('sites', choose_site, 'Pick the site to search'),
-    ('quality', quality, 'Set the minimal quality to download without confirmation'),
-    #('quit', quit, 'Quit the program')
+    ('settings', settings, 'View/change settings')
 ]

+ 37 - 0
command/settings.py

@@ -0,0 +1,37 @@
+from helper import console
+from helper.settings import Settings
+
+
+def settings():
+    while True:
+        setting = console.option_picker('Choose which setting to change',
+                              Settings.__list__,
+                              quit=True,
+                              objects=[
+                                  '__id__',
+                                  'x.name',
+                                  'x.str_value'
+                              ],
+                              table=[
+                                  ('ID', 2),
+                                  ('Setting', 20),
+                                  ('Value', 100)
+                              ])
+
+        if setting is None:
+            console.output('Saving settings...')
+            Settings.write()
+            console.output('Settings saved')
+            break
+        else:
+            entry = Settings.__list__[setting]
+
+            value = console.ask_input('Enter new value', entry.type, quit=True)
+
+            if value is None:
+                console.output('Value not changed', console.DBG_INFO)
+                continue
+            else:
+                console.output('Changing value of {x.name} from {x.value} to {new}'.format(x=entry, new=value), console.DBG_INFO)
+                entry.set(value)
+    return True

+ 47 - 53
helper/settings.py

@@ -1,57 +1,51 @@
-import os
 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='DEFAULT', 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
-
-        self.read_config()
-
-        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):
-        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)
+import os
+from pathlib import Path
+from helper import console
 
 
 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('commentFormat', 'MD_COMMENT', '', 'music-downloader')
+    # Collect all the setting instances
+    __list__ = []
+
+    __config__ = None
+
+    __file__ = Path('settings.ini')
+
+    def initialize():
+        from .settingsentry import SettingEntry
+        # Directory to permanently save the file (ENV: MD_SAVEDIR)
+        Settings.SaveDir = SettingEntry('saveDir', 'MD_SAVEDIR', '~/Downloads', 'music-downloader', os.path.expanduser)
+
+        # Directory to temporary download the file (ENV: MD_TMP)
+        Settings.tmpDir = SettingEntry('tmpDir', 'MD_TMP', '~/tmp', 'music-downloader', os.path.expanduser)
+
+        # Minimal debug level (ENV: MD_LOGGING)
+        Settings.Debuglvl = SettingEntry('debuglvl', 'MD_LOGGING', 0, 'music-downloader', int)
+
+        # Minimal bitrate to auto download the file (ENV: MD_QUALITY)
+        Settings.MinQuality = SettingEntry('minQuality', 'MD_QUALITY', 300, 'music-downloader', int)
+
+        # Format for downloaded file ID3 comment
+        Settings.CommentFormat = SettingEntry('commentFormat', 'MD_COMMENT', '', 'music-downloader')
+
+    def write():
+        for entry in Settings.__list__:
+            entry.push_config()
+        console.output('Writing settings to \'{0}\''.format(Settings.__file__.absolute()), console.DBG_INFO)
+        Settings.__config__.write(Settings.__file__.open('w'))
+
+    def read():
+        _config = configparser.ConfigParser()
+        if Settings.__file__.exists():
+            console.output('Reading settings from \'{0}\''.format(Settings.__file__.absolute()), console.DBG_INFO)
+            _config.read(Settings.__file__)
+            Settings.__config__ = _config
+        else:
+            console.output('Settings not found at \'{0}\''.format(Settings.__file__.absolute()), console.DBG_INFO)
+
+    SaveDir = None
+    tmpDir = None
+    Debuglvl = None
+    MinQuality = None
+    CommentFormat = None

+ 44 - 0
helper/settingsentry.py

@@ -0,0 +1,44 @@
+import os
+
+from .settings import Settings
+
+
+class SettingEntry:
+    def __init__(self, name, env_name=None, default=None, namespace='DEFAULT', 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
+
+        self.str_value = None
+
+        self.read_config()
+
+        if os.getenv(self.environment) is not None:
+            self.read_from_env = True
+            self.set(os.getenv(self.environment))
+
+        Settings.__list__.append(self)
+
+    def __str__(self):
+        return '{0} => {1}'.format(self.name, self.value)
+
+    def __eq__(self, other):
+        return self.name == other
+
+    def read_config(self):
+        if self.namespace in Settings.__config__ and self.name in Settings.__config__[self.namespace] and not self.read_from_env:
+            self.set(Settings.__config__[self.namespace][self.name])
+
+    def push_config(self):
+        Settings.__config__[self.namespace][self.name] = self.str_value
+
+    def set(self, value):
+        self.value = self.type(value)
+        self.str_value = str(self.value)
+
+    def get(self):
+        return self.type(self.value)

+ 3 - 1
main.py

@@ -6,10 +6,12 @@ import command
 
 
 def main():
+    Settings.read()
+    Settings.initialize()
+
     os.makedirs(Settings.tmpDir.get(), exist_ok=True)
     console.unlock()
 
-
     console.output('Music downloader by Deben Oldert')
     console.output()