Projekt

Obecné

Profil

Stáhnout (3.33 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, inject
8

    
9
from src.constants import DEFAULT_CONNECTION_STRING, TEST_DATABASE_FILE, DEFAULT_SERVER_BASE_URL, LOG_NAME, \
10
    LOG_DIR_LOCATION, DEFAULT_LOG_LEVEL
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
SERVER_LOG_LEVEL = "LogLevel"
20

    
21
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

    
30
class Configuration:
31
    """
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
        self.base_server_url = DEFAULT_SERVER_BASE_URL
43
        self.log_level = DEFAULT_LOG_LEVEL
44

    
45

    
46
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
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
    config_name = "X509_CONFIG"
64
    config_file = os.environ.get(config_name)
65
    app_configuration = Configuration()
66
    # if configuration file is not specified use the default configuration
67
    if config_file is not None and os.path.exists(config_file):
68
        config = configparser.ConfigParser()
69
        config.read(config_file)
70

    
71
        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
        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
            app_configuration.log_level = server_config.get(SERVER_LOG_LEVEL, DEFAULT_LOG_LEVEL)
80
    binder.bind(Configuration, to=app_configuration, scope=singleton)
81

    
82

    
83
def configure_logging(config: Configuration):
84
    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
    # set log level based on config file
94
    app_logger = logging.getLogger(LOG_NAME)
95
    app_logger.setLevel(LOG_LEVEL_MAPPING.get(config.log_level, logging.DEBUG))
96

    
97
    app_logger.addHandler(handler)
98

    
99
    # TODO check is 'valid'
100
    log = logging.getLogger('werkzeug')
101
    log.disabled = True
(2-2/3)