Projekt

Obecné

Profil

Stáhnout (2.98 KB) Statistiky
| Větev: | Tag: | Revize:
1
import os
2

    
3
import configparser
4
import logging
5
from logging import handlers
6

    
7
from injector import singleton
8

    
9
from src.constants import DEFAULT_CONNECTION_STRING, TEST_DATABASE_FILE, DEFAULT_SERVER_BASE_URL, LOG_NAME, \
10
    LOG_DIR_LOCATION
11
from src.constants import LOG_FILE_LOCATION, LOG_FORMAT
12
from src.utils.logger import Logger
13

    
14
DATABASE_SECTION = "Database"
15
DATABASE_CONNECTION_STRING = "ConnectionString"
16

    
17
SERVER_SECTION = "Server"
18
SERVER_BASE_URL = "ServerBaseURL"
19

    
20

    
21
class Configuration:
22
    """
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
        self.base_server_url = DEFAULT_SERVER_BASE_URL
34

    
35

    
36
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
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
    # 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
    app_configuration = Configuration()
58
    # if configuration file is not specified use the default configuration
59
    if config_file is not None and os.path.exists(config_file):
60
        config = configparser.ConfigParser()
61
        config.read(config_file)
62

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

    
72
    binder.bind(Configuration, to=app_configuration, scope=singleton)
73

    
74

    
75
def configure_logging():
76
    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

    
89
    # TODO check is 'valid'
90
    log = logging.getLogger('werkzeug')
91
    log.disabled = True
(2-2/3)