Projekt

Obecné

Profil

Stáhnout (3.33 KB) Statistiky
| Větev: | Tag: | Revize:
1 c073a0fc Jan Pašek
import os
2
3
import configparser
4 dbca269d David Friesecký
import logging
5 ed35ce72 David Friesecký
from logging import handlers
6
7 d78aa613 Jan Pašek
from injector import singleton, inject
8 aff74b5a Jan Pašek
9 ed35ce72 David Friesecký
from src.constants import DEFAULT_CONNECTION_STRING, TEST_DATABASE_FILE, DEFAULT_SERVER_BASE_URL, LOG_NAME, \
10 d78aa613 Jan Pašek
    LOG_DIR_LOCATION, DEFAULT_LOG_LEVEL
11 1bdc90c0 David Friesecký
from src.constants import LOG_FILE_LOCATION, LOG_FORMAT
12 535ce081 David Friesecký
from src.utils.logger import Logger
13 c073a0fc Jan Pašek
14
DATABASE_SECTION = "Database"
15
DATABASE_CONNECTION_STRING = "ConnectionString"
16 aff74b5a Jan Pašek
17 a766e644 Jan Pašek
SERVER_SECTION = "Server"
18
SERVER_BASE_URL = "ServerBaseURL"
19 d78aa613 Jan Pašek
SERVER_LOG_LEVEL = "LogLevel"
20 a766e644 Jan Pašek
21 53b0c8bc Jan Pašek
LOG_LEVEL_MAPPING = {
22
    "DEBUG": logging.DEBUG,
23
    "INFO": logging.INFO,
24
    "WARNING": logging.WARNING,
25
    "ERROR": logging.ERROR,
26
    "CRITICAL": logging.CRITICAL
27
}
28
29 2f38462f Jan Pašek
30 aff74b5a Jan Pašek
class Configuration:
31 c073a0fc Jan Pašek
    """
32
    Configuration class servers for injecting current application
33
    configuration all over the application
34
    """
35
36
    def __init__(self):
37
        """
38
        Constructor
39
        It must initialize all variables to their default values
40
        """
41
        self.connection_string = DEFAULT_CONNECTION_STRING
42 a766e644 Jan Pašek
        self.base_server_url = DEFAULT_SERVER_BASE_URL
43 d78aa613 Jan Pašek
        self.log_level = DEFAULT_LOG_LEVEL
44 c073a0fc Jan Pašek
45 aff74b5a Jan Pašek
46 d2b0ef43 Stanislav Král
def test_configuration():
47
    conf = Configuration()
48
    conf.connection_string = TEST_DATABASE_FILE
49
    return conf
50
51
52
def test_configuration_binder(binder):
53
    binder.bind(Configuration, to=test_configuration(), scope=singleton)
54
55
56 c073a0fc Jan Pašek
def configure_env_variable(binder):
57
    """
58
    Load configuration file stored in X509_CONFIG environment variable.
59
    If the file is not specified, use the default configuration
60
    :param binder: injector configuration binder instance
61
    :return: N/A
62
    """
63 535ce081 David Friesecký
    config_name = "X509_CONFIG"
64
    config_file = os.environ.get(config_name)
65 c073a0fc Jan Pašek
    app_configuration = Configuration()
66
    # if configuration file is not specified use the default configuration
67 a766e644 Jan Pašek
    if config_file is not None and os.path.exists(config_file):
68 c073a0fc Jan Pašek
        config = configparser.ConfigParser()
69
        config.read(config_file)
70 aff74b5a Jan Pašek
71 c073a0fc Jan Pašek
        if config[DATABASE_SECTION] is not None:
72
            database_config = config[DATABASE_SECTION]
73
            app_configuration.connection_string = database_config.get(DATABASE_CONNECTION_STRING,
74
                                                                      DEFAULT_CONNECTION_STRING)
75 a766e644 Jan Pašek
        if config[SERVER_SECTION] is not None:
76
            server_config = config[SERVER_SECTION]
77
            app_configuration.base_server_url = server_config.get(SERVER_BASE_URL,
78
                                                                  DEFAULT_SERVER_BASE_URL)
79 d78aa613 Jan Pašek
            app_configuration.log_level = server_config.get(SERVER_LOG_LEVEL, DEFAULT_LOG_LEVEL)
80 c073a0fc Jan Pašek
    binder.bind(Configuration, to=app_configuration, scope=singleton)
81 dbca269d David Friesecký
82
83 d78aa613 Jan Pašek
def configure_logging(config: Configuration):
84 ed35ce72 David Friesecký
    if not os.path.exists(LOG_DIR_LOCATION.shortest_relative_path()):
85
        os.makedirs(LOG_DIR_LOCATION.shortest_relative_path())
86
87
    handler = logging.handlers.TimedRotatingFileHandler(
88
        LOG_FILE_LOCATION.shortest_relative_path(),
89
        when='H', interval=1)
90
    formatter = logging.Formatter(LOG_FORMAT)
91
    handler.setFormatter(formatter)
92
93 d78aa613 Jan Pašek
    # set log level based on config file
94 ed35ce72 David Friesecký
    app_logger = logging.getLogger(LOG_NAME)
95 53b0c8bc Jan Pašek
    app_logger.setLevel(LOG_LEVEL_MAPPING.get(config.log_level, logging.DEBUG))
96 d78aa613 Jan Pašek
97 ed35ce72 David Friesecký
    app_logger.addHandler(handler)
98 5e31b492 David Friesecký
99
    # TODO check is 'valid'
100
    log = logging.getLogger('werkzeug')
101
    log.disabled = True