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
|
class Configuration:
|
12
|
"""
|
13
|
Configuration class servers for injecting current application
|
14
|
configuration all over the application
|
15
|
"""
|
16
|
|
17
|
def __init__(self):
|
18
|
"""
|
19
|
Constructor
|
20
|
It must initialize all variables to their default values
|
21
|
"""
|
22
|
self.connection_string = DEFAULT_CONNECTION_STRING
|
23
|
|
24
|
|
25
|
def configure_env_variable(binder):
|
26
|
"""
|
27
|
Load configuration file stored in X509_CONFIG environment variable.
|
28
|
If the file is not specified, use the default configuration
|
29
|
:param binder: injector configuration binder instance
|
30
|
:return: N/A
|
31
|
"""
|
32
|
# TODO log which configuration is going to be used
|
33
|
config_file = os.environ.get("X509_CONFIG")
|
34
|
app_configuration = Configuration()
|
35
|
# if configuration file is not specified use the default configuration
|
36
|
if config_file is not None:
|
37
|
config = configparser.ConfigParser()
|
38
|
config.read(config_file)
|
39
|
|
40
|
if config[DATABASE_SECTION] is not None:
|
41
|
database_config = config[DATABASE_SECTION]
|
42
|
app_configuration.connection_string = database_config.get(DATABASE_CONNECTION_STRING,
|
43
|
DEFAULT_CONNECTION_STRING)
|
44
|
|
45
|
binder.bind(Configuration, to=app_configuration, scope=singleton)
|