123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235 |
- import warnings
- from functools import partial
- from socketio import Server
- from typing import Callable
- from pycs import app
- from pycs.database.File import File
- from pycs.database.Label import Label
- from pycs.database.Model import Model
- from pycs.database.Project import Project
- from pycs.database.Result import Result
- from pycs.frontend.util.JSONEncoder import JSONEncoder
- from pycs.jobs.Job import Job
- from pycs.util import Singleton
- class NotificationManager(Singleton):
- """
- send events via a socket.io connection
- """
- def __repr__(self) -> str:
- return f"<{self.__class__.__name__} 0x{id(self):x}>"
- def setup(self, sio: Server):
- self.sio = sio
- def init(self):
- self.sio = None
- self.json = JSONEncoder()
- def __call__(self, name, obj_id, cls=None):
- if self.sio is None:
- warnings.warn("NotificationManager was called before a setup was performed!")
- return
- if cls is not None:
- assert isinstance(obj_id, int), \
- f"{cls.__name__} ID must be an integer, but was {type(obj_id)} ({obj_id=})!"
- obj = cls.query.get(obj_id)
- else:
- obj = obj_id
- assert obj is not None, "Object was unexpectedly None!"
- app.logger.debug(f"{name}: {obj}")
- enc = self.json.default(obj)
- self.sio.emit(name, enc)
- def _new_signal(self, signal_name, suffix, *args, **kwargs):
- return partial(self, f'{signal_name}-{suffix}', *args, **kwargs)
- def create(self, suffix, *args, **kwargs) -> Callable:
- return self._new_signal('create', suffix, *args, **kwargs)
- def edit(self, suffix, *args, **kwargs) -> Callable:
- return self._new_signal('edit', suffix, *args, **kwargs)
- def remove(self, suffix, *args, **kwargs) -> Callable:
- return self._new_signal('remove', suffix, *args, **kwargs)
- @classmethod
- def removed(kls, suffix, *args, **kwargs):
- return kls().remove(suffix)(*args, **kwargs)
- @classmethod
- def edited(kls, suffix, *args, **kwargs):
- return kls().edit(suffix)(*args, **kwargs)
- @classmethod
- def created(kls, suffix, *args, **kwargs):
- return kls().create(suffix)(*args, **kwargs)
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
|