Merge pull request 'v3.2.1' (#106) from dev into main
All checks were successful
Tests / test (3.10) (push) Successful in 1m3s
Tests / test (3.11) (push) Successful in 1m23s
Tests / test (3.8) (push) Successful in 1m7s
Tests / test (3.9) (push) Successful in 1m7s

Reviewed-on: #106
This commit is contained in:
Profitroll 2024-05-26 17:53:00 +03:00
commit 748b2b2abb
5 changed files with 51 additions and 8 deletions

View File

@ -1,4 +1,4 @@
__version__ = "3.2.0" __version__ = "3.2.1"
__license__ = "GPL3" __license__ = "GPL3"
__author__ = "Profitroll" __author__ = "Profitroll"

View File

@ -102,20 +102,28 @@ async def config_set(
async def config_delete( 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: ) -> None:
"""Set config's key by its path """Set config's key by its path
### Args: ### Args:
* key (`str`): Key to delete * key (`str`): Key to delete
* *path (`str`): Path to the key of the target * *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"` * 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: ### 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) config_data = await json_read(config_file)
try:
nested_delete(config_data, *(*path, key)) 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) await json_write(config_data, config_file)

View File

@ -98,20 +98,28 @@ def config_set(
def config_delete( 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: ) -> None:
"""Set config's key by its path """Set config's key by its path
### Args: ### Args:
* key (`str`): Key to delete * key (`str`): Key to delete
* *path (`str`): Path to the key of the target * *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"` * 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: ### 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) config_data = json_read(config_file)
try:
nested_delete(config_data, *(*path, key)) nested_delete(config_data, *(*path, key))
except KeyError as exc:
if not missing_ok:
raise exc from exc
json_write(config_data, config_file) json_write(config_data, config_file)

View File

@ -3,7 +3,7 @@ from typing import Any, List
import pytest import pytest
from libbot import config_delete, config_get, config_set, sync from libbot import config_delete, config_get, config_set
@pytest.mark.asyncio @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): async def test_config_delete(key: str, path: List[str], location_config: Path):
await config_delete(key, *path, config_file=location_config) await config_delete(key, *path, config_file=location_config)
assert key not in (await config_get(*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
)

View File

@ -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): def test_config_delete(key: str, path: List[str], location_config: Path):
sync.config_delete(key, *path, config_file=location_config) sync.config_delete(key, *path, config_file=location_config)
assert key not in sync.config_get(*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
)