61 lines
1.4 KiB
Python
61 lines
1.4 KiB
Python
from yaml import load
|
|
|
|
from jarvis.db import DBManager
|
|
|
|
try:
|
|
from yaml import CLoader as Loader
|
|
except ImportError:
|
|
from yaml import Loader
|
|
|
|
|
|
class Config(object):
|
|
def __new__(cls, *args, **kwargs):
|
|
it = cls.__dict__.get("it")
|
|
if it is not None:
|
|
return it
|
|
cls.__it__ = it = object.__new__(cls)
|
|
it.init(*args, **kwargs)
|
|
return it
|
|
|
|
def init(
|
|
self,
|
|
token: str,
|
|
client_id: str,
|
|
logo: str,
|
|
mongo: dict,
|
|
urls: dict,
|
|
cogs: list = None,
|
|
gitlab_token: str = None,
|
|
max_messages: int = 1000,
|
|
):
|
|
self.token = token
|
|
self.client_id = client_id
|
|
self.logo = logo
|
|
self.mongo = mongo
|
|
self.urls = urls
|
|
self.cogs = cogs
|
|
self.max_messages = max_messages
|
|
self.gitlab_token = gitlab_token
|
|
db = DBManager(config=mongo["connect"]).mongo[mongo["database"]].config
|
|
db_config = db.find()
|
|
for item in db_config:
|
|
setattr(self, item["key"], item["value"])
|
|
|
|
@classmethod
|
|
def from_yaml(cls, y):
|
|
instance = cls(**y)
|
|
return instance
|
|
|
|
|
|
def get_config(path: str = "config.yaml") -> Config:
|
|
if Config.__dict__.get("it"):
|
|
return Config()
|
|
with open(path) as f:
|
|
raw = f.read()
|
|
y = load(raw, Loader=Loader)
|
|
return Config.from_yaml(y)
|
|
|
|
|
|
def reload_config():
|
|
if "it" in Config.__dict__:
|
|
Config.__dict__.pop("it")
|