events.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. from abc import ABCMeta, abstractmethod
  2. class EventDispatcher(object):
  3. __metaclass__ = ABCMeta
  4. @abstractmethod
  5. def observe(self, event_name, priority, callback):
  6. """
  7. Register a callback to be invoked when a given event occurs. `observe`
  8. will allow you to listen to events as they occur, but not modify the
  9. event chain or its parameters. If you need to modify an event, use
  10. the `intercept` method. `observe` is a simplified case of `intercept`,
  11. and receives a simpler list of parameters in its callback.
  12. :type event_name: str
  13. :param event_name: The name of the event to which you are subscribing
  14. the callback function. Accepts wildcard parameters.
  15. :type priority: int
  16. :param priority: The priority that this handler should be given.
  17. When the event is emitted, all handlers will be run in order of
  18. priority.
  19. :type callback: function
  20. :param callback: The callback function that should be called with
  21. the parameters given at when the even is emitted.
  22. """
  23. pass
  24. @abstractmethod
  25. def intercept(self, event_name, priority, callback):
  26. """
  27. Register a callback to be invoked when a given event occurs. Intercept
  28. will allow you to both observe events and modify the event chain and
  29. its parameters. If you only want to observe an event, use the `observe`
  30. method. Intercept and `observe` only differ in what parameters the
  31. callback receives, with intercept receiving additional parameters to
  32. allow controlling the event chain.
  33. :type event_name: str
  34. :param event_name: The name of the event to which you are subscribing
  35. the callback function. Accepts wildcard parameters.
  36. :type priority: int
  37. :param priority: The priority that this handler should be given.
  38. When the event is emitted, all handlers will be run in order of
  39. priority.
  40. :type callback: function
  41. :param callback: The callback function that should be called with
  42. the parameters given at when the even is emitted.
  43. """
  44. pass
  45. @abstractmethod
  46. def call(self, event_name, priority, callback, **kwargs):
  47. """
  48. Raises an event while registering a given callback
  49. :type event_name: str
  50. :param event_name: The name of the event to which you are subscribing
  51. the callback function. Accepts wildcard parameters.
  52. :type priority: int
  53. :param priority: The priority that this handler should be given.
  54. When the event is emitted, all handlers will be run in order of
  55. priority.
  56. :type callback: function
  57. :param callback: The callback function that should be called with
  58. the parameters given at when the even is emitted.
  59. """
  60. pass