1 |
c073a0fc
|
Jan Pašek
|
import os
|
2 |
|
|
|
3 |
|
|
import configparser
|
4 |
dbca269d
|
David Friesecký
|
import logging
|
5 |
ed35ce72
|
David Friesecký
|
from logging import handlers
|
6 |
|
|
|
7 |
d78aa613
|
Jan Pašek
|
from injector import singleton, inject
|
8 |
aff74b5a
|
Jan Pašek
|
|
9 |
ed35ce72
|
David Friesecký
|
from src.constants import DEFAULT_CONNECTION_STRING, TEST_DATABASE_FILE, DEFAULT_SERVER_BASE_URL, LOG_NAME, \
|
10 |
07a6c869
|
David Friesecký
|
LOG_DIR, DEFAULT_LOG_LEVEL, LOG_FILENAME
|
11 |
1bdc90c0
|
David Friesecký
|
from src.constants import LOG_FILE_LOCATION, LOG_FORMAT
|
12 |
535ce081
|
David Friesecký
|
from src.utils.logger import Logger
|
13 |
c073a0fc
|
Jan Pašek
|
|
14 |
|
|
DATABASE_SECTION = "Database"
|
15 |
|
|
DATABASE_CONNECTION_STRING = "ConnectionString"
|
16 |
aff74b5a
|
Jan Pašek
|
|
17 |
a766e644
|
Jan Pašek
|
SERVER_SECTION = "Server"
|
18 |
|
|
SERVER_BASE_URL = "ServerBaseURL"
|
19 |
d78aa613
|
Jan Pašek
|
SERVER_LOG_LEVEL = "LogLevel"
|
20 |
a766e644
|
Jan Pašek
|
|
21 |
53b0c8bc
|
Jan Pašek
|
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 |
2f38462f
|
Jan Pašek
|
|
30 |
aff74b5a
|
Jan Pašek
|
class Configuration:
|
31 |
c073a0fc
|
Jan Pašek
|
"""
|
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 |
688c63b7
|
Jan Pašek
|
self.config_file = None
|
42 |
c073a0fc
|
Jan Pašek
|
self.connection_string = DEFAULT_CONNECTION_STRING
|
43 |
a766e644
|
Jan Pašek
|
self.base_server_url = DEFAULT_SERVER_BASE_URL
|
44 |
d78aa613
|
Jan Pašek
|
self.log_level = DEFAULT_LOG_LEVEL
|
45 |
c073a0fc
|
Jan Pašek
|
|
46 |
aff74b5a
|
Jan Pašek
|
|
47 |
d2b0ef43
|
Stanislav Král
|
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 |
c073a0fc
|
Jan Pašek
|
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 |
535ce081
|
David Friesecký
|
config_name = "X509_CONFIG"
|
65 |
|
|
config_file = os.environ.get(config_name)
|
66 |
c073a0fc
|
Jan Pašek
|
app_configuration = Configuration()
|
67 |
|
|
# if configuration file is not specified use the default configuration
|
68 |
a766e644
|
Jan Pašek
|
if config_file is not None and os.path.exists(config_file):
|
69 |
688c63b7
|
Jan Pašek
|
app_configuration.config_file = config_file
|
70 |
c073a0fc
|
Jan Pašek
|
config = configparser.ConfigParser()
|
71 |
|
|
config.read(config_file)
|
72 |
aff74b5a
|
Jan Pašek
|
|
73 |
c073a0fc
|
Jan Pašek
|
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 |
a766e644
|
Jan Pašek
|
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 |
d78aa613
|
Jan Pašek
|
app_configuration.log_level = server_config.get(SERVER_LOG_LEVEL, DEFAULT_LOG_LEVEL)
|
82 |
688c63b7
|
Jan Pašek
|
|
83 |
c073a0fc
|
Jan Pašek
|
binder.bind(Configuration, to=app_configuration, scope=singleton)
|
84 |
dbca269d
|
David Friesecký
|
|
85 |
|
|
|
86 |
d78aa613
|
Jan Pašek
|
def configure_logging(config: Configuration):
|
87 |
2d625493
|
Jan Pašek
|
if not os.path.exists(LOG_DIR):
|
88 |
|
|
os.makedirs(LOG_DIR)
|
89 |
ed35ce72
|
David Friesecký
|
|
90 |
|
|
handler = logging.handlers.TimedRotatingFileHandler(
|
91 |
07a6c869
|
David Friesecký
|
os.path.join(LOG_DIR, LOG_FILENAME),
|
92 |
ed35ce72
|
David Friesecký
|
when='H', interval=1)
|
93 |
|
|
formatter = logging.Formatter(LOG_FORMAT)
|
94 |
|
|
handler.setFormatter(formatter)
|
95 |
|
|
|
96 |
d78aa613
|
Jan Pašek
|
# set log level based on config file
|
97 |
ed35ce72
|
David Friesecký
|
app_logger = logging.getLogger(LOG_NAME)
|
98 |
53b0c8bc
|
Jan Pašek
|
app_logger.setLevel(LOG_LEVEL_MAPPING.get(config.log_level, logging.DEBUG))
|
99 |
d78aa613
|
Jan Pašek
|
|
100 |
ed35ce72
|
David Friesecký
|
app_logger.addHandler(handler)
|
101 |
5e31b492
|
David Friesecký
|
|
102 |
|
|
# TODO check is 'valid'
|
103 |
|
|
log = logging.getLogger('werkzeug')
|
104 |
|
|
log.disabled = True
|