From c3032feaf7e3e974cb9cc743f559ebba170a807c Mon Sep 17 00:00:00 2001 From: Alexander Malysh Date: Thu, 14 Nov 2024 10:25:14 +0000 Subject: [PATCH 1/4] * allow json in env variables --- freqtrade/configuration/environment_vars.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/freqtrade/configuration/environment_vars.py b/freqtrade/configuration/environment_vars.py index 37445538d..5baa2bfc3 100644 --- a/freqtrade/configuration/environment_vars.py +++ b/freqtrade/configuration/environment_vars.py @@ -1,3 +1,4 @@ +import json import logging import os from typing import Any @@ -20,6 +21,11 @@ def _get_var_typed(val): return True elif val.lower() in ("f", "false"): return False + # try to convert from json + try: + return json.loads(val) + except json.decoder.JSONDecodeError: + pass # keep as string return val From 0683ba3a54da1ec56252f732b680bf27f58823c4 Mon Sep 17 00:00:00 2001 From: Matthias Date: Sun, 8 Dec 2024 08:30:45 +0100 Subject: [PATCH 2/4] feat: limit environment-variable json parsing to lists --- freqtrade/configuration/environment_vars.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/freqtrade/configuration/environment_vars.py b/freqtrade/configuration/environment_vars.py index 5baa2bfc3..8a825f59b 100644 --- a/freqtrade/configuration/environment_vars.py +++ b/freqtrade/configuration/environment_vars.py @@ -1,8 +1,9 @@ -import json import logging import os from typing import Any +import rapidjson + from freqtrade.constants import ENV_VAR_PREFIX from freqtrade.misc import deep_merge_dicts @@ -23,8 +24,11 @@ def _get_var_typed(val): return False # try to convert from json try: - return json.loads(val) - except json.decoder.JSONDecodeError: + value = rapidjson.loads(val) + # Limited to lists for now + if isinstance(value, list): + return value + except rapidjson.JSONDecodeError: pass # keep as string return val From 934bcf253eb4427ab8d372cb193e2346e49ca0ad Mon Sep 17 00:00:00 2001 From: Matthias Date: Sun, 8 Dec 2024 08:30:55 +0100 Subject: [PATCH 3/4] test: add tests for list parsing --- tests/test_configuration.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tests/test_configuration.py b/tests/test_configuration.py index 8011ded96..3e9df382b 100644 --- a/tests/test_configuration.py +++ b/tests/test_configuration.py @@ -1481,6 +1481,12 @@ def test_flat_vars_to_nested_dict(caplog): "FREQTRADE__STAKE_AMOUNT": "200.05", "FREQTRADE__TELEGRAM__CHAT_ID": "2151", "NOT_RELEVANT": "200.0", # Will be ignored + "FREQTRADE__ARRAY": '[{"name":"default","host":"xxx"}]', + "FREQTRADE__EXCHANGE__PAIR_WHITELIST": '["BTC/USDT", "ETH/USDT"]', + # Fails due to trailing comma + "FREQTRADE__ARRAY_TRAIL_COMMA": '[{"name":"default","host":"xxx",}]', + # Object fails + "FREQTRADE__OBJECT": '{"name":"default","host":"xxx"}', } expected = { "stake_amount": 200.05, @@ -1494,8 +1500,12 @@ def test_flat_vars_to_nested_dict(caplog): }, "some_setting": True, "some_false_setting": False, + "pair_whitelist": ["BTC/USDT", "ETH/USDT"], }, "telegram": {"chat_id": "2151"}, + "array": [{"name": "default", "host": "xxx"}], + "object": '{"name":"default","host":"xxx"}', + "array_trail_comma": '[{"name":"default","host":"xxx",}]', } res = _flat_vars_to_nested_dict(test_args, ENV_VAR_PREFIX) assert res == expected From 10b5d5e56bfb758db825cd5c2daadf3e0eaa4810 Mon Sep 17 00:00:00 2001 From: Matthias Date: Sun, 8 Dec 2024 08:35:13 +0100 Subject: [PATCH 4/4] docs: add "list parsing" logic to documentation --- docs/configuration.md | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/docs/configuration.md b/docs/configuration.md index 16ae37603..0228f6234 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -39,13 +39,19 @@ Please note that Environment variables will overwrite corresponding settings in Common example: -``` +``` bash FREQTRADE__TELEGRAM__CHAT_ID= FREQTRADE__TELEGRAM__TOKEN= FREQTRADE__EXCHANGE__KEY= FREQTRADE__EXCHANGE__SECRET= ``` +Json lists are parsed as json - so you can use the following to set a list of pairs: + +``` bash +export FREQTRADE__EXCHANGE__PAIR_WHITELIST='["BTC/USDT", "ETH/USDT"]' +``` + !!! Note Environment variables detected are logged at startup - so if you can't find why a value is not what you think it should be based on the configuration, make sure it's not loaded from an environment variable. @@ -54,7 +60,7 @@ FREQTRADE__EXCHANGE__SECRET= ??? Warning "Loading sequence" Environment variables are loaded after the initial configuration. As such, you cannot provide the path to the configuration through environment variables. Please use `--config path/to/config.json` for that. - This also applies to user_dir to some degree. while the user directory can be set through environment variables - the configuration will **not** be loaded from that location. + This also applies to `user_dir` to some degree. while the user directory can be set through environment variables - the configuration will **not** be loaded from that location. ### Multiple configuration files