From 6d3c20479dd11d844ae9fb9340393f1648b4def9 Mon Sep 17 00:00:00 2001 From: profitroll Date: Sun, 26 May 2024 16:50:14 +0200 Subject: [PATCH 1/2] Added optional missing_ok to config_delete --- src/libbot/__main__.py | 14 +++++++++++--- src/libbot/sync/__main__.py | 14 +++++++++++--- tests/test_config_async.py | 16 +++++++++++++++- tests/test_config_sync.py | 13 +++++++++++++ 4 files changed, 50 insertions(+), 7 deletions(-) diff --git a/src/libbot/__main__.py b/src/libbot/__main__.py index ccac675..b7f03b3 100644 --- a/src/libbot/__main__.py +++ b/src/libbot/__main__.py @@ -102,20 +102,28 @@ async def config_set( async def config_delete( - key: str, *path: str, config_file: Union[str, Path] = "config.json" + key: str, + *path: str, + missing_ok: bool = False, + config_file: Union[str, Path] = "config.json", ) -> None: """Set config's key by its path ### Args: * key (`str`): Key to delete * *path (`str`): Path to the key of the target + * missing_ok (`bool`): Do not raise an exception if the key is missing. Defaults to `False` * config_file (`Union[str, Path]`, *optional*): Path-like object or path as a string of a location of the config file. Defaults to `"config.json"` ### Raises: - * `KeyError`: Key is not found under path provided + * `KeyError`: Key is not found under path provided and `missing_ok` is `False` """ config_data = await json_read(config_file) - nested_delete(config_data, *(*path, key)) + try: + nested_delete(config_data, *(*path, key)) + except KeyError as exc: + if not missing_ok: + raise exc from exc await json_write(config_data, config_file) diff --git a/src/libbot/sync/__main__.py b/src/libbot/sync/__main__.py index 1abf1d4..4745ab8 100644 --- a/src/libbot/sync/__main__.py +++ b/src/libbot/sync/__main__.py @@ -98,20 +98,28 @@ def config_set( def config_delete( - key: str, *path: str, config_file: Union[str, Path] = "config.json" + key: str, + *path: str, + missing_ok: bool = False, + config_file: Union[str, Path] = "config.json", ) -> None: """Set config's key by its path ### Args: * key (`str`): Key to delete * *path (`str`): Path to the key of the target + * missing_ok (`bool`): Do not raise an exception if the key is missing. Defaults to `False` * config_file (`Union[str, Path]`, *optional*): Path-like object or path as a string of a location of the config file. Defaults to `"config.json"` ### Raises: - * `KeyError`: Key is not found under path provided + * `KeyError`: Key is not found under path provided and `missing_ok` is `False` """ config_data = json_read(config_file) - nested_delete(config_data, *(*path, key)) + try: + nested_delete(config_data, *(*path, key)) + except KeyError as exc: + if not missing_ok: + raise exc from exc json_write(config_data, config_file) diff --git a/tests/test_config_async.py b/tests/test_config_async.py index c23223c..1ef144d 100644 --- a/tests/test_config_async.py +++ b/tests/test_config_async.py @@ -3,7 +3,7 @@ from typing import Any, List import pytest -from libbot import config_delete, config_get, config_set, sync +from libbot import config_delete, config_get, config_set @pytest.mark.asyncio @@ -58,3 +58,17 @@ async def test_config_set(key: str, path: List[str], value: Any, location_config async def test_config_delete(key: str, path: List[str], location_config: Path): await config_delete(key, *path, config_file=location_config) assert key not in (await config_get(*path, config_file=location_config)) + + +@pytest.mark.asyncio +@pytest.mark.parametrize( + "key, path", + [ + ("bot_lol", ["bot"]), + ], +) +async def test_config_delete_missing(key: str, path: List[str], location_config: Path): + assert ( + await config_delete(key, *path, missing_ok=True, config_file=location_config) + is None + ) diff --git a/tests/test_config_sync.py b/tests/test_config_sync.py index 6ffe4af..522dcda 100644 --- a/tests/test_config_sync.py +++ b/tests/test_config_sync.py @@ -51,3 +51,16 @@ def test_config_set(key: str, path: List[str], value: Any, location_config: Path def test_config_delete(key: str, path: List[str], location_config: Path): sync.config_delete(key, *path, config_file=location_config) assert key not in sync.config_get(*path, config_file=location_config) + + +@pytest.mark.parametrize( + "key, path", + [ + ("bot_lol", ["bot"]), + ], +) +async def test_config_delete_missing(key: str, path: List[str], location_config: Path): + assert ( + sync.config_delete(key, *path, missing_ok=True, config_file=location_config) + is None + ) -- 2.39.2 From 15f92740504d5cfd9d746c0e3f0a7f86492557f4 Mon Sep 17 00:00:00 2001 From: profitroll Date: Sun, 26 May 2024 16:50:42 +0200 Subject: [PATCH 2/2] Bump version to 3.2.1 --- src/libbot/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libbot/__init__.py b/src/libbot/__init__.py index 15853ce..3fc6fec 100644 --- a/src/libbot/__init__.py +++ b/src/libbot/__init__.py @@ -1,4 +1,4 @@ -__version__ = "3.2.0" +__version__ = "3.2.1" __license__ = "GPL3" __author__ = "Profitroll" -- 2.39.2