Projekt

Obecné

Profil

Stáhnout (1.46 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 c073a0fc Jan Pašek
from src.constants import DEFAULT_CONNECTION_STRING
7
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 c073a0fc Jan Pašek
def configure_env_variable(binder):
27
    """
28
    Load configuration file stored in X509_CONFIG environment variable.
29
    If the file is not specified, use the default configuration
30
    :param binder: injector configuration binder instance
31
    :return: N/A
32
    """
33
    # TODO log which configuration is going to be used
34
    config_file = os.environ.get("X509_CONFIG")
35
    app_configuration = Configuration()
36
    # if configuration file is not specified use the default configuration
37
    if config_file is not None:
38
        config = configparser.ConfigParser()
39
        config.read(config_file)
40 aff74b5a Jan Pašek
41 c073a0fc Jan Pašek
        if config[DATABASE_SECTION] is not None:
42
            database_config = config[DATABASE_SECTION]
43
            app_configuration.connection_string = database_config.get(DATABASE_CONNECTION_STRING,
44
                                                                      DEFAULT_CONNECTION_STRING)
45 aff74b5a Jan Pašek
46 c073a0fc Jan Pašek
    binder.bind(Configuration, to=app_configuration, scope=singleton)