From bead581a92c51ab1e7f18161972d73c6ffa3eb3f Mon Sep 17 00:00:00 2001 From: Jan Tuomi Date: Sun, 10 Sep 2023 11:19:57 +0300 Subject: Add dotenv and env var in config support --- app/utils.py | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) (limited to 'app/utils.py') diff --git a/app/utils.py b/app/utils.py index 626ee24..f0c175f 100644 --- a/app/utils.py +++ b/app/utils.py @@ -1,19 +1,36 @@ +import os from datetime import datetime -from typing import TypeAlias, Union +from typing import Any, TypeAlias, Union from app.Item import Item from dataclasses import asdict -from app.PluginInterface import Params - ItemDictValue: TypeAlias = Union[str, dict[str, "ItemDictValue"], list["ItemDictValue"]] ItemDict: TypeAlias = dict[str, ItemDictValue] -def get_param(key: str, params: Params) -> str: - if key not in params: - raise Exception(f"no {key} field in config entry: " + str(params)) +def evaluate_env_ref(v: Any) -> str: + if type(v) == str and v.startswith("${") and v.endswith("}"): + return os.environ[v[2:-1]] # type: ignore + else: + return v + + +def get_config(config: dict[str, Any], key: str) -> Any: + v: Any + try: + v = config[key] + except KeyError: + raise Exception(f"no {key} field in config: " + str(config)) + + return evaluate_env_ref(v) + + +def get_config_or_default( + config: dict[str, Any], key: str, default: Any | None = None +) -> Any: + v: Any = config.get(key, default) - return params[key] + return evaluate_env_ref(v) def item_to_dict(item: Item) -> ItemDict: -- cgit v1.3