40 lines
875 B
Python
40 lines
875 B
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 = []
|
|
OPTIONAL = {}
|
|
|
|
@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
|