jarvis-core/jarvis_core/config.py

48 lines
1.1 KiB
Python

"""Load global config."""
from pathlib import Path
from typing import Union
from yaml import load
from jarvis_core.util import Singleton
try:
from yaml import CLoader as Loader
except ImportError:
from yaml import Loader
DEFAULT_PATH = Path("config.yaml")
class Config(Singleton):
REQUIRED = ["token", "client_id", "mongo", "urls"]
OPTIONAL = {
"sync": False,
"log_level": "WARNING",
"scales": None,
"events": True,
"gitlab_token": None,
"max_messages": 1000,
"twitter": None,
}
@classmethod
def from_yaml(cls, filepath: Union[Path, str] = DEFAULT_PATH) -> "Config":
"""Load the yaml config file."""
if inst := cls.__dict__.get("inst"):
return inst
if isinstance(filepath, str):
filepath = Path(filepath)
with filepath.open() as f:
raw = f.read()
y = load(raw, Loader=Loader)
return cls(**y)
@classmethod
def reload(cls) -> bool:
"""Reload the config."""
return cls.__dict__.pop("inst", None) is None