2023-05-02 16:46:15 +03:00
|
|
|
from typing import Any
|
2023-05-04 17:09:47 +03:00
|
|
|
|
2023-05-02 16:46:15 +03:00
|
|
|
import aiofiles
|
2023-05-04 17:09:47 +03:00
|
|
|
from ujson import dumps, loads
|
2023-05-02 16:46:15 +03:00
|
|
|
|
|
|
|
|
|
|
|
async def json_read(path: str) -> Any:
|
|
|
|
async with aiofiles.open(path, mode="r", encoding="utf-8") as f:
|
|
|
|
data = await f.read()
|
|
|
|
return loads(data)
|
|
|
|
|
|
|
|
|
|
|
|
async def json_write(data: Any, path: str) -> None:
|
|
|
|
async with aiofiles.open(path, mode="w", encoding="utf-8") as f:
|
2023-05-06 18:51:07 +03:00
|
|
|
await f.write(dumps(data, ensure_ascii=False, escape_forward_slashes=False, indent=4))
|
2023-05-02 16:46:15 +03:00
|
|
|
|
|
|
|
|
|
|
|
async def config_get(key: str, *path: str) -> Any:
|
|
|
|
this_key = await json_read("config.json")
|
|
|
|
for dict_key in path:
|
|
|
|
this_key = this_key[dict_key]
|
|
|
|
return this_key[key]
|
|
|
|
|
|
|
|
|
|
|
|
async def config_set(key: str, value: Any, *path: str) -> None:
|
|
|
|
this_dict = await json_read("config.json")
|
|
|
|
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)
|
|
|
|
await json_write(this_dict, "config.json")
|
2023-05-04 17:14:23 +03:00
|
|
|
return
|