48 lines
1.2 KiB
Python
48 lines
1.2 KiB
Python
import logging, uuid
|
|
from datetime import datetime
|
|
from enum import StrEnum, auto
|
|
from typing import Dict
|
|
|
|
class Notification:
|
|
class Status(StrEnum):
|
|
INFO = auto()
|
|
WARN = auto()
|
|
ERROR = auto()
|
|
|
|
def __init__(
|
|
self,
|
|
module: str = "server",
|
|
status: Status = Status.INFO,
|
|
msg: str = "Default Notification",
|
|
):
|
|
self.status = status
|
|
self.msg = msg
|
|
self.module = module.lower()
|
|
self.time = datetime.now()
|
|
|
|
def as_dict(self):
|
|
return {
|
|
"status": str(self.status),
|
|
"module": self.module,
|
|
"msg": self.msg,
|
|
"time": self.time.isoformat()
|
|
}
|
|
|
|
class NotificationManager:
|
|
def __init__(self):
|
|
self.notifications = {}
|
|
|
|
def notify(self, notification: Notification):
|
|
self.notifications[str(uuid.uuid4())] = notification
|
|
|
|
def delete(self, nid):
|
|
is_removed: bool = self.notifications.pop(nid, None) is not None
|
|
if is_removed: logging.getLogger(__name__).log(logging.INFO, f"deleted {nid}")
|
|
return is_removed
|
|
|
|
def serialized(self):
|
|
return {str(k): self.notifications[k].as_dict() for k in self.notifications.keys()}
|
|
|
|
|
|
NOTIFICATION_MANAGER = NotificationManager()
|