Projekt

Obecné

Profil

Stáhnout (2.44 KB) Statistiky
| Větev: | Tag: | Revize:
1 c073a0fc Jan Pašek
import os
2
3
import configparser
4 dbca269d David Friesecký
import logging
5 aff74b5a Jan Pašek
from injector import singleton
6
7 a766e644 Jan Pašek
from src.constants import DEFAULT_CONNECTION_STRING, TEST_DATABASE_FILE, DEFAULT_SERVER_BASE_URL
8 c073a0fc Jan Pašek
9
DATABASE_SECTION = "Database"
10
DATABASE_CONNECTION_STRING = "ConnectionString"
11 aff74b5a Jan Pašek
12 a766e644 Jan Pašek
SERVER_SECTION = "Server"
13
SERVER_BASE_URL = "ServerBaseURL"
14
15 dbca269d David Friesecký
LOG_FILE = "application.log"
16
LOG_FORMAT = "%(levelname)-8s %(asctime)s - %(message)s"
17
18 2f38462f Jan Pašek
19 aff74b5a Jan Pašek
class Configuration:
20 c073a0fc Jan Pašek
    """
21
    Configuration class servers for injecting current application
22
    configuration all over the application
23
    """
24
25
    def __init__(self):
26
        """
27
        Constructor
28
        It must initialize all variables to their default values
29
        """
30
        self.connection_string = DEFAULT_CONNECTION_STRING
31 a766e644 Jan Pašek
        self.base_server_url = DEFAULT_SERVER_BASE_URL
32 c073a0fc Jan Pašek
33 aff74b5a Jan Pašek
34 d2b0ef43 Stanislav Král
def test_configuration():
35
    conf = Configuration()
36
    conf.connection_string = TEST_DATABASE_FILE
37
    return conf
38
39
40
def test_configuration_binder(binder):
41
    binder.bind(Configuration, to=test_configuration(), scope=singleton)
42
43
44 c073a0fc Jan Pašek
def configure_env_variable(binder):
45
    """
46
    Load configuration file stored in X509_CONFIG environment variable.
47
    If the file is not specified, use the default configuration
48
    :param binder: injector configuration binder instance
49
    :return: N/A
50
    """
51
    # TODO log which configuration is going to be used
52
    config_file = os.environ.get("X509_CONFIG")
53
    app_configuration = Configuration()
54
    # if configuration file is not specified use the default configuration
55 a766e644 Jan Pašek
    if config_file is not None and os.path.exists(config_file):
56 c073a0fc Jan Pašek
        config = configparser.ConfigParser()
57
        config.read(config_file)
58 aff74b5a Jan Pašek
59 c073a0fc Jan Pašek
        if config[DATABASE_SECTION] is not None:
60
            database_config = config[DATABASE_SECTION]
61
            app_configuration.connection_string = database_config.get(DATABASE_CONNECTION_STRING,
62
                                                                      DEFAULT_CONNECTION_STRING)
63 a766e644 Jan Pašek
        if config[SERVER_SECTION] is not None:
64
            server_config = config[SERVER_SECTION]
65
            app_configuration.base_server_url = server_config.get(SERVER_BASE_URL,
66
                                                                  DEFAULT_SERVER_BASE_URL)
67 aff74b5a Jan Pašek
68 c073a0fc Jan Pašek
    binder.bind(Configuration, to=app_configuration, scope=singleton)
69 dbca269d David Friesecký
70
71
def configure_logging(self):
72
    logging.basicConfig(filename=LOG_FILE,
73
                        filemode='a+',
74
                        format=LOG_FORMAT,
75
                        level=logging.DEBUG)