diff options
Diffstat (limited to 'app')
| -rw-r--r-- | app/AggroConfig.py | 4 | ||||
| -rw-r--r-- | app/PluginManager.py | 4 | ||||
| -rw-r--r-- | app/utils.py | 31 |
3 files changed, 29 insertions, 10 deletions
diff --git a/app/AggroConfig.py b/app/AggroConfig.py index 7e6a5f3..789fd6b 100644 --- a/app/AggroConfig.py +++ b/app/AggroConfig.py @@ -1,10 +1,12 @@ from dataclasses import dataclass +from app.PluginInterface import Params + @dataclass class AggroConfig: server_host: str server_port: int db_path: str - plugins: dict[str, dict[str, str]] + plugins: dict[str, Params] graph: dict[str, list[str]] diff --git a/app/PluginManager.py b/app/PluginManager.py index 68149fd..fb6b4c3 100644 --- a/app/PluginManager.py +++ b/app/PluginManager.py @@ -6,7 +6,7 @@ from typing import Any from app.Item import Item from app.PluginInterface import Params, PluginInterface from app.AggroConfig import AggroConfig -from app.utils import get_param +from app.utils import get_config from app.MemoryState import memory_state @@ -44,7 +44,7 @@ class PluginManager: for id in self.config.plugins: params: Params = self.config.plugins[id] - plugin_name = get_param("plugin", params) + plugin_name: str = get_config(params, "plugin") schedule_expr: str | None = params.get("schedule_expr", None) if plugin_name not in self._plugins: 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: |
