|
1 |
import os
|
|
2 |
|
|
3 |
import configparser
|
1 |
4 |
from injector import singleton
|
2 |
5 |
|
|
6 |
from src.constants import DEFAULT_CONNECTION_STRING
|
|
7 |
|
|
8 |
DATABASE_SECTION = "Database"
|
|
9 |
DATABASE_CONNECTION_STRING = "ConnectionString"
|
3 |
10 |
|
4 |
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 |
|
5 |
24 |
|
6 |
|
def __init__(self, connection_string):
|
7 |
|
self.connection_string = connection_string
|
|
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)
|
8 |
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)
|
9 |
44 |
|
10 |
|
def configure_default(binder):
|
11 |
|
configuration = Configuration("db/database_sqlite.db")
|
12 |
|
binder.bind(Configuration, to=configuration, scope=singleton)
|
|
45 |
binder.bind(Configuration, to=app_configuration, scope=singleton)
|
Re #8569 Added support for handling configuration files