1
|
import os
|
2
|
|
3
|
import configparser
|
4
|
import logging
|
5
|
from logging import handlers
|
6
|
|
7
|
from injector import singleton, inject
|
8
|
|
9
|
from src.constants import DEFAULT_CONNECTION_STRING, TEST_DATABASE_FILE, DEFAULT_SERVER_BASE_URL, LOG_NAME, \
|
10
|
LOG_DIR, DEFAULT_LOG_LEVEL, LOG_FILENAME
|
11
|
from src.constants import LOG_FILE_LOCATION, LOG_FORMAT
|
12
|
from src.utils.logger import Logger
|
13
|
|
14
|
DATABASE_SECTION = "Database"
|
15
|
DATABASE_CONNECTION_STRING = "ConnectionString"
|
16
|
|
17
|
SERVER_SECTION = "Server"
|
18
|
SERVER_BASE_URL = "ServerBaseURL"
|
19
|
SERVER_LOG_LEVEL = "LogLevel"
|
20
|
|
21
|
LOG_LEVEL_MAPPING = {
|
22
|
"DEBUG": logging.DEBUG,
|
23
|
"INFO": logging.INFO,
|
24
|
"WARNING": logging.WARNING,
|
25
|
"ERROR": logging.ERROR,
|
26
|
"CRITICAL": logging.CRITICAL
|
27
|
}
|
28
|
|
29
|
|
30
|
class Configuration:
|
31
|
"""
|
32
|
Configuration class servers for injecting current application
|
33
|
configuration all over the application
|
34
|
"""
|
35
|
|
36
|
def __init__(self):
|
37
|
"""
|
38
|
Constructor
|
39
|
It must initialize all variables to their default values
|
40
|
"""
|
41
|
self.config_file = None
|
42
|
self.connection_string = DEFAULT_CONNECTION_STRING
|
43
|
self.base_server_url = DEFAULT_SERVER_BASE_URL
|
44
|
self.log_level = DEFAULT_LOG_LEVEL
|
45
|
|
46
|
|
47
|
def test_configuration():
|
48
|
conf = Configuration()
|
49
|
conf.connection_string = TEST_DATABASE_FILE
|
50
|
return conf
|
51
|
|
52
|
|
53
|
def test_configuration_binder(binder):
|
54
|
binder.bind(Configuration, to=test_configuration(), scope=singleton)
|
55
|
|
56
|
|
57
|
def configure_env_variable(binder):
|
58
|
"""
|
59
|
Load configuration file stored in X509_CONFIG environment variable.
|
60
|
If the file is not specified, use the default configuration
|
61
|
:param binder: injector configuration binder instance
|
62
|
:return: N/A
|
63
|
"""
|
64
|
config_name = "X509_CONFIG"
|
65
|
config_file = os.environ.get(config_name)
|
66
|
app_configuration = Configuration()
|
67
|
# if configuration file is not specified use the default configuration
|
68
|
if config_file is not None and os.path.exists(config_file):
|
69
|
app_configuration.config_file = config_file
|
70
|
config = configparser.ConfigParser()
|
71
|
config.read(config_file)
|
72
|
|
73
|
if config[DATABASE_SECTION] is not None:
|
74
|
database_config = config[DATABASE_SECTION]
|
75
|
app_configuration.connection_string = database_config.get(DATABASE_CONNECTION_STRING,
|
76
|
DEFAULT_CONNECTION_STRING)
|
77
|
if config[SERVER_SECTION] is not None:
|
78
|
server_config = config[SERVER_SECTION]
|
79
|
app_configuration.base_server_url = server_config.get(SERVER_BASE_URL,
|
80
|
DEFAULT_SERVER_BASE_URL)
|
81
|
app_configuration.log_level = server_config.get(SERVER_LOG_LEVEL, DEFAULT_LOG_LEVEL)
|
82
|
|
83
|
binder.bind(Configuration, to=app_configuration, scope=singleton)
|
84
|
|
85
|
|
86
|
def configure_logging(config: Configuration):
|
87
|
if not os.path.exists(LOG_DIR):
|
88
|
os.makedirs(LOG_DIR)
|
89
|
|
90
|
handler = logging.handlers.TimedRotatingFileHandler(
|
91
|
os.path.join(LOG_DIR, LOG_FILENAME),
|
92
|
when='H', interval=1)
|
93
|
formatter = logging.Formatter(LOG_FORMAT)
|
94
|
handler.setFormatter(formatter)
|
95
|
|
96
|
# set log level based on config file
|
97
|
app_logger = logging.getLogger(LOG_NAME)
|
98
|
app_logger.setLevel(LOG_LEVEL_MAPPING.get(config.log_level, logging.DEBUG))
|
99
|
|
100
|
app_logger.addHandler(handler)
|
101
|
|
102
|
# TODO check is 'valid'
|
103
|
log = logging.getLogger('werkzeug')
|
104
|
log.disabled = True
|