Skip to content

Commit

Permalink
[Config] keep inactive settings in nested dict configs
Browse files Browse the repository at this point in the history
  • Loading branch information
techfreaque committed Mar 18, 2023
1 parent d67160c commit 474a8b9
Show file tree
Hide file tree
Showing 3 changed files with 174 additions and 4 deletions.
4 changes: 2 additions & 2 deletions octobot_tentacles_manager/api/configurator.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ def get_activated_tentacles(tentacles_setup_config: configuration.TentaclesSetup


def update_tentacle_config(tentacles_setup_config: configuration.TentaclesSetupConfiguration,
tentacle_class: object, config_data: dict) -> None:
configuration.update_config(tentacles_setup_config, tentacle_class, config_data)
tentacle_class: object, config_data: dict, keep_existing: bool=True) -> None:
configuration.update_config(tentacles_setup_config, tentacle_class, config_data, keep_existing)


def import_user_tentacles_config_folder(tentacles_setup_config: configuration.TentaclesSetupConfiguration):
Expand Down
20 changes: 18 additions & 2 deletions octobot_tentacles_manager/configuration/tentacle_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,30 @@ def get_config(tentacles_setup_config, klass) -> dict:
return configuration.read_config(config_path)


def update_config(tentacles_setup_config, klass, config_update) -> None:
def update_config(tentacles_setup_config, klass, config_update, keep_existing = True) -> None:
config_file = _get_config_file_path(tentacles_setup_config, klass)
current_config = configuration.read_config(config_file)
# only update values in config update not to erase values in root config (might not be editable)
current_config.update(config_update)
if keep_existing:
# keep inactive settings in nested dicts
current_config = _recursive_config_update(current_config, config_update)
else:
current_config.update(config_update)
config_file = _get_config_file_path(tentacles_setup_config, klass, updated_config=True)
configuration.write_config(config_file, current_config)

def _recursive_config_update(current_config: dict, config_update: dict)-> dict:
merged_configs = {**current_config}
for key, values in config_update.items():
if isinstance(values, dict):
if isinstance(current_config.get(key), dict):
merged_configs[key] = _recursive_config_update(
current_config=current_config[key],
config_update=values
)
continue
merged_configs[key] = values
return merged_configs

def factory_reset_config(tentacles_setup_config, klass) -> None:
shutil.copy(_get_reference_config_file_path(klass), _get_config_file_path(tentacles_setup_config, klass))
Expand Down
154 changes: 154 additions & 0 deletions tests/configuration/test_tentacle_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ async def test_get_config():


async def test_update_config():
_cleanup()
async with aiohttp.ClientSession() as session:
await api.install_all_tentacles(_tentacles_local_path(), aiohttp_session=session)
from tentacles.Evaluator.RealTime import InstantFluctuationsEvaluator
Expand All @@ -67,6 +68,159 @@ async def test_update_config():
_cleanup()


async def test_keep_existing_update_config():
async with aiohttp.ClientSession() as session:
await api.install_all_tentacles(_tentacles_local_path(), aiohttp_session=session)
from tentacles.Evaluator.RealTime import InstantFluctuationsEvaluator
setup_config = configuration.TentaclesSetupConfiguration()
# init nested config
config_update = {
"price_difference_threshold_percent": 2,
"plop": 42,
"nested_thing": {
"price_difference_threshold_percent": 2,
"plop": 42,
"another_nested_thing": {
"price_difference_threshold_percent": 2,
"plop": 42
}
}
}
update_config(setup_config, InstantFluctuationsEvaluator, config_update)
assert get_config(setup_config, InstantFluctuationsEvaluator) == {
"price_difference_threshold_percent": 2,
"volume_difference_threshold_percent": 400,
"plop": 42,
"nested_thing": {
"price_difference_threshold_percent": 2,
"plop": 42,
"another_nested_thing": {
"price_difference_threshold_percent": 2,
"plop": 42
}
}
}

# test keep existing option
config_update = {
"nested_thing": {
"new_other_nested_thing": {
"price_difference_threshold_percent": 2,
"plop": 42
}
}
}
update_config(setup_config, InstantFluctuationsEvaluator, config_update, keep_existing=True)
assert get_config(setup_config, InstantFluctuationsEvaluator) == {
"price_difference_threshold_percent": 2,
"volume_difference_threshold_percent": 400,
"plop": 42,
"nested_thing": {
"price_difference_threshold_percent": 2,
"plop": 42,
"another_nested_thing": {
"price_difference_threshold_percent": 2,
"plop": 42
},
"new_other_nested_thing": {
"price_difference_threshold_percent": 2,
"plop": 42
}
}
}
# test deep nested with keep existing option
config_update = {
"nested_thing": {
"new_other_nested_thing": {
"i am very deep": {
"price_difference_threshold_percent": 2,
"plop": 42
}
}
}
}
update_config(setup_config, InstantFluctuationsEvaluator, config_update, keep_existing=True)
assert get_config(setup_config, InstantFluctuationsEvaluator) == {
"price_difference_threshold_percent": 2,
"volume_difference_threshold_percent": 400,
"plop": 42,
"nested_thing": {
"price_difference_threshold_percent": 2,
"plop": 42,
"another_nested_thing": {
"price_difference_threshold_percent": 2,
"plop": 42
},
"new_other_nested_thing": {
"price_difference_threshold_percent": 2,
"plop": 42,
"i am very deep": {
"price_difference_threshold_percent": 2,
"plop": 42
}
}
}
}
# try adding to deep config
config_update = {
"nested_thing": {
"new_other_nested_thing": {
"i am also deep": {
"price_difference_threshold_percent": 2,
"plop": 42
}
}
}
}
update_config(setup_config, InstantFluctuationsEvaluator, config_update, keep_existing=True)
assert get_config(setup_config, InstantFluctuationsEvaluator) == {
"price_difference_threshold_percent": 2,
"volume_difference_threshold_percent": 400,
"plop": 42,
"nested_thing": {
"price_difference_threshold_percent": 2,
"plop": 42,
"another_nested_thing": {
"price_difference_threshold_percent": 2,
"plop": 42
},
"new_other_nested_thing": {
"price_difference_threshold_percent": 2,
"plop": 42,
"i am very deep": {
"price_difference_threshold_percent": 2,
"plop": 42
},
"i am also deep": {
"price_difference_threshold_percent": 2,
"plop": 42
}
}
}
}

# test keep existing false
config_update = {
"nested_thing": {
"i am alone here": {
"price_difference_threshold_percent": 42,
}
}
}
update_config(setup_config, InstantFluctuationsEvaluator, config_update, keep_existing=False)
assert get_config(setup_config, InstantFluctuationsEvaluator) == {
"price_difference_threshold_percent": 2,
"volume_difference_threshold_percent": 400,
"plop": 42,
"nested_thing": {
"i am alone here": {
"price_difference_threshold_percent": 42,
}
}
}
_cleanup()


async def test_factory_reset_config():
async with aiohttp.ClientSession() as session:
await api.install_all_tentacles(_tentacles_local_path(), aiohttp_session=session)
Expand Down

0 comments on commit 474a8b9

Please sign in to comment.