Add .env loading
This commit is contained in:
parent
bb89107b93
commit
d8f5aff799
3 changed files with 779 additions and 319 deletions
|
@ -1,26 +1,53 @@
|
|||
"""Load global config."""
|
||||
from lib2to3.pgen2 import token
|
||||
import os
|
||||
from pathlib import Path
|
||||
from typing import Union
|
||||
|
||||
from dotenv import load_dotenv
|
||||
from yaml import load
|
||||
|
||||
from jarvis_core.util import Singleton
|
||||
from jarvis_core.util import Singleton, find_all
|
||||
|
||||
try:
|
||||
from yaml import CLoader as Loader
|
||||
except ImportError:
|
||||
from yaml import Loader
|
||||
|
||||
|
||||
DEFAULT_PATH = Path("config.yaml")
|
||||
DEFAULT_YAML_PATH = Path("config.yaml")
|
||||
DEFAULT_ENV_PATH = Path(".env")
|
||||
|
||||
|
||||
class Config(Singleton):
|
||||
REQUIRED = []
|
||||
OPTIONAL = {}
|
||||
ENV_REQUIRED = []
|
||||
ENV_OPTIONAL = {}
|
||||
|
||||
@classmethod
|
||||
def from_yaml(cls, filepath: Union[Path, str] = DEFAULT_PATH) -> "Config":
|
||||
def _process_env(cls, **kwargs) -> dict:
|
||||
"""Process environment variables into standard arguments"""
|
||||
|
||||
@classmethod
|
||||
def from_env(cls, filepath: Union[Path, str] = DEFAULT_ENV_PATH) -> "Config":
|
||||
"""Loag the environment config."""
|
||||
if inst := cls.__dict__.get("inst"):
|
||||
return inst
|
||||
|
||||
load_dotenv(filepath)
|
||||
|
||||
data = {}
|
||||
for item in cls.ENV_REQUIRED:
|
||||
data[item] = os.environ.get(item, None)
|
||||
for item, default in cls.ENV_OPTIONAL.items():
|
||||
data[item] = os.environ.get(item, default)
|
||||
|
||||
data = cls._process_env(**data)
|
||||
|
||||
return cls(**data)
|
||||
|
||||
@classmethod
|
||||
def from_yaml(cls, filepath: Union[Path, str] = DEFAULT_YAML_PATH) -> "Config":
|
||||
"""Load the yaml config file."""
|
||||
if inst := cls.__dict__.get("inst"):
|
||||
return inst
|
||||
|
@ -34,6 +61,13 @@ class Config(Singleton):
|
|||
y = load(raw, Loader=Loader)
|
||||
return cls(**y)
|
||||
|
||||
@classmethod
|
||||
def load(cls) -> "Config":
|
||||
if DEFAULT_ENV_PATH.exists():
|
||||
return cls.from_env()
|
||||
else:
|
||||
return cls.from_yaml()
|
||||
|
||||
@classmethod
|
||||
def reload(cls) -> bool:
|
||||
"""Reload the config."""
|
||||
|
|
1050
poetry.lock
generated
1050
poetry.lock
generated
File diff suppressed because it is too large
Load diff
|
@ -1,6 +1,6 @@
|
|||
[tool.poetry]
|
||||
name = "jarvis-core"
|
||||
version = "0.14.0"
|
||||
version = "0.15.0"
|
||||
description = "JARVIS core"
|
||||
authors = ["Zevaryx <zevaryx@gmail.com>"]
|
||||
|
||||
|
@ -14,9 +14,13 @@ pytz = "^2022.1"
|
|||
aiohttp = "^3.8.1"
|
||||
rich = "^12.3.0"
|
||||
nanoid = "^2.0.0"
|
||||
python-dotenv = "^0.21.0"
|
||||
|
||||
[tool.poetry.dev-dependencies]
|
||||
pytest = "^7.1"
|
||||
ipython = "^8.5.0"
|
||||
rich = "^12.6.0"
|
||||
black = {version = "^22.10.0", allow-prereleases = true}
|
||||
|
||||
[build-system]
|
||||
requires = ["poetry-core>=1.0.0"]
|
||||
|
|
Loading…
Add table
Reference in a new issue