Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

base.py 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. """
  2. kombu.transport.base
  3. ====================
  4. Base transport interface.
  5. """
  6. from __future__ import absolute_import
  7. import errno
  8. import socket
  9. from amqp.exceptions import RecoverableConnectionError
  10. from kombu.exceptions import ChannelError, ConnectionError
  11. from kombu.message import Message
  12. from kombu.utils import cached_property
  13. from kombu.utils.compat import get_errno
  14. __all__ = ['Message', 'StdChannel', 'Management', 'Transport']
  15. def _LeftBlank(obj, method):
  16. return NotImplementedError(
  17. 'Transport {0.__module__}.{0.__name__} does not implement {1}'.format(
  18. obj.__class__, method))
  19. class StdChannel(object):
  20. no_ack_consumers = None
  21. def Consumer(self, *args, **kwargs):
  22. from kombu.messaging import Consumer
  23. return Consumer(self, *args, **kwargs)
  24. def Producer(self, *args, **kwargs):
  25. from kombu.messaging import Producer
  26. return Producer(self, *args, **kwargs)
  27. def get_bindings(self):
  28. raise _LeftBlank(self, 'get_bindings')
  29. def after_reply_message_received(self, queue):
  30. """reply queue semantics: can be used to delete the queue
  31. after transient reply message received."""
  32. pass
  33. def __enter__(self):
  34. return self
  35. def __exit__(self, *exc_info):
  36. self.close()
  37. class Management(object):
  38. def __init__(self, transport):
  39. self.transport = transport
  40. def get_bindings(self):
  41. raise _LeftBlank(self, 'get_bindings')
  42. class Transport(object):
  43. """Base class for transports."""
  44. Management = Management
  45. #: The :class:`~kombu.Connection` owning this instance.
  46. client = None
  47. #: Set to True if :class:`~kombu.Connection` should pass the URL
  48. #: unmodified.
  49. can_parse_url = False
  50. #: Default port used when no port has been specified.
  51. default_port = None
  52. #: Tuple of errors that can happen due to connection failure.
  53. connection_errors = (ConnectionError, )
  54. #: Tuple of errors that can happen due to channel/method failure.
  55. channel_errors = (ChannelError, )
  56. #: Type of driver, can be used to separate transports
  57. #: using the AMQP protocol (driver_type: 'amqp'),
  58. #: Redis (driver_type: 'redis'), etc...
  59. driver_type = 'N/A'
  60. #: Name of driver library (e.g. 'py-amqp', 'redis', 'beanstalkc').
  61. driver_name = 'N/A'
  62. #: Whether this transports support heartbeats,
  63. #: and that the :meth:`heartbeat_check` method has any effect.
  64. supports_heartbeats = False
  65. #: Set to true if the transport supports the AIO interface.
  66. supports_ev = False
  67. __reader = None
  68. def __init__(self, client, **kwargs):
  69. self.client = client
  70. def establish_connection(self):
  71. raise _LeftBlank(self, 'establish_connection')
  72. def close_connection(self, connection):
  73. raise _LeftBlank(self, 'close_connection')
  74. def create_channel(self, connection):
  75. raise _LeftBlank(self, 'create_channel')
  76. def close_channel(self, connection):
  77. raise _LeftBlank(self, 'close_channel')
  78. def drain_events(self, connection, **kwargs):
  79. raise _LeftBlank(self, 'drain_events')
  80. def heartbeat_check(self, connection, rate=2):
  81. pass
  82. def driver_version(self):
  83. return 'N/A'
  84. def get_heartbeat_interval(self, connection):
  85. return 0
  86. def register_with_event_loop(self, loop):
  87. pass
  88. def unregister_from_event_loop(self, loop):
  89. pass
  90. def verify_connection(self, connection):
  91. return True
  92. def _make_reader(self, connection, timeout=socket.timeout,
  93. error=socket.error, get_errno=get_errno,
  94. _unavail=(errno.EAGAIN, errno.EINTR)):
  95. drain_events = connection.drain_events
  96. def _read(loop):
  97. if not connection.connected:
  98. raise RecoverableConnectionError('Socket was disconnected')
  99. try:
  100. drain_events(timeout=0)
  101. except timeout:
  102. return
  103. except error as exc:
  104. if get_errno(exc) in _unavail:
  105. return
  106. raise
  107. loop.call_soon(_read, loop)
  108. return _read
  109. def qos_semantics_matches_spec(self, connection):
  110. return True
  111. def on_readable(self, connection, loop):
  112. reader = self.__reader
  113. if reader is None:
  114. reader = self.__reader = self._make_reader(connection)
  115. reader(loop)
  116. @property
  117. def default_connection_params(self):
  118. return {}
  119. def get_manager(self, *args, **kwargs):
  120. return self.Management(self)
  121. @cached_property
  122. def manager(self):
  123. return self.get_manager()