#!/usr/bin/env python
# -*- coding: utf-8 -*-

import configparser
from configparser import NoOptionError, NoSectionError


def get_connection_string_from_config_file(cfg_src, section):
    """
    Gets connection parameters from specified section in
    a configuration file.
    """
    cfg_parser = configparser.ConfigParser()
    cfg_parser.read(cfg_src)

    try:
        db_engine = cfg_parser.get(section, 'db_engine')
        user = cfg_parser.get(section, 'user')
        password = cfg_parser.get(section, 'password')
        host = cfg_parser.get(section, 'host')
        port = cfg_parser.get(section, 'port')
        database = cfg_parser.get(section, 'database')
    except NoOptionError as e:
        print(e)
        return
    except NoSectionError as e:
        print(e)
        return
    except KeyError as e:
        print(
            "Unable to retrieve parameter '%s' "
            "from configuration file." % e.args[0])
        return

    conn_string = "{0}://{1}:{2}@{3}:{4}/{5}".format(
        db_engine, user, password, host, port, database)

    return conn_string

@@ -60,16 +60,75 @@
 gparser%0A
+import logging%0Aimport logging.handlers%0Aimport os%0Aimport re%0A
 from con
@@ -1145,12 +1145,2597 @@
 conn_string%0A
+%0A%0Aclass WhitespaceRemovingFormatter(logging.Formatter):%0A%0A    REGEX = re.compile(%22%5E%5C+?%5Cs%22)%0A%0A    def format(self, record):%0A        record.msg = record.msg.strip()%0A        record.msg = re.sub(self.REGEX, %22%22, record.msg)%0A        return super(WhitespaceRemovingFormatter, self).format(record)%0A%0A%0Adef prepare_logging(log_types=%5B'file', 'screen'%5D, logdir=''):%0A%0A    logging.getLogger(%22requests%22).setLevel(logging.WARNING)%0A%0A    if not logdir:%0A        logdir = os.path.join(os.getenv(%22TEMP%22), %22log%22)%0A%0A    if not os.path.isdir(logdir):%0A        os.makedirs(logdir)%0A%0A    handlers = list()%0A%0A    if 'screen' in log_types:%0A        stream_handler = logging.StreamHandler()%0A        stream_handler.setLevel(logging.INFO)%0A        stream_formatter = logging.Formatter(%22%25(message)s%22)%0A        stream_handler.setFormatter(stream_formatter)%0A        handlers.append(stream_handler)%0A%0A    if 'file' in log_types:%0A%0A        file_formatter = logging.Formatter(%0A            %22%25(asctime)s.%25(msecs)03d %25(thread)5d %22 +%0A            %22%25(name)-35s %25(levelname)-8s %25(message)s%22,%0A            datefmt=%22%25y-%25m-%25d %25H:%25M:%25S%22)%0A%0A        debug_log = os.path.join(logdir, 'pynhldb_debug.log')%0A        file_debug_handler = logging.handlers.TimedRotatingFileHandler(%0A            debug_log, when='midnight', interval=1)%0A        file_debug_handler.setFormatter(file_formatter)%0A        file_debug_handler.setLevel(logging.DEBUG)%0A        handlers.append(file_debug_handler)%0A%0A        info_log = os.path.join(logdir, 'pynhldb_info.log')%0A        file_info_handler = logging.handlers.TimedRotatingFileHandler(%0A            info_log, when='midnight', interval=1)%0A        file_info_handler.setFormatter(file_formatter)%0A        file_info_handler.setLevel(logging.INFO)%0A        handlers.append(file_info_handler)%0A%0A        warn_log = os.path.join(logdir, 'pynhldb_warn.log')%0A        file_warn_handler = logging.handlers.TimedRotatingFileHandler(%0A            warn_log, when='midnight', interval=1)%0A        file_warn_handler.setFormatter(file_formatter)%0A        file_warn_handler.setLevel(logging.WARN)%0A        handlers.append(file_warn_handler)%0A%0A        logging.Formatter.formatTime%0A%0A        error_log = os.path.join(logdir, 'pynhldb_error.log')%0A        file_error_handler = logging.handlers.TimedRotatingFileHandler(%0A            error_log, when='midnight', interval=1)%0A        file_error_handler.setFormatter(file_formatter)%0A        file_error_handler.setLevel(logging.ERROR)%0A        handlers.append(file_error_handler)%0A%0A    logger = logging.getLogger()%0A    logger.setLevel(logging.DEBUG)%0A    for handler in handlers:%0A        logger.addHandler(handler)%0A

Add logging preparation