Projekt

Obecné

Profil

Stáhnout (1.71 KB) Statistiky
| Větev: | Tag: | Revize:
1
import os
2

    
3
import configparser
4
from injector import singleton
5

    
6
from src.constants import DEFAULT_CONNECTION_STRING, TEST_DATABASE_FILE
7

    
8
DATABASE_SECTION = "Database"
9
DATABASE_CONNECTION_STRING = "ConnectionString"
10

    
11

    
12
class Configuration:
13
    """
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

    
26
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
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

    
51
        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

    
56
    binder.bind(Configuration, to=app_configuration, scope=singleton)
(2-2/3)