This repository was archived by the owner on Feb 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_configuration.py
More file actions
89 lines (73 loc) · 3.25 KB
/
Copy path_configuration.py
File metadata and controls
89 lines (73 loc) · 3.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import logging
import logging.config
import os
import os.path
import sys
import yaml
logger = logging.getLogger(__name__)
def load(server_file_path: str, logging_loader=yaml.FullLoader) -> dict:
"""
Load logging and server YAML configurations according to SERVER_ENVIRONMENT environment variable.
:param server_file_path: Path to the server.py file (or any other file located in the python module directory).
:param logging_loader: yaml loader to use to process the logging configuration. Use yaml.FullLoader by default.
:return: server configuration as a dictionary.
"""
module_directory = os.path.abspath(os.path.dirname(server_file_path))
configuration_folder = os.path.join(module_directory, "..", "configuration")
load_logging_configuration(configuration_folder, logging_loader)
return load_configuration(configuration_folder)
def load_logging_configuration(configuration_folder: str, loader=yaml.FullLoader) -> str:
"""
Load logging configuration according to SERVER_ENVIRONMENT environment variable.
If file is not found, then logging will be performed as INFO into stdout.
Return loaded configuration file path. None if not loaded.
:param loader: yaml loader to use to process the logging configuration. Use yaml.FullLoader by default.
"""
file_path = os.path.join(configuration_folder, f"logging_{get_environment()}.yml")
return _load_logging_configuration(file_path, loader)
def get_environment():
"""Return current server environment."""
return os.environ.get("SERVER_ENVIRONMENT", "default")
def _load_logging_configuration(file_path: str, loader) -> str:
"""
Load YAML logging configuration file_path.
If file is not found, then logging will be performed as INFO into stdout.
"""
if os.path.isfile(file_path):
with open(file_path, "r") as config_file:
logging.config.dictConfig(yaml.load(config_file, loader))
logger.info(f"Logging configuration file ({file_path}) loaded.")
return file_path
else:
logging.basicConfig(
format="%(asctime)s - %(levelname)s - %(process)d:%(thread)d - %(filename)s:%(lineno)d - %(message)s",
handlers=[logging.StreamHandler(sys.stdout)],
level=logging.INFO,
)
logger.warning(
f"Logging configuration file ({file_path}) cannot be found. Using standard output."
)
def load_configuration(configuration_folder: str) -> dict:
"""
Load configuration according to SERVER_ENVIRONMENT environment variable.
Return a dictionary (empty if file cannot be found).
"""
file_path = os.path.join(
configuration_folder, f"configuration_{get_environment()}.yml"
)
return _load_configuration(file_path)
def _load_configuration(file_path: str) -> dict:
"""
Load YAML configuration file path.
Return a dictionary (empty if file cannot be found).
"""
if os.path.isfile(file_path):
with open(file_path, "r") as config_file:
conf = yaml.full_load(config_file)
logger.info(f"Loading configuration from {file_path}.")
return conf
else:
logger.warning(
f"Configuration file {file_path} cannot be found. Considering as empty."
)
return {}