2021-07-31 15:43:10 +00:00
|
|
|
import logging
|
|
|
|
import os
|
|
|
|
from typing import Any, Dict
|
|
|
|
|
|
|
|
from freqtrade.constants import ENV_VAR_PREFIX
|
|
|
|
from freqtrade.misc import deep_merge_dicts
|
|
|
|
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2024-01-13 15:53:31 +00:00
|
|
|
def _get_var_typed(val):
|
2021-07-31 15:43:10 +00:00
|
|
|
try:
|
|
|
|
return int(val)
|
|
|
|
except ValueError:
|
|
|
|
try:
|
|
|
|
return float(val)
|
|
|
|
except ValueError:
|
2024-05-12 14:29:24 +00:00
|
|
|
if val.lower() in ("t", "true"):
|
2021-07-31 15:43:10 +00:00
|
|
|
return True
|
2024-05-12 14:29:24 +00:00
|
|
|
elif val.lower() in ("f", "false"):
|
2021-07-31 15:43:10 +00:00
|
|
|
return False
|
|
|
|
# keep as string
|
|
|
|
return val
|
|
|
|
|
|
|
|
|
2024-01-13 15:53:31 +00:00
|
|
|
def _flat_vars_to_nested_dict(env_dict: Dict[str, Any], prefix: str) -> Dict[str, Any]:
|
2021-07-31 15:43:10 +00:00
|
|
|
"""
|
|
|
|
Environment variables must be prefixed with FREQTRADE.
|
|
|
|
FREQTRADE__{section}__{key}
|
|
|
|
:param env_dict: Dictionary to validate - usually os.environ
|
|
|
|
:param prefix: Prefix to consider (usually FREQTRADE__)
|
|
|
|
:return: Nested dict based on available and relevant variables.
|
|
|
|
"""
|
2024-05-12 14:29:24 +00:00
|
|
|
no_convert = ["CHAT_ID", "PASSWORD"]
|
2021-07-31 15:43:10 +00:00
|
|
|
relevant_vars: Dict[str, Any] = {}
|
|
|
|
|
|
|
|
for env_var, val in sorted(env_dict.items()):
|
|
|
|
if env_var.startswith(prefix):
|
|
|
|
logger.info(f"Loading variable '{env_var}'")
|
2024-05-12 14:29:24 +00:00
|
|
|
key = env_var.replace(prefix, "")
|
|
|
|
for k in reversed(key.split("__")):
|
|
|
|
val = {
|
2024-05-13 17:49:15 +00:00
|
|
|
k.lower(): (
|
|
|
|
_get_var_typed(val)
|
|
|
|
if not isinstance(val, dict) and k not in no_convert
|
|
|
|
else val
|
|
|
|
)
|
2024-05-12 14:29:24 +00:00
|
|
|
}
|
2021-07-31 15:43:10 +00:00
|
|
|
relevant_vars = deep_merge_dicts(val, relevant_vars)
|
|
|
|
return relevant_vars
|
|
|
|
|
|
|
|
|
|
|
|
def enironment_vars_to_dict() -> Dict[str, Any]:
|
|
|
|
"""
|
|
|
|
Read environment variables and return a nested dict for relevant variables
|
|
|
|
Relevant variables must follow the FREQTRADE__{section}__{key} pattern
|
|
|
|
:return: Nested dict based on available and relevant variables.
|
|
|
|
"""
|
2024-01-13 15:53:31 +00:00
|
|
|
return _flat_vars_to_nested_dict(os.environ.copy(), ENV_VAR_PREFIX)
|