Projekt

Obecné

Profil

Stáhnout (3.45 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

    
22
class Configuration:
23
    """
24
    Configuration class servers for injecting current application
25
    configuration all over the application
26
    """
27

    
28
    def __init__(self):
29
        """
30
        Constructor
31
        It must initialize all variables to their default values
32
        """
33
        self.connection_string = DEFAULT_CONNECTION_STRING
34
        self.base_server_url = DEFAULT_SERVER_BASE_URL
35
        self.log_level = DEFAULT_LOG_LEVEL
36

    
37

    
38
def test_configuration():
39
    conf = Configuration()
40
    conf.connection_string = TEST_DATABASE_FILE
41
    return conf
42

    
43

    
44
def test_configuration_binder(binder):
45
    binder.bind(Configuration, to=test_configuration(), scope=singleton)
46

    
47

    
48
def configure_env_variable(binder):
49
    """
50
    Load configuration file stored in X509_CONFIG environment variable.
51
    If the file is not specified, use the default configuration
52
    :param binder: injector configuration binder instance
53
    :return: N/A
54
    """
55
    config_name = "X509_CONFIG"
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
            app_configuration.log_level = server_config.get(SERVER_LOG_LEVEL, DEFAULT_LOG_LEVEL)
72
    binder.bind(Configuration, to=app_configuration, scope=singleton)
73

    
74

    
75
def configure_logging(config: Configuration):
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
    # set log level based on config file
86
    app_logger = logging.getLogger(LOG_NAME)
87
    if config.log_level == "DEBUG":
88
        app_logger.setLevel(logging.DEBUG)
89
    elif config.log_level == "INFO":
90
        app_logger.setLevel(logging.INFO)
91
    elif config.log_level == "WARNING":
92
        app_logger.setLevel(logging.WARNING)
93
    elif config.log_level == "ERROR":
94
        app_logger.setLevel(logging.ERROR)
95
    else:
96
        app_logger.setLevel(logging.DEBUG)#
97

    
98
    app_logger.addHandler(handler)
99

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