2023-05-11 21:23:51 +03:00
|
|
|
from pathlib import Path
|
|
|
|
from typing import Any, Union
|
2023-05-11 21:19:46 +03:00
|
|
|
|
|
|
|
from ujson import dumps, loads
|
|
|
|
|
|
|
|
|
2023-05-11 21:23:51 +03:00
|
|
|
def json_read(path: Union[str, Path]) -> Any:
|
2023-05-11 21:52:08 +03:00
|
|
|
"""Read contents of a JSON file
|
|
|
|
|
|
|
|
### Args:
|
|
|
|
* path (`Union[str, Path]`): Path-like object or path as a string
|
|
|
|
|
|
|
|
### Returns:
|
|
|
|
* `Any`: File contents
|
|
|
|
"""
|
2023-05-11 21:23:51 +03:00
|
|
|
with open(str(path), mode="r", encoding="utf-8") as f:
|
2023-05-11 21:19:46 +03:00
|
|
|
data = f.read()
|
|
|
|
return loads(data)
|
|
|
|
|
|
|
|
|
2023-05-11 21:23:51 +03:00
|
|
|
def json_write(data: Any, path: Union[str, Path]) -> None:
|
2023-05-11 21:52:08 +03:00
|
|
|
"""Write contents to a JSON file
|
|
|
|
|
|
|
|
### Args:
|
|
|
|
* data (`Any`): Contents to write. Must be a JSON serializable
|
|
|
|
* path (`Union[str, Path]`): Path-like object or path as a string of a destination
|
|
|
|
"""
|
2023-05-11 21:23:51 +03:00
|
|
|
with open(str(path), mode="w", encoding="utf-8") as f:
|
2023-05-11 21:19:46 +03:00
|
|
|
f.write(dumps(data, ensure_ascii=False, escape_forward_slashes=False, indent=4))
|
|
|
|
|
|
|
|
|
2023-05-11 21:52:08 +03:00
|
|
|
def config_get(
|
|
|
|
key: str, *path: str, config_file: Union[str, Path] = "config.json"
|
|
|
|
) -> Any:
|
|
|
|
"""Get a value of the config key by its path provided
|
|
|
|
For example, `foo.bar.key` has a path of `"foo", "bar"` and the key `"key"`
|
|
|
|
|
|
|
|
### Args:
|
|
|
|
* key (`str`): Key that contains the value
|
|
|
|
* *path (`str`): Path to the key that contains the value
|
|
|
|
* 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"`
|
|
|
|
|
|
|
|
### Returns:
|
|
|
|
* `Any`: Key's value
|
|
|
|
|
|
|
|
### Example:
|
|
|
|
Get the "salary" of "Pete" from this JSON structure:
|
|
|
|
```json
|
|
|
|
{
|
|
|
|
"users": {
|
|
|
|
"Pete": {
|
|
|
|
"salary": 10.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
This can be easily done with the following code:
|
|
|
|
```python
|
|
|
|
import libbot
|
|
|
|
salary = libbot.sync.config_get("salary", "users", "Pete")
|
|
|
|
```
|
|
|
|
"""
|
2023-05-11 21:23:51 +03:00
|
|
|
this_key = json_read(config_file)
|
2023-05-11 21:19:46 +03:00
|
|
|
for dict_key in path:
|
|
|
|
this_key = this_key[dict_key]
|
|
|
|
return this_key[key]
|
|
|
|
|
|
|
|
|
2023-05-11 21:23:51 +03:00
|
|
|
def config_set(
|
2023-05-11 21:52:08 +03:00
|
|
|
key: str, value: Any, *path: str, config_file: Union[str, Path] = "config.json"
|
2023-05-11 21:23:51 +03:00
|
|
|
) -> None:
|
2023-05-11 21:52:08 +03:00
|
|
|
"""Set config's key by its path to the value
|
|
|
|
|
|
|
|
### Args:
|
|
|
|
* key (`str`): Key that leads to the value
|
|
|
|
* value (`Any`): Any JSON serializable data
|
|
|
|
* *path (`str`): Path to the key of the target
|
|
|
|
* 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"`
|
|
|
|
"""
|
2023-05-11 21:23:51 +03:00
|
|
|
this_dict = json_read(config_file)
|
2023-05-11 21:19:46 +03:00
|
|
|
string = "this_dict"
|
|
|
|
for arg in path:
|
|
|
|
string += f'["{arg}"]'
|
|
|
|
if type(value) in [str]:
|
|
|
|
string += f'["{key}"] = "{value}"'
|
|
|
|
else:
|
|
|
|
string += f'["{key}"] = {value}'
|
|
|
|
exec(string)
|
2023-05-11 21:23:51 +03:00
|
|
|
json_write(this_dict, config_file)
|
2023-05-11 21:19:46 +03:00
|
|
|
return
|