mirror of
https://github.com/fusion44/blitz_api.git
synced 2026-08-17 12:26:11 +02:00
If the environment variable BAPI_ENV_PATH is set, the config system will try to read configs from the given path instead of .env in pwd If no file is found .env in pwd will be used as a fallback Env variables will always override settings in .env files.
48 lines
1.4 KiB
Python
48 lines
1.4 KiB
Python
import os
|
|
from decouple import (
|
|
Config,
|
|
RepositoryEmpty,
|
|
RepositoryEnv,
|
|
Undefined,
|
|
UndefinedValueError,
|
|
)
|
|
from loguru import logger
|
|
from typing import Any
|
|
|
|
_config: Config | None = None
|
|
|
|
|
|
def config(
|
|
option: str,
|
|
default: Any | Undefined = Undefined(),
|
|
cast: Any | Undefined = Undefined(),
|
|
):
|
|
if _config is None:
|
|
_setup_config()
|
|
logger.trace("Configuration was not initialized => calling setup_config()")
|
|
|
|
try:
|
|
if _config is not None: # query again, to please the linter
|
|
return _config(option, default=default, cast=cast)
|
|
except UndefinedValueError as e:
|
|
logger.debug(f"Type of _config: {_config}")
|
|
raise e
|
|
|
|
|
|
def _setup_config():
|
|
global _config
|
|
file_path = os.environ.get("BAPI_ENV_PATH")
|
|
file_path = "" if file_path is None else os.path.abspath(file_path)
|
|
try:
|
|
if os.path.isfile(file_path):
|
|
logger.info(f"Using configuration from: {file_path}")
|
|
_config = Config(RepositoryEnv(file_path))
|
|
elif os.path.isfile(".env"):
|
|
logger.info("Using configuration from: .env")
|
|
_config = Config(RepositoryEnv(".env"))
|
|
else:
|
|
logger.info("No configuration file found, using empty repository")
|
|
_config = Config(RepositoryEmpty())
|
|
except Exception as e:
|
|
logger.error(f"Exception {e}, using empty repository")
|
|
_config = Config(RepositoryEmpty())
|