Browse Source

move database setup to helpers

Helmut Pozimski 6 years ago
parent
commit
a3c76ce891
2 changed files with 54 additions and 43 deletions
  1. 53 0
      lib_stov/helpers.py
  2. 1 43
      stov

+ 53 - 0
lib_stov/helpers.py

@@ -27,6 +27,7 @@ import logging
 
 from lib_stov import stov_exceptions
 from lib_stov import configuration
+from lib_stov import database
 
 LOGGER = logging.getLogger("stov")
 
@@ -253,3 +254,55 @@ def setup_configuration(args):
                     logger.error(error)
                     sys.exit(1)
     return conf
+
+
+def setup_database(conf):
+    """ Sets up the database and provides a DB object to talk to the database
+    in the application.
+
+    :param conf: configuration object
+    :type conf: lib_stov.configuration.Conf
+    :return: database object
+    :rtype: lib_stov.database.Db
+    """
+    if os.access(conf.dbpath, os.F_OK):
+        try:
+            db = database.Db(path=conf.dbpath,
+                             version=conf.values["db_version"])
+        except stov_exceptions.DBConnectionFailedException as error:
+            LOGGER.error(error)
+            sys.exit(1)
+    else:
+        try:
+            db = database.Db(path=conf.dbpath,
+                             version=conf.values["db_version"])
+        except stov_exceptions.DBConnectionFailedException as error:
+            LOGGER.error(error)
+            sys.exit(1)
+        else:
+            try:
+                db.populate()
+            except stov_exceptions.DBWriteAccessFailedException as error:
+                LOGGER.error(error)
+                sys.exit(1)
+            else:
+                LOGGER.debug(_("Created initial database tables."))
+
+    try:
+        conf = configuration.Conf()
+        LOGGER.debug(_("Comparing current and running database version."))
+        if not conf.check_db():
+            LOGGER.info(
+                _("Your database needs to be updated, performing update "
+                  "now."))
+            db.update()
+            conf.values["db_version"] = db.get_version()
+            LOGGER.debug("Opening configuration file.")
+            try:
+                conf.write_config()
+            except stov_exceptions.ConfigFileWriteErrorException as error:
+                LOGGER.error(error)
+    except stov_exceptions.DBWriteAccessFailedException as error:
+        LOGGER.error(error)
+        sys.exit(1)
+    return db

+ 1 - 43
stov

@@ -35,9 +35,7 @@ from email.mime.multipart import MIMEMultipart
 from email.mime.text import MIMEText
 
 from lib_stov import subscription
-from lib_stov import configuration
 from lib_stov import stov_exceptions
-from lib_stov import database
 from lib_stov import helpers
 
 
@@ -48,47 +46,7 @@ ARGUMENTS = PARSER.parse_args()
 helpers.create_lock()
 CONF = helpers.setup_configuration(ARGUMENTS)
 LOGGER = logging.getLogger("stov")
-
-
-# Create the initial connection to the database
-
-if os.access(CONF.dbpath, os.F_OK):
-    try:
-        DB = database.Db(path=CONF.dbpath, version=CONF.values["db_version"])
-    except stov_exceptions.DBConnectionFailedException as error:
-        LOGGER.error(error)
-        sys.exit(1)
-else:
-    try:
-        DB = database.Db(path=CONF.dbpath, version=CONF.values["db_version"])
-    except stov_exceptions.DBConnectionFailedException as error:
-        LOGGER.error(error)
-        sys.exit(1)
-    else:
-        try:
-            DB.populate()
-        except stov_exceptions.DBWriteAccessFailedException as error:
-            LOGGER.error(error)
-            sys.exit(1)
-        else:
-            LOGGER.debug(_("Created initial database tables."))
-
-try:
-    CONF = configuration.Conf()
-    LOGGER.debug(_("Comparing current and running database version."))
-    if not CONF.check_db():
-        LOGGER.info(_("Your database needs to be updated, performing update "
-                      "now."))
-        DB.update()
-        CONF.values["db_version"] = DB.get_version()
-        LOGGER.debug("Opening configuration file.")
-        try:
-            CONF.write_config()
-        except stov_exceptions.ConfigFileWriteErrorException as error:
-            LOGGER.error(error)
-except stov_exceptions.DBWriteAccessFailedException as error:
-    LOGGER.error(error)
-    sys.exit(1)
+DB = helpers.setup_database(CONF)
 
 # youtube-dl is really a dependency but the program will run with limited
 # functionality without it so we need to check that here