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