Projekt

Obecné

Profil

Stáhnout (1.46 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
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 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

    
41
        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

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