forked from Zentro/RoRServerBot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.py
More file actions
85 lines (71 loc) · 2.7 KB
/
config.py
File metadata and controls
85 lines (71 loc) · 2.7 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
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
import yaml
import logging
class Config:
_config = None
@staticmethod
def load_config(file_path="config.yaml"):
"""
Load the YAML configuration file into memory.
Args:
file_path(str): The path to the YAML configuration file.
Defaults to 'config.yaml'.
Raises:
FileNotFoundError: If the configuration file is not found at the
given path.
yaml.YAMLError: If there is an error in parsing the YAML file.
"""
with open(file_path, "r") as f:
Config._config = yaml.safe_load(f)
@staticmethod
def get_config():
"""
Retrieve the loaded configuration data.
Returns:
dict: The loaded configuration as a dictionary.
Raises:
RuntimeError: If the configuration has not been loaded yet.
"""
if Config._config is None:
raise RuntimeError("")
return Config._config
@staticmethod
def get_logging_level():
"""
Convert the logging level from the configuration file to a `logging`
module constant.
Returns:
int: Corresponding logging level
(ex., `logging.INFO`, `logging.ERROR`).
Raises:
KeyError: If the logging level is not defined in the configuration.
ValueError: If the logging level in the configuration is invalid.
"""
if Config._config is None:
raise RuntimeError(
"Configuration not loaded. Call `load_config` first.")
log_level_str = Config._config.get("logging", {}).get("level", "info")
log_levels = {
"debug": logging.DEBUG,
"info": logging.INFO,
"warning": logging.WARNING,
"error": logging.ERROR,
"critical": logging.CRITICAL
}
if log_level_str not in log_levels:
raise ValueError(f"Invalid logging level: {log_level_str}")
return log_levels[log_level_str]