Created initial version

This commit is contained in:
Profitroll
2023-05-11 20:19:46 +02:00
parent ca948b0329
commit 948942fdf2
7 changed files with 134 additions and 0 deletions

7
libbot/__init__.py Normal file
View File

@@ -0,0 +1,7 @@
__name__ = "libbot"
__version__ = "0.1"
__license__ = "GPL3"
__author__ = "Profitroll"
from .__main__ import *
from . import sync

35
libbot/__main__.py Normal file
View File

@@ -0,0 +1,35 @@
from typing import Any
import aiofiles
from ujson import loads, dumps
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:
await f.write(dumps(data, ensure_ascii=False, escape_forward_slashes=False))
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")
return

35
libbot/sync/__init__.py Normal file
View File

@@ -0,0 +1,35 @@
from typing import Any
from ujson import dumps, loads
def json_read(path: str) -> Any:
with open(path, mode="r", encoding="utf-8") as f:
data = f.read()
return loads(data)
def json_write(data: Any, path: str) -> None:
with open(path, mode="w", encoding="utf-8") as f:
f.write(dumps(data, ensure_ascii=False, escape_forward_slashes=False, indent=4))
def config_get(key: str, *path: str) -> Any:
this_key = json_read("config.json")
for dict_key in path:
this_key = this_key[dict_key]
return this_key[key]
def config_set(key: str, value: Any, *path: str) -> None:
this_dict = 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)
json_write(this_dict, "config.json")
return