Projekt

Obecné

Profil

Stáhnout (2.75 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ý
6 5678036c David Friesecký
from injector import singleton
7 aff74b5a Jan Pašek
8 5678036c David Friesecký
from src.constants import DEFAULT_CONNECTION_STRING, TEST_DATABASE_FILE, DEFAULT_SERVER_BASE_URL, \
9
    DEFAULT_LOG_LEVEL, DEFAULT_ROOT_DIR
10 c073a0fc Jan Pašek
11
DATABASE_SECTION = "Database"
12
DATABASE_CONNECTION_STRING = "ConnectionString"
13 aff74b5a Jan Pašek
14 a766e644 Jan Pašek
SERVER_SECTION = "Server"
15
SERVER_BASE_URL = "ServerBaseURL"
16 d78aa613 Jan Pašek
SERVER_LOG_LEVEL = "LogLevel"
17 5678036c David Friesecký
SERVER_ROOT_DIR = "RootDir"
18 a766e644 Jan Pašek
19 53b0c8bc Jan Pašek
LOG_LEVEL_MAPPING = {
20
    "DEBUG": logging.DEBUG,
21
    "INFO": logging.INFO,
22
    "WARNING": logging.WARNING,
23
    "ERROR": logging.ERROR,
24
    "CRITICAL": logging.CRITICAL
25
}
26
27 2f38462f Jan Pašek
28 aff74b5a Jan Pašek
class Configuration:
29 c073a0fc Jan Pašek
    """
30
    Configuration class servers for injecting current application
31
    configuration all over the application
32
    """
33
34
    def __init__(self):
35
        """
36
        Constructor
37
        It must initialize all variables to their default values
38
        """
39 688c63b7 Jan Pašek
        self.config_file = None
40 c073a0fc Jan Pašek
        self.connection_string = DEFAULT_CONNECTION_STRING
41 a766e644 Jan Pašek
        self.base_server_url = DEFAULT_SERVER_BASE_URL
42 d78aa613 Jan Pašek
        self.log_level = DEFAULT_LOG_LEVEL
43 5678036c David Friesecký
        self.root_dir = DEFAULT_ROOT_DIR
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 688c63b7 Jan Pašek
        app_configuration.config_file = config_file
69 c073a0fc Jan Pašek
        config = configparser.ConfigParser()
70
        config.read(config_file)
71 aff74b5a Jan Pašek
72 c073a0fc Jan Pašek
        if config[DATABASE_SECTION] is not None:
73
            database_config = config[DATABASE_SECTION]
74
            app_configuration.connection_string = database_config.get(DATABASE_CONNECTION_STRING,
75
                                                                      DEFAULT_CONNECTION_STRING)
76 a766e644 Jan Pašek
        if config[SERVER_SECTION] is not None:
77
            server_config = config[SERVER_SECTION]
78
            app_configuration.base_server_url = server_config.get(SERVER_BASE_URL,
79
                                                                  DEFAULT_SERVER_BASE_URL)
80 d78aa613 Jan Pašek
            app_configuration.log_level = server_config.get(SERVER_LOG_LEVEL, DEFAULT_LOG_LEVEL)
81 5678036c David Friesecký
            app_configuration.root_dir = server_config.get(SERVER_ROOT_DIR, DEFAULT_ROOT_DIR)
82 688c63b7 Jan Pašek
83 c073a0fc Jan Pašek
    binder.bind(Configuration, to=app_configuration, scope=singleton)