middleware.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. from abc import ABCMeta, abstractmethod
  2. class Middleware(object):
  3. """
  4. Provides a mechanism for grouping related event handlers together, to
  5. provide logically cohesive middleware. The middleware class allows event
  6. handlers to subscribe to events through the install method, and unsubscribe
  7. through the uninstall method. This allows event handlers to be added and
  8. removed as a group. The event handler implementations will also typically
  9. live inside the middleware class. For example, LoggingMiddleware may
  10. register multiple event handlers to log data before and after calls.
  11. ResourceTrackingMiddleware may track all objects that are created or
  12. deleted.
  13. """
  14. __metaclass__ = ABCMeta
  15. @abstractmethod
  16. def install(self, provider):
  17. """
  18. Use this method to subscribe all event handlers that are part of this
  19. middleware. The install method will be called when the middleware is
  20. first added to a MiddleWareManager.
  21. :type provider: :class:`.Provider`
  22. :param provider: The provider that this middleware belongs to
  23. """
  24. pass
  25. @abstractmethod
  26. def uninstall(self, provider):
  27. """
  28. Use this method to unsubscribe all event handlers for this middleware.
  29. """
  30. pass
  31. class MiddlewareManager(object):
  32. """
  33. Provides a mechanism for tracking a list of installed middleware
  34. """
  35. __metaclass__ = ABCMeta
  36. @abstractmethod
  37. def add(self, middleware):
  38. """
  39. Use this method to add middleware to this middleware manager.
  40. :type middleware: :class:`.Middleware`
  41. :param middleware: The middleware implementation
  42. """
  43. pass
  44. @abstractmethod
  45. def remove(self, middleware):
  46. """
  47. Use this method to remove this middleware from the middleware manager.
  48. """
  49. pass