NotificationList.py 962 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. import warnings
  2. from functools import partial
  3. from pycs.frontend.notifications.NotificationManager import NotificationManager
  4. class NotificationList:
  5. """
  6. stores notifications to fire them later in the correct thread
  7. """
  8. def __init__(self):
  9. self.__list = []
  10. def add(self, name: str, suffix: str, *args, **kwargs):
  11. """
  12. add a function and parameters to be executed later
  13. :param fun: function
  14. :param params: parameters
  15. """
  16. func = NotificationManager()._new_signal(name, suffix, *args, **kwargs)
  17. self.__list.append(func)
  18. def fire(self, *args, **kwargs):
  19. """
  20. fire all stored events and reset the list
  21. """
  22. for func in self.__list:
  23. try:
  24. func(*args, **kwargs)
  25. except Exception as e:
  26. warnings.warn(f"{func} failed with arguments {args} and {kwargs}: {e}!")
  27. self.__list.clear()