Add JSON database support
This commit is contained in:
50
discord/ext/json/database.py
Normal file
50
discord/ext/json/database.py
Normal 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)
|
||||
Reference in New Issue
Block a user