Add JSON database support

This commit is contained in:
privacy lulz
2021-09-02 15:45:46 +00:00
parent 7ca4e650fb
commit 08d012d6dd
2 changed files with 104 additions and 12 deletions

View File

@@ -0,0 +1,50 @@
from .types import JSONFile, Entry
from typing import Union, Any
class Database:
def __init__(self, file:JSONFile):
self.file = file;
self.database = {};
async def getData(self) -> dict:
"""
Get contents from a JSONFile
"""
contents = await self.file.serialize("load")
return contents
async def dumpData(self, data:dict) -> None:
"""
Dump a dict into file
=====
data `dict` -
The data to be dumped into the file.
"""
await self.file.serialize("dump", contents=data)
async def loadFile(self) -> None:
"""
Load JSON from file to self.database
"""
self.database = await self.getData();
async def getEntry(self, name:str) -> Entry:
value : Union[Any] = self.database[name]
return Entry(name, value)
async def editEntry(self, name:str, value:Union[Any]):
self.database[name] = value;
async def saveData(self):
"""
Save current database to file
"""
await self.file.serialize("dump", contents=self.database)