Plex-Bot-Music/PlexBot/__init__.py

42 lines
1.2 KiB
Python
Raw Normal View History

2020-07-27 06:08:00 +02:00
import logging
import sys
from pathlib import Path
from typing import Dict
import yaml
2020-07-23 06:21:41 +02:00
2020-07-27 06:08:00 +02:00
FORMAT = "%(asctime)s %(levelname)s: [%(filename)s:%(lineno)s - %(funcName)20s() ] %(message)s"
2020-07-23 06:21:41 +02:00
2020-07-27 06:08:00 +02:00
logging.basicConfig(format=FORMAT)
root_log = logging.getLogger()
plex_log = logging.getLogger("Plex")
bot_log = logging.getLogger("Bot")
2020-07-27 06:08:00 +02:00
def load_config(filename: str) -> Dict[str, str]:
# All config files should be in /config
# for docker deployment.
filename = Path("/config", filename)
try:
with open(filename, "r") as f:
config = yaml.safe_load(f)
except FileNotFoundError:
root_log.fatal("Configuration file not found.")
2020-07-27 06:08:00 +02:00
sys.exit(-1)
# Convert str level type to logging constant
levels = {
"DEBUG": logging.DEBUG,
"INFO": logging.INFO,
"WARNING": logging.WARNING,
"ERROR": logging.ERROR,
"CRITICAL": logging.CRITICAL,
}
config["root"]["log_level"] = levels[config["root"]["log_level"].upper()]
config["plex"]["log_level"] = levels[config["plex"]["log_level"].upper()]
config["discord"]["log_level"] = levels[config["discord"]["log_level"].upper()]
2020-07-23 06:21:41 +02:00
return config