Added optional missing_ok to config_delete

This commit is contained in:
2024-05-26 16:50:14 +02:00
parent dfaadfd769
commit 6d3c20479d
4 changed files with 50 additions and 7 deletions

View File

@@ -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)

View File

@@ -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)