Projekt

Obecné

Profil

Stáhnout (1.71 KB) Statistiky
| Větev: | Tag: | Revize:
1 c073a0fc Jan Pašek
import os
2
3
import configparser
4 aff74b5a Jan Pašek
from injector import singleton
5
6 d2b0ef43 Stanislav Král
from src.constants import DEFAULT_CONNECTION_STRING, TEST_DATABASE_FILE
7 c073a0fc Jan Pašek
8
DATABASE_SECTION = "Database"
9
DATABASE_CONNECTION_STRING = "ConnectionString"
10 aff74b5a Jan Pašek
11 2f38462f Jan Pašek
12 aff74b5a Jan Pašek
class Configuration:
13 c073a0fc Jan Pašek
    """
14
    Configuration class servers for injecting current application
15
    configuration all over the application
16
    """
17
18
    def __init__(self):
19
        """
20
        Constructor
21
        It must initialize all variables to their default values
22
        """
23
        self.connection_string = DEFAULT_CONNECTION_STRING
24
25 aff74b5a Jan Pašek
26 d2b0ef43 Stanislav Král
def test_configuration():
27
    conf = Configuration()
28
    conf.connection_string = TEST_DATABASE_FILE
29
    return conf
30
31
32
def test_configuration_binder(binder):
33
    binder.bind(Configuration, to=test_configuration(), scope=singleton)
34
35
36 c073a0fc Jan Pašek
def configure_env_variable(binder):
37
    """
38
    Load configuration file stored in X509_CONFIG environment variable.
39
    If the file is not specified, use the default configuration
40
    :param binder: injector configuration binder instance
41
    :return: N/A
42
    """
43
    # TODO log which configuration is going to be used
44
    config_file = os.environ.get("X509_CONFIG")
45
    app_configuration = Configuration()
46
    # if configuration file is not specified use the default configuration
47
    if config_file is not None:
48
        config = configparser.ConfigParser()
49
        config.read(config_file)
50 aff74b5a Jan Pašek
51 c073a0fc Jan Pašek
        if config[DATABASE_SECTION] is not None:
52
            database_config = config[DATABASE_SECTION]
53
            app_configuration.connection_string = database_config.get(DATABASE_CONNECTION_STRING,
54
                                                                      DEFAULT_CONNECTION_STRING)
55 aff74b5a Jan Pašek
56 c073a0fc Jan Pašek
    binder.bind(Configuration, to=app_configuration, scope=singleton)