2020-08-10 10:26:38 +02:00
|
|
|
"""
|
|
|
|
Plex music bot for discord.
|
2020-08-09 22:47:00 +02:00
|
|
|
|
2020-08-10 10:26:38 +02:00
|
|
|
Do not import this module, it is intended to be
|
|
|
|
used exclusively within a docker environment.
|
2020-08-09 22:47:00 +02:00
|
|
|
"""
|
2020-07-27 06:08:00 +02:00
|
|
|
import logging
|
|
|
|
import sys
|
2020-08-04 11:59:48 +02:00
|
|
|
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)
|
2020-08-09 08:28:14 +02:00
|
|
|
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]:
|
2020-08-09 22:47:00 +02:00
|
|
|
"""Loads config from yaml file
|
|
|
|
|
|
|
|
Grabs key/value config pairs from a file.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
filename: str path to yaml file.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
Dict[str, str] Values from config file.
|
2020-07-27 06:08:00 +02:00
|
|
|
|
2020-08-09 22:47:00 +02:00
|
|
|
Raises:
|
|
|
|
FileNotFound Configuration file not found.
|
|
|
|
"""
|
2020-07-27 06:08:00 +02:00
|
|
|
# All config files should be in /config
|
|
|
|
# for docker deployment.
|
|
|
|
filename = Path("/config", filename)
|
|
|
|
try:
|
2020-08-09 22:47:00 +02:00
|
|
|
with open(filename, "r") as config_file:
|
|
|
|
config = yaml.safe_load(config_file)
|
2020-07-27 06:08:00 +02:00
|
|
|
except FileNotFoundError:
|
2020-08-09 08:28:14 +02:00
|
|
|
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,
|
|
|
|
}
|
2020-08-09 08:28:14 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
2022-02-13 12:02:15 +01:00
|
|
|
if config["lyrics"] and config["lyrics"]["token"].lower() == "none":
|
|
|
|
config["lyrics"] = None
|
2020-09-06 23:32:40 +02:00
|
|
|
|
2020-07-23 06:21:41 +02:00
|
|
|
return config
|