Projekt

Obecné

Profil

Stáhnout (2.98 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 aff74b5a Jan Pašek
from injector import singleton
8
9 ed35ce72 David Friesecký
from src.constants import DEFAULT_CONNECTION_STRING, TEST_DATABASE_FILE, DEFAULT_SERVER_BASE_URL, LOG_NAME, \
10
    LOG_DIR_LOCATION
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
20 2f38462f Jan Pašek
21 aff74b5a Jan Pašek
class Configuration:
22 c073a0fc Jan Pašek
    """
23
    Configuration class servers for injecting current application
24
    configuration all over the application
25
    """
26
27
    def __init__(self):
28
        """
29
        Constructor
30
        It must initialize all variables to their default values
31
        """
32
        self.connection_string = DEFAULT_CONNECTION_STRING
33 a766e644 Jan Pašek
        self.base_server_url = DEFAULT_SERVER_BASE_URL
34 c073a0fc Jan Pašek
35 aff74b5a Jan Pašek
36 d2b0ef43 Stanislav Král
def test_configuration():
37
    conf = Configuration()
38
    conf.connection_string = TEST_DATABASE_FILE
39
    return conf
40
41
42
def test_configuration_binder(binder):
43
    binder.bind(Configuration, to=test_configuration(), scope=singleton)
44
45
46 c073a0fc Jan Pašek
def configure_env_variable(binder):
47
    """
48
    Load configuration file stored in X509_CONFIG environment variable.
49
    If the file is not specified, use the default configuration
50
    :param binder: injector configuration binder instance
51
    :return: N/A
52
    """
53 535ce081 David Friesecký
    # TODO check: log which configuration is going to be used
54
    config_name = "X509_CONFIG"
55
    Logger.info(f"Using configuration '{config_name}'")
56
    config_file = os.environ.get(config_name)
57 c073a0fc Jan Pašek
    app_configuration = Configuration()
58
    # if configuration file is not specified use the default configuration
59 a766e644 Jan Pašek
    if config_file is not None and os.path.exists(config_file):
60 c073a0fc Jan Pašek
        config = configparser.ConfigParser()
61
        config.read(config_file)
62 aff74b5a Jan Pašek
63 c073a0fc Jan Pašek
        if config[DATABASE_SECTION] is not None:
64
            database_config = config[DATABASE_SECTION]
65
            app_configuration.connection_string = database_config.get(DATABASE_CONNECTION_STRING,
66
                                                                      DEFAULT_CONNECTION_STRING)
67 a766e644 Jan Pašek
        if config[SERVER_SECTION] is not None:
68
            server_config = config[SERVER_SECTION]
69
            app_configuration.base_server_url = server_config.get(SERVER_BASE_URL,
70
                                                                  DEFAULT_SERVER_BASE_URL)
71 aff74b5a Jan Pašek
72 c073a0fc Jan Pašek
    binder.bind(Configuration, to=app_configuration, scope=singleton)
73 dbca269d David Friesecký
74
75 1bdc90c0 David Friesecký
def configure_logging():
76 ed35ce72 David Friesecký
    if not os.path.exists(LOG_DIR_LOCATION.shortest_relative_path()):
77
        os.makedirs(LOG_DIR_LOCATION.shortest_relative_path())
78
79
    handler = logging.handlers.TimedRotatingFileHandler(
80
        LOG_FILE_LOCATION.shortest_relative_path(),
81
        when='H', interval=1)
82
    formatter = logging.Formatter(LOG_FORMAT)
83
    handler.setFormatter(formatter)
84
85
    app_logger = logging.getLogger(LOG_NAME)
86
    app_logger.setLevel(logging.DEBUG)
87
    app_logger.addHandler(handler)
88 5e31b492 David Friesecký
89
    # TODO check is 'valid'
90
    log = logging.getLogger('werkzeug')
91
    log.disabled = True