Improved typing and added config_file to funcs

This commit is contained in:
Profitroll 2023-05-11 20:23:51 +02:00
parent c2c85324d5
commit 4bca6d97d4
2 changed files with 28 additions and 21 deletions

View File

@ -1,28 +1,32 @@
from typing import Any from pathlib import Path
from typing import Any, Union
import aiofiles import aiofiles
from ujson import loads, dumps from ujson import dumps, loads
async def json_read(path: str) -> Any: async def json_read(path: Union[str, Path]) -> Any:
async with aiofiles.open(path, mode="r", encoding="utf-8") as f: async with aiofiles.open(str(path), mode="r", encoding="utf-8") as f:
data = await f.read() data = await f.read()
return loads(data) return loads(data)
async def json_write(data: Any, path: str) -> None: async def json_write(data: Any, path: Union[str, Path]) -> None:
async with aiofiles.open(path, mode="w", encoding="utf-8") as f: async with aiofiles.open(str(path), mode="w", encoding="utf-8") as f:
await f.write(dumps(data, ensure_ascii=False, escape_forward_slashes=False)) await f.write(dumps(data, ensure_ascii=False, escape_forward_slashes=False))
async def config_get(key: str, *path: str) -> Any: async def config_get(key: str, *path: str, config_file: str = "config.json") -> Any:
this_key = await json_read("config.json") this_key = await json_read(config_file)
for dict_key in path: for dict_key in path:
this_key = this_key[dict_key] this_key = this_key[dict_key]
return this_key[key] return this_key[key]
async def config_set(key: str, value: Any, *path: str) -> None: async def config_set(
this_dict = await json_read("config.json") key: str, value: Any, *path: str, config_file: str = "config.json"
) -> None:
this_dict = await json_read(config_file)
string = "this_dict" string = "this_dict"
for arg in path: for arg in path:
string += f'["{arg}"]' string += f'["{arg}"]'
@ -31,5 +35,5 @@ async def config_set(key: str, value: Any, *path: str) -> None:
else: else:
string += f'["{key}"] = {value}' string += f'["{key}"] = {value}'
exec(string) exec(string)
await json_write(this_dict, "config.json") await json_write(this_dict, config_file)
return return

View File

@ -1,28 +1,31 @@
from typing import Any from pathlib import Path
from typing import Any, Union
from ujson import dumps, loads from ujson import dumps, loads
def json_read(path: str) -> Any: def json_read(path: Union[str, Path]) -> Any:
with open(path, mode="r", encoding="utf-8") as f: with open(str(path), mode="r", encoding="utf-8") as f:
data = f.read() data = f.read()
return loads(data) return loads(data)
def json_write(data: Any, path: str) -> None: def json_write(data: Any, path: Union[str, Path]) -> None:
with open(path, mode="w", encoding="utf-8") as f: with open(str(path), mode="w", encoding="utf-8") as f:
f.write(dumps(data, ensure_ascii=False, escape_forward_slashes=False, indent=4)) f.write(dumps(data, ensure_ascii=False, escape_forward_slashes=False, indent=4))
def config_get(key: str, *path: str) -> Any: def config_get(key: str, *path: str, config_file: str = "config.json") -> Any:
this_key = json_read("config.json") this_key = json_read(config_file)
for dict_key in path: for dict_key in path:
this_key = this_key[dict_key] this_key = this_key[dict_key]
return this_key[key] return this_key[key]
def config_set(key: str, value: Any, *path: str) -> None: def config_set(
this_dict = json_read("config.json") key: str, value: Any, *path: str, config_file: str = "config.json"
) -> None:
this_dict = json_read(config_file)
string = "this_dict" string = "this_dict"
for arg in path: for arg in path:
string += f'["{arg}"]' string += f'["{arg}"]'
@ -31,5 +34,5 @@ def config_set(key: str, value: Any, *path: str) -> None:
else: else:
string += f'["{key}"] = {value}' string += f'["{key}"] = {value}'
exec(string) exec(string)
json_write(this_dict, "config.json") json_write(this_dict, config_file)
return return