Browse Source

move configuration setup to helpers

Helmut Pozimski 6 years ago
parent
commit
d1762a9f37
2 changed files with 80 additions and 65 deletions
  1. 77 8
      lib_stov/helpers.py
  2. 3 57
      stov

+ 77 - 8
lib_stov/helpers.py

@@ -25,6 +25,9 @@ import signal
 import argparse
 import logging
 
+from lib_stov import stov_exceptions
+from lib_stov import configuration
+
 LOGGER = logging.getLogger("stov")
 
 
@@ -34,25 +37,25 @@ def initialize_gettext():
     """
     # Determine the path where the stov files are for localization
 
-    LOCALE_PATH = os.path.join(sys.path[0] + "/locale")
-    if gettext.find("stov", LOCALE_PATH) is None:
-        BASE_PATH = os.path.split(sys.path[0])[0]
-        if "share" in BASE_PATH:
-            LOCALE_PATH = os.path.join(BASE_PATH, "locale")
+    locale_path = os.path.join(sys.path[0] + "/locale")
+    if gettext.find("stov", locale_path) is None:
+        base_path = os.path.split(sys.path[0])[0]
+        if "share" in base_path:
+            locale_path = os.path.join(base_path, "locale")
         else:
-            LOCALE_PATH = os.path.join(BASE_PATH, "share/locale")
+            locale_path = os.path.join(base_path, "share/locale")
 
     # Initialize gettext to support translation of the program
 
     try:
-        TRANS = gettext.translation("stov", LOCALE_PATH)
+        trans = gettext.translation("stov", locale_path)
     except IOError:
         gettext.install("stov")
         if os.environ["LANG"] != "C" and os.environ["LANGUAGE"] != "C":
             print(_("The translation files could not be found, localization "
                     "won't be available"), file=sys.stderr)
     else:
-        TRANS.install()
+        trans.install()
 
 
 def sighandler(signum, frame):
@@ -184,3 +187,69 @@ def remove_lock():
         LOGGER.error(_("Could not delete the lock file. Please check what "
                        "went wrong and clean up manually!"))
         sys.exit(1)
+
+
+def setup_configuration(args):
+    """
+    Reads the configuration and sets it up to be used within the application.
+
+    :param args: command line arguments
+    :return: configuration object
+    :rtype: lib_stov.configuration.Conf
+    """
+    if not os.access(os.environ['HOME'] + "/.stov", os.F_OK & os.W_OK):
+        print(_("This seems to be the first time you run the programm, do "
+                "you want to run the interactive assistant? (yes/no)"))
+        conf = configuration.Conf()
+        logger = conf.configure_logging(args.verbose, args.quiet)
+        temp_input = input()
+        if temp_input == "yes":
+            conf.assist()
+            try:
+                conf.initialize()
+            except stov_exceptions.ConfigFileWriteErrorException as error:
+                logger.error(error)
+            else:
+                logger.info(_("Writing initial configuration according to "
+                              "your input, have fun!"))
+        else:
+            logger.info(_("Writing initial configuration according to default"
+                          " values."))
+            logger.debug(_("Creating hidden directory in home for "
+                           "configuration and database."))
+            try:
+                conf.initialize()
+            except stov_exceptions.DirectoryCreationFailedException as error:
+                logger.error(error)
+            except stov_exceptions.ConfigFileWriteErrorException as error:
+                logger.error(error)
+    else:
+        conf = configuration.Conf()
+        logger = conf.configure_logging(args.verbose, args.quiet)
+        if os.access(str(os.environ['HOME']) + "/.stov/stov.config", os.F_OK):
+            logger.debug(_("Migrating configuration from plain text to json."))
+            conf.migrate_config()
+        try:
+            logger.debug(
+                _("Comparing current and running configuration version."))
+            CHECK_RESULT = conf.check_config()
+        except stov_exceptions.ConfigFileReadErrorException as error:
+            logger.error(error)
+            sys.exit(1)
+        except stov_exceptions.InvalidConfigurationVersionException as error:
+            logger.error(error)
+            sys.exit(1)
+        else:
+            if not CHECK_RESULT:
+                logger.info(
+                    _("Your configuration needs to be updated, performing"
+                      " update now."))
+                try:
+                    conf.update_config()
+                except stov_exceptions.ConfigFileReadErrorException as error:
+                    logger.error(error)
+                    sys.exit(1)
+                except stov_exceptions.ConfigFileWriteErrorException as error:
+                    logger.error(error)
+                    sys.exit(1)
+    return conf

+ 3 - 57
stov

@@ -30,6 +30,7 @@ import os
 import smtplib
 import subprocess
 import socket
+import logging
 from email.mime.multipart import MIMEMultipart
 from email.mime.text import MIMEText
 
@@ -45,63 +46,8 @@ helpers.setup_sighandler()
 PARSER = helpers.parse_arguments()
 ARGUMENTS = PARSER.parse_args()
 helpers.create_lock()
-
-# Check if the configuration directory exists and is writeable. If it
-# doesnt, create it using the configuration class.
-
-if not os.access(os.environ['HOME'] + "/.stov", os.F_OK & os.W_OK):
-    print(_("This seems to be the first time you run the programm, do "
-            "you want to run the interactive assistant? (yes/no)"))
-    CONF = configuration.Conf()
-    LOGGER = CONF.configure_logging(ARGUMENTS.verbose, ARGUMENTS.quiet)
-    TEMP_INPUT = input()
-    if TEMP_INPUT == "yes":
-        CONF.assist()
-        try:
-            CONF.initialize()
-        except stov_exceptions.ConfigFileWriteErrorException as error:
-            LOGGER.error(error)
-        else:
-            LOGGER.info(_("Writing initial configuration according to your "
-                          "input, have fun!"))
-    else:
-        LOGGER.info(_("Writing initial configuration according to default"
-                      " values."))
-        LOGGER.debug(_("Creating hidden directory in home for configuration"
-                       " and database."))
-        try:
-            CONF.initialize()
-        except stov_exceptions.DirectoryCreationFailedException as error:
-            LOGGER.error(error)
-        except stov_exceptions.ConfigFileWriteErrorException as error:
-            LOGGER.error(error)
-else:
-    CONF = configuration.Conf()
-    LOGGER = CONF.configure_logging(ARGUMENTS.verbose, ARGUMENTS.quiet)
-    if os.access(str(os.environ['HOME']) + "/.stov/stov.config", os.F_OK):
-        LOGGER.debug(_("Migrating configuration from plain text to json."))
-        CONF.migrate_config()
-    try:
-        LOGGER.debug(_("Comparing current and running configuration version."))
-        CHECK_RESULT = CONF.check_config()
-    except stov_exceptions.ConfigFileReadErrorException as error:
-        LOGGER.error(error)
-        sys.exit(1)
-    except stov_exceptions.InvalidConfigurationVersionException as error:
-        LOGGER.error(error)
-        sys.exit(1)
-    else:
-        if not CHECK_RESULT:
-            LOGGER.info(_("Your configuration needs to be updated, performing"
-                          " update now."))
-            try:
-                CONF.update_config()
-            except stov_exceptions.ConfigFileReadErrorException as error:
-                LOGGER.error(error)
-                sys.exit(1)
-            except stov_exceptions.ConfigFileWriteErrorException as error:
-                LOGGER.error(error)
-                sys.exit(1)
+CONF = helpers.setup_configuration(ARGUMENTS)
+LOGGER = logging.getLogger("stov")
 
 
 # Create the initial connection to the database