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.

entity.py 26KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748
  1. """
  2. kombu.entity
  3. ================
  4. Exchange and Queue declarations.
  5. """
  6. from __future__ import absolute_import
  7. from .abstract import MaybeChannelBound, Object
  8. from .exceptions import ContentDisallowed
  9. from .five import string_t
  10. from .serialization import prepare_accept_content
  11. TRANSIENT_DELIVERY_MODE = 1
  12. PERSISTENT_DELIVERY_MODE = 2
  13. DELIVERY_MODES = {'transient': TRANSIENT_DELIVERY_MODE,
  14. 'persistent': PERSISTENT_DELIVERY_MODE}
  15. __all__ = ['Exchange', 'Queue', 'binding']
  16. def _reprstr(s):
  17. s = repr(s)
  18. if isinstance(s, string_t) and s.startswith("u'"):
  19. return s[2:-1]
  20. return s[1:-1]
  21. def pretty_bindings(bindings):
  22. return '[%s]' % (', '.join(map(str, bindings)))
  23. class Exchange(MaybeChannelBound):
  24. """An Exchange declaration.
  25. :keyword name: See :attr:`name`.
  26. :keyword type: See :attr:`type`.
  27. :keyword channel: See :attr:`channel`.
  28. :keyword durable: See :attr:`durable`.
  29. :keyword auto_delete: See :attr:`auto_delete`.
  30. :keyword delivery_mode: See :attr:`delivery_mode`.
  31. :keyword arguments: See :attr:`arguments`.
  32. .. attribute:: name
  33. Name of the exchange. Default is no name (the default exchange).
  34. .. attribute:: type
  35. *This description of AMQP exchange types was shamelessly stolen
  36. from the blog post `AMQP in 10 minutes: Part 4`_ by
  37. Rajith Attapattu. Reading this article is recommended if you're
  38. new to amqp.*
  39. "AMQP defines four default exchange types (routing algorithms) that
  40. covers most of the common messaging use cases. An AMQP broker can
  41. also define additional exchange types, so see your broker
  42. manual for more information about available exchange types.
  43. * `direct` (*default*)
  44. Direct match between the routing key in the message, and the
  45. routing criteria used when a queue is bound to this exchange.
  46. * `topic`
  47. Wildcard match between the routing key and the routing pattern
  48. specified in the exchange/queue binding. The routing key is
  49. treated as zero or more words delimited by `"."` and
  50. supports special wildcard characters. `"*"` matches a
  51. single word and `"#"` matches zero or more words.
  52. * `fanout`
  53. Queues are bound to this exchange with no arguments. Hence any
  54. message sent to this exchange will be forwarded to all queues
  55. bound to this exchange.
  56. * `headers`
  57. Queues are bound to this exchange with a table of arguments
  58. containing headers and values (optional). A special argument
  59. named "x-match" determines the matching algorithm, where
  60. `"all"` implies an `AND` (all pairs must match) and
  61. `"any"` implies `OR` (at least one pair must match).
  62. :attr:`arguments` is used to specify the arguments.
  63. .. _`AMQP in 10 minutes: Part 4`:
  64. http://bit.ly/amqp-exchange-types
  65. .. attribute:: channel
  66. The channel the exchange is bound to (if bound).
  67. .. attribute:: durable
  68. Durable exchanges remain active when a server restarts. Non-durable
  69. exchanges (transient exchanges) are purged when a server restarts.
  70. Default is :const:`True`.
  71. .. attribute:: auto_delete
  72. If set, the exchange is deleted when all queues have finished
  73. using it. Default is :const:`False`.
  74. .. attribute:: delivery_mode
  75. The default delivery mode used for messages. The value is an integer,
  76. or alias string.
  77. * 1 or `"transient"`
  78. The message is transient. Which means it is stored in
  79. memory only, and is lost if the server dies or restarts.
  80. * 2 or "persistent" (*default*)
  81. The message is persistent. Which means the message is
  82. stored both in-memory, and on disk, and therefore
  83. preserved if the server dies or restarts.
  84. The default value is 2 (persistent).
  85. .. attribute:: arguments
  86. Additional arguments to specify when the exchange is declared.
  87. """
  88. TRANSIENT_DELIVERY_MODE = TRANSIENT_DELIVERY_MODE
  89. PERSISTENT_DELIVERY_MODE = PERSISTENT_DELIVERY_MODE
  90. name = ''
  91. type = 'direct'
  92. durable = True
  93. auto_delete = False
  94. passive = False
  95. delivery_mode = PERSISTENT_DELIVERY_MODE
  96. attrs = (
  97. ('name', None),
  98. ('type', None),
  99. ('arguments', None),
  100. ('durable', bool),
  101. ('passive', bool),
  102. ('auto_delete', bool),
  103. ('delivery_mode', lambda m: DELIVERY_MODES.get(m) or m),
  104. )
  105. def __init__(self, name='', type='', channel=None, **kwargs):
  106. super(Exchange, self).__init__(**kwargs)
  107. self.name = name or self.name
  108. self.type = type or self.type
  109. self.maybe_bind(channel)
  110. def __hash__(self):
  111. return hash('E|%s' % (self.name, ))
  112. def declare(self, nowait=False, passive=None):
  113. """Declare the exchange.
  114. Creates the exchange on the broker.
  115. :keyword nowait: If set the server will not respond, and a
  116. response will not be waited for. Default is :const:`False`.
  117. """
  118. passive = self.passive if passive is None else passive
  119. if self.name:
  120. return self.channel.exchange_declare(
  121. exchange=self.name, type=self.type, durable=self.durable,
  122. auto_delete=self.auto_delete, arguments=self.arguments,
  123. nowait=nowait, passive=passive,
  124. )
  125. def bind_to(self, exchange='', routing_key='',
  126. arguments=None, nowait=False, **kwargs):
  127. """Binds the exchange to another exchange.
  128. :keyword nowait: If set the server will not respond, and the call
  129. will not block waiting for a response. Default is :const:`False`.
  130. """
  131. if isinstance(exchange, Exchange):
  132. exchange = exchange.name
  133. return self.channel.exchange_bind(destination=self.name,
  134. source=exchange,
  135. routing_key=routing_key,
  136. nowait=nowait,
  137. arguments=arguments)
  138. def unbind_from(self, source='', routing_key='',
  139. nowait=False, arguments=None):
  140. """Delete previously created exchange binding from the server."""
  141. if isinstance(source, Exchange):
  142. source = source.name
  143. return self.channel.exchange_unbind(destination=self.name,
  144. source=source,
  145. routing_key=routing_key,
  146. nowait=nowait,
  147. arguments=arguments)
  148. def Message(self, body, delivery_mode=None, priority=None,
  149. content_type=None, content_encoding=None,
  150. properties=None, headers=None):
  151. """Create message instance to be sent with :meth:`publish`.
  152. :param body: Message body.
  153. :keyword delivery_mode: Set custom delivery mode. Defaults
  154. to :attr:`delivery_mode`.
  155. :keyword priority: Message priority, 0 to 9. (currently not
  156. supported by RabbitMQ).
  157. :keyword content_type: The messages content_type. If content_type
  158. is set, no serialization occurs as it is assumed this is either
  159. a binary object, or you've done your own serialization.
  160. Leave blank if using built-in serialization as our library
  161. properly sets content_type.
  162. :keyword content_encoding: The character set in which this object
  163. is encoded. Use "binary" if sending in raw binary objects.
  164. Leave blank if using built-in serialization as our library
  165. properly sets content_encoding.
  166. :keyword properties: Message properties.
  167. :keyword headers: Message headers.
  168. """
  169. properties = {} if properties is None else properties
  170. dm = delivery_mode or self.delivery_mode
  171. properties['delivery_mode'] = \
  172. DELIVERY_MODES[dm] if (dm != 2 and dm != 1) else dm
  173. return self.channel.prepare_message(body,
  174. properties=properties,
  175. priority=priority,
  176. content_type=content_type,
  177. content_encoding=content_encoding,
  178. headers=headers)
  179. def publish(self, message, routing_key=None, mandatory=False,
  180. immediate=False, exchange=None):
  181. """Publish message.
  182. :param message: :meth:`Message` instance to publish.
  183. :param routing_key: Routing key.
  184. :param mandatory: Currently not supported.
  185. :param immediate: Currently not supported.
  186. """
  187. exchange = exchange or self.name
  188. return self.channel.basic_publish(message,
  189. exchange=exchange,
  190. routing_key=routing_key,
  191. mandatory=mandatory,
  192. immediate=immediate)
  193. def delete(self, if_unused=False, nowait=False):
  194. """Delete the exchange declaration on server.
  195. :keyword if_unused: Delete only if the exchange has no bindings.
  196. Default is :const:`False`.
  197. :keyword nowait: If set the server will not respond, and a
  198. response will not be waited for. Default is :const:`False`.
  199. """
  200. return self.channel.exchange_delete(exchange=self.name,
  201. if_unused=if_unused,
  202. nowait=nowait)
  203. def binding(self, routing_key='', arguments=None, unbind_arguments=None):
  204. return binding(self, routing_key, arguments, unbind_arguments)
  205. def __eq__(self, other):
  206. if isinstance(other, Exchange):
  207. return (self.name == other.name and
  208. self.type == other.type and
  209. self.arguments == other.arguments and
  210. self.durable == other.durable and
  211. self.auto_delete == other.auto_delete and
  212. self.delivery_mode == other.delivery_mode)
  213. return NotImplemented
  214. def __ne__(self, other):
  215. return not self.__eq__(other)
  216. def __repr__(self):
  217. return super(Exchange, self).__repr__(str(self))
  218. def __str__(self):
  219. return 'Exchange %s(%s)' % (_reprstr(self.name) or repr(''), self.type)
  220. @property
  221. def can_cache_declaration(self):
  222. return not self.auto_delete
  223. class binding(Object):
  224. """Represents a queue or exchange binding.
  225. :keyword exchange: Exchange to bind to.
  226. :keyword routing_key: Routing key used as binding key.
  227. :keyword arguments: Arguments for bind operation.
  228. :keyword unbind_arguments: Arguments for unbind operation.
  229. """
  230. attrs = (
  231. ('exchange', None),
  232. ('routing_key', None),
  233. ('arguments', None),
  234. ('unbind_arguments', None)
  235. )
  236. def __init__(self, exchange=None, routing_key='',
  237. arguments=None, unbind_arguments=None):
  238. self.exchange = exchange
  239. self.routing_key = routing_key
  240. self.arguments = arguments
  241. self.unbind_arguments = unbind_arguments
  242. def declare(self, channel, nowait=False):
  243. """Declare destination exchange."""
  244. if self.exchange and self.exchange.name:
  245. ex = self.exchange(channel)
  246. ex.declare(nowait=nowait)
  247. def bind(self, entity, nowait=False):
  248. """Bind entity to this binding."""
  249. entity.bind_to(exchange=self.exchange,
  250. routing_key=self.routing_key,
  251. arguments=self.arguments,
  252. nowait=nowait)
  253. def unbind(self, entity, nowait=False):
  254. """Unbind entity from this binding."""
  255. entity.unbind_from(self.exchange,
  256. routing_key=self.routing_key,
  257. arguments=self.unbind_arguments,
  258. nowait=nowait)
  259. def __repr__(self):
  260. return '<binding: %s>' % (self, )
  261. def __str__(self):
  262. return '%s->%s' % (
  263. _reprstr(self.exchange.name), _reprstr(self.routing_key),
  264. )
  265. class Queue(MaybeChannelBound):
  266. """A Queue declaration.
  267. :keyword name: See :attr:`name`.
  268. :keyword exchange: See :attr:`exchange`.
  269. :keyword routing_key: See :attr:`routing_key`.
  270. :keyword channel: See :attr:`channel`.
  271. :keyword durable: See :attr:`durable`.
  272. :keyword exclusive: See :attr:`exclusive`.
  273. :keyword auto_delete: See :attr:`auto_delete`.
  274. :keyword queue_arguments: See :attr:`queue_arguments`.
  275. :keyword binding_arguments: See :attr:`binding_arguments`.
  276. :keyword on_declared: See :attr:`on_declared`
  277. .. attribute:: name
  278. Name of the queue. Default is no name (default queue destination).
  279. .. attribute:: exchange
  280. The :class:`Exchange` the queue binds to.
  281. .. attribute:: routing_key
  282. The routing key (if any), also called *binding key*.
  283. The interpretation of the routing key depends on
  284. the :attr:`Exchange.type`.
  285. * direct exchange
  286. Matches if the routing key property of the message and
  287. the :attr:`routing_key` attribute are identical.
  288. * fanout exchange
  289. Always matches, even if the binding does not have a key.
  290. * topic exchange
  291. Matches the routing key property of the message by a primitive
  292. pattern matching scheme. The message routing key then consists
  293. of words separated by dots (`"."`, like domain names), and
  294. two special characters are available; star (`"*"`) and hash
  295. (`"#"`). The star matches any word, and the hash matches
  296. zero or more words. For example `"*.stock.#"` matches the
  297. routing keys `"usd.stock"` and `"eur.stock.db"` but not
  298. `"stock.nasdaq"`.
  299. .. attribute:: channel
  300. The channel the Queue is bound to (if bound).
  301. .. attribute:: durable
  302. Durable queues remain active when a server restarts.
  303. Non-durable queues (transient queues) are purged if/when
  304. a server restarts.
  305. Note that durable queues do not necessarily hold persistent
  306. messages, although it does not make sense to send
  307. persistent messages to a transient queue.
  308. Default is :const:`True`.
  309. .. attribute:: exclusive
  310. Exclusive queues may only be consumed from by the
  311. current connection. Setting the 'exclusive' flag
  312. always implies 'auto-delete'.
  313. Default is :const:`False`.
  314. .. attribute:: auto_delete
  315. If set, the queue is deleted when all consumers have
  316. finished using it. Last consumer can be cancelled
  317. either explicitly or because its channel is closed. If
  318. there was no consumer ever on the queue, it won't be
  319. deleted.
  320. .. attribute:: queue_arguments
  321. Additional arguments used when declaring the queue.
  322. .. attribute:: binding_arguments
  323. Additional arguments used when binding the queue.
  324. .. attribute:: alias
  325. Unused in Kombu, but applications can take advantage of this.
  326. For example to give alternate names to queues with automatically
  327. generated queue names.
  328. .. attribute:: on_declared
  329. Optional callback to be applied when the queue has been
  330. declared (the ``queue_declare`` operation is complete).
  331. This must be a function with a signature that accepts at least 3
  332. positional arguments: ``(name, messages, consumers)``.
  333. """
  334. ContentDisallowed = ContentDisallowed
  335. name = ''
  336. exchange = Exchange('')
  337. routing_key = ''
  338. durable = True
  339. exclusive = False
  340. auto_delete = False
  341. no_ack = False
  342. attrs = (
  343. ('name', None),
  344. ('exchange', None),
  345. ('routing_key', None),
  346. ('queue_arguments', None),
  347. ('binding_arguments', None),
  348. ('durable', bool),
  349. ('exclusive', bool),
  350. ('auto_delete', bool),
  351. ('no_ack', None),
  352. ('alias', None),
  353. ('bindings', list),
  354. )
  355. def __init__(self, name='', exchange=None, routing_key='',
  356. channel=None, bindings=None, on_declared=None,
  357. **kwargs):
  358. super(Queue, self).__init__(**kwargs)
  359. self.name = name or self.name
  360. self.exchange = exchange or self.exchange
  361. self.routing_key = routing_key or self.routing_key
  362. self.bindings = set(bindings or [])
  363. self.on_declared = on_declared
  364. # allows Queue('name', [binding(...), binding(...), ...])
  365. if isinstance(exchange, (list, tuple, set)):
  366. self.bindings |= set(exchange)
  367. if self.bindings:
  368. self.exchange = None
  369. # exclusive implies auto-delete.
  370. if self.exclusive:
  371. self.auto_delete = True
  372. self.maybe_bind(channel)
  373. def bind(self, channel):
  374. on_declared = self.on_declared
  375. bound = super(Queue, self).bind(channel)
  376. bound.on_declared = on_declared
  377. return bound
  378. def __hash__(self):
  379. return hash('Q|%s' % (self.name, ))
  380. def when_bound(self):
  381. if self.exchange:
  382. self.exchange = self.exchange(self.channel)
  383. def declare(self, nowait=False):
  384. """Declares the queue, the exchange and binds the queue to
  385. the exchange."""
  386. # - declare main binding.
  387. if self.exchange:
  388. self.exchange.declare(nowait)
  389. self.queue_declare(nowait, passive=False)
  390. if self.exchange and self.exchange.name:
  391. self.queue_bind(nowait)
  392. # - declare extra/multi-bindings.
  393. for B in self.bindings:
  394. B.declare(self.channel)
  395. B.bind(self, nowait=nowait)
  396. return self.name
  397. def queue_declare(self, nowait=False, passive=False):
  398. """Declare queue on the server.
  399. :keyword nowait: Do not wait for a reply.
  400. :keyword passive: If set, the server will not create the queue.
  401. The client can use this to check whether a queue exists
  402. without modifying the server state.
  403. """
  404. ret = self.channel.queue_declare(queue=self.name,
  405. passive=passive,
  406. durable=self.durable,
  407. exclusive=self.exclusive,
  408. auto_delete=self.auto_delete,
  409. arguments=self.queue_arguments,
  410. nowait=nowait)
  411. if not self.name:
  412. self.name = ret[0]
  413. if self.on_declared:
  414. self.on_declared(*ret)
  415. return ret
  416. def queue_bind(self, nowait=False):
  417. """Create the queue binding on the server."""
  418. return self.bind_to(self.exchange, self.routing_key,
  419. self.binding_arguments, nowait=nowait)
  420. def bind_to(self, exchange='', routing_key='',
  421. arguments=None, nowait=False):
  422. if isinstance(exchange, Exchange):
  423. exchange = exchange.name
  424. return self.channel.queue_bind(queue=self.name,
  425. exchange=exchange,
  426. routing_key=routing_key,
  427. arguments=arguments,
  428. nowait=nowait)
  429. def get(self, no_ack=None, accept=None):
  430. """Poll the server for a new message.
  431. Must return the message if a message was available,
  432. or :const:`None` otherwise.
  433. :keyword no_ack: If enabled the broker will automatically
  434. ack messages.
  435. :keyword accept: Custom list of accepted content types.
  436. This method provides direct access to the messages in a
  437. queue using a synchronous dialogue, designed for
  438. specific types of applications where synchronous functionality
  439. is more important than performance.
  440. """
  441. no_ack = self.no_ack if no_ack is None else no_ack
  442. message = self.channel.basic_get(queue=self.name, no_ack=no_ack)
  443. if message is not None:
  444. m2p = getattr(self.channel, 'message_to_python', None)
  445. if m2p:
  446. message = m2p(message)
  447. if message.errors:
  448. message._reraise_error()
  449. message.accept = prepare_accept_content(accept)
  450. return message
  451. def purge(self, nowait=False):
  452. """Remove all ready messages from the queue."""
  453. return self.channel.queue_purge(queue=self.name,
  454. nowait=nowait) or 0
  455. def consume(self, consumer_tag='', callback=None,
  456. no_ack=None, nowait=False):
  457. """Start a queue consumer.
  458. Consumers last as long as the channel they were created on, or
  459. until the client cancels them.
  460. :keyword consumer_tag: Unique identifier for the consumer. The
  461. consumer tag is local to a connection, so two clients
  462. can use the same consumer tags. If this field is empty
  463. the server will generate a unique tag.
  464. :keyword no_ack: If enabled the broker will automatically ack
  465. messages.
  466. :keyword nowait: Do not wait for a reply.
  467. :keyword callback: callback called for each delivered message
  468. """
  469. if no_ack is None:
  470. no_ack = self.no_ack
  471. return self.channel.basic_consume(queue=self.name,
  472. no_ack=no_ack,
  473. consumer_tag=consumer_tag or '',
  474. callback=callback,
  475. nowait=nowait)
  476. def cancel(self, consumer_tag):
  477. """Cancel a consumer by consumer tag."""
  478. return self.channel.basic_cancel(consumer_tag)
  479. def delete(self, if_unused=False, if_empty=False, nowait=False):
  480. """Delete the queue.
  481. :keyword if_unused: If set, the server will only delete the queue
  482. if it has no consumers. A channel error will be raised
  483. if the queue has consumers.
  484. :keyword if_empty: If set, the server will only delete the queue
  485. if it is empty. If it is not empty a channel error will be raised.
  486. :keyword nowait: Do not wait for a reply.
  487. """
  488. return self.channel.queue_delete(queue=self.name,
  489. if_unused=if_unused,
  490. if_empty=if_empty,
  491. nowait=nowait)
  492. def queue_unbind(self, arguments=None, nowait=False):
  493. return self.unbind_from(self.exchange, self.routing_key,
  494. arguments, nowait)
  495. def unbind_from(self, exchange='', routing_key='',
  496. arguments=None, nowait=False):
  497. """Unbind queue by deleting the binding from the server."""
  498. return self.channel.queue_unbind(queue=self.name,
  499. exchange=exchange.name,
  500. routing_key=routing_key,
  501. arguments=arguments,
  502. nowait=nowait)
  503. def __eq__(self, other):
  504. if isinstance(other, Queue):
  505. return (self.name == other.name and
  506. self.exchange == other.exchange and
  507. self.routing_key == other.routing_key and
  508. self.queue_arguments == other.queue_arguments and
  509. self.binding_arguments == other.binding_arguments and
  510. self.durable == other.durable and
  511. self.exclusive == other.exclusive and
  512. self.auto_delete == other.auto_delete)
  513. return NotImplemented
  514. def __ne__(self, other):
  515. return not self.__eq__(other)
  516. def __repr__(self):
  517. s = super(Queue, self).__repr__
  518. if self.bindings:
  519. return s('Queue {name} -> {bindings}'.format(
  520. name=_reprstr(self.name),
  521. bindings=pretty_bindings(self.bindings),
  522. ))
  523. return s(
  524. 'Queue {name} -> {0.exchange!r} -> {routing_key}'.format(
  525. self, name=_reprstr(self.name),
  526. routing_key=_reprstr(self.routing_key),
  527. ),
  528. )
  529. @property
  530. def can_cache_declaration(self):
  531. return not self.auto_delete
  532. @classmethod
  533. def from_dict(self, queue, **options):
  534. binding_key = options.get('binding_key') or options.get('routing_key')
  535. e_durable = options.get('exchange_durable')
  536. if e_durable is None:
  537. e_durable = options.get('durable')
  538. e_auto_delete = options.get('exchange_auto_delete')
  539. if e_auto_delete is None:
  540. e_auto_delete = options.get('auto_delete')
  541. q_durable = options.get('queue_durable')
  542. if q_durable is None:
  543. q_durable = options.get('durable')
  544. q_auto_delete = options.get('queue_auto_delete')
  545. if q_auto_delete is None:
  546. q_auto_delete = options.get('auto_delete')
  547. e_arguments = options.get('exchange_arguments')
  548. q_arguments = options.get('queue_arguments')
  549. b_arguments = options.get('binding_arguments')
  550. bindings = options.get('bindings')
  551. exchange = Exchange(options.get('exchange'),
  552. type=options.get('exchange_type'),
  553. delivery_mode=options.get('delivery_mode'),
  554. routing_key=options.get('routing_key'),
  555. durable=e_durable,
  556. auto_delete=e_auto_delete,
  557. arguments=e_arguments)
  558. return Queue(queue,
  559. exchange=exchange,
  560. routing_key=binding_key,
  561. durable=q_durable,
  562. exclusive=options.get('exclusive'),
  563. auto_delete=q_auto_delete,
  564. no_ack=options.get('no_ack'),
  565. queue_arguments=q_arguments,
  566. binding_arguments=b_arguments,
  567. bindings=bindings)
  568. def as_dict(self, recurse=False):
  569. res = super(Queue, self).as_dict(recurse)
  570. if not recurse:
  571. return res
  572. bindings = res.get('bindings')
  573. if bindings:
  574. res['bindings'] = [b.as_dict(recurse=True) for b in bindings]
  575. return res