2023-10-01 17:47:35 +00:00
|
|
|
# pylint: disable=too-few-public-methods
|
|
|
|
|
|
|
|
import dictdatabase as DDB
|
2023-11-24 19:50:18 +00:00
|
|
|
from ..config import DatabaseSettings
|
2023-10-01 17:47:35 +00:00
|
|
|
|
|
|
|
class Database:
|
|
|
|
"""
|
|
|
|
This is a singleton that can be accessed using get_instance()
|
|
|
|
"""
|
2023-11-24 19:50:18 +00:00
|
|
|
class IsSingletonException(Exception):
|
|
|
|
pass
|
|
|
|
|
2023-10-01 17:47:35 +00:00
|
|
|
__instance = None
|
|
|
|
db = DDB
|
2023-11-24 19:50:18 +00:00
|
|
|
DatabaseSettings.DATABASE_DIRECTORY.mkdir(exist_ok=True, parents=True)
|
2023-10-01 17:47:35 +00:00
|
|
|
db.config.storage_directory = DatabaseSettings.DATABASE_DIRECTORY
|
2023-10-01 19:40:06 +00:00
|
|
|
if not db.at("requests").exists():
|
|
|
|
db.at("requests").create({})
|
|
|
|
if not db.at("profiles").exists():
|
|
|
|
db.at("profiles").create({})
|
2023-10-01 17:47:35 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_instance():
|
|
|
|
if Database.__instance is None:
|
|
|
|
Database()
|
|
|
|
return Database.__instance
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
if Database.__instance is not None:
|
2023-11-24 19:50:18 +00:00
|
|
|
raise self.IsSingletonException("This class is a singleton")
|
2023-10-01 17:47:35 +00:00
|
|
|
Database.__instance = self
|