diff --git a/coolamqp/attaches/__init__.py b/coolamqp/attaches/__init__.py index f13c492ef7a368f9ef406e99430f838306200aa6..7c8a8a569499f9c85156f85a466f0faca47bc984 100644 --- a/coolamqp/attaches/__init__.py +++ b/coolamqp/attaches/__init__.py @@ -1,5 +1,6 @@ # coding=UTF-8 from __future__ import absolute_import, division, print_function + """ Attaches are components that attach to an coolamqp.uplink.Connection and perform some duties These duties almost always require allocating a channel. A base class - Channeler - is provided to faciliate that. @@ -15,4 +16,4 @@ EVERYTHING HERE IS CALLED BY LISTENER THREAD UNLESS STATED OTHERWISE. from coolamqp.attaches.consumer import Consumer, BodyReceiveMode from coolamqp.attaches.publisher import Publisher from coolamqp.attaches.agroup import AttacheGroup -from coolamqp.attaches.declarer import Declarer \ No newline at end of file +from coolamqp.attaches.declarer import Declarer diff --git a/coolamqp/attaches/agroup.py b/coolamqp/attaches/agroup.py index 97838abbe35d1b0d079852c3c4ed9613639a1b05..1433f7f5363b29f1f9043670763c61499b58e8e6 100644 --- a/coolamqp/attaches/agroup.py +++ b/coolamqp/attaches/agroup.py @@ -5,9 +5,8 @@ This is an attache that attaches multiple attaches. It evicts cancelled attaches. """ from __future__ import print_function, absolute_import, division -import six + import logging -import weakref logger = logging.getLogger(__name__) diff --git a/coolamqp/attaches/channeler.py b/coolamqp/attaches/channeler.py index 4c8f30c63696925b332356b38144fdf08131b75a..bf712755743c3da5c09e780b9a79219ef718624b 100644 --- a/coolamqp/attaches/channeler.py +++ b/coolamqp/attaches/channeler.py @@ -4,15 +4,14 @@ Base class for consumer or publisher with the capabiility to set up and tear down channels """ from __future__ import print_function, absolute_import, division -import six -from coolamqp.framing.frames import AMQPMethodFrame, AMQPBodyFrame, AMQPHeaderFrame -from coolamqp.framing.definitions import ChannelOpen, ChannelOpenOk, BasicConsume, \ - BasicConsumeOk, QueueDeclare, QueueDeclareOk, ExchangeDeclare, ExchangeDeclareOk, \ - QueueBind, QueueBindOk, ChannelClose, ChannelCloseOk, BasicCancel, BasicDeliver, \ - BasicAck, BasicReject, ACCESS_REFUSED, RESOURCE_LOCKED, BasicCancelOk -from coolamqp.uplink import HeaderOrBodyWatch, MethodWatch + import logging +from coolamqp.framing.definitions import ChannelOpen, ChannelOpenOk, \ + ChannelClose, ChannelCloseOk, BasicCancel, \ + BasicCancelOk +from coolamqp.framing.frames import AMQPMethodFrame + ST_OFFLINE = 0 # Consumer is *not* consuming, no setup attempts are being made ST_SYNCING = 1 # A process targeted at consuming has been started ST_ONLINE = 2 # Consumer is declared all right @@ -136,7 +135,8 @@ class Channeler(Attache): self.on_operational(False) self.state = ST_OFFLINE - if not isinstance(payload, (ChannelClose, ChannelCloseOk)) and (payload is not None): + if not isinstance(payload, (ChannelClose, ChannelCloseOk)) and ( + payload is not None): # I do not know how to handle that! return @@ -157,7 +157,8 @@ class Channeler(Attache): self.channel_id = None if isinstance(payload, ChannelClose): - logger.debug('Channel closed: %s %s', payload.reply_code, payload.reply_text.tobytes()) + logger.debug('Channel closed: %s %s', payload.reply_code, + payload.reply_text.tobytes()) def methods(self, payloads): """ @@ -171,7 +172,8 @@ class Channeler(Attache): if self.channel_id is None: return # advanced teardown xD - frames = [AMQPMethodFrame(self.channel_id, payload) for payload in payloads] + frames = [AMQPMethodFrame(self.channel_id, payload) for payload in + payloads] self.connection.send(frames) def method(self, payload): @@ -182,7 +184,8 @@ class Channeler(Attache): """ self.methods([payload]) - def method_and_watch(self, method_payload, method_classes_to_watch, callable): + def method_and_watch(self, method_payload, method_classes_to_watch, + callable): """ Syntactic sugar for @@ -192,7 +195,8 @@ class Channeler(Attache): callable) """ assert self.channel_id is not None - self.connection.method_and_watch(self.channel_id, method_payload, method_classes_to_watch, callable) + self.connection.method_and_watch(self.channel_id, method_payload, + method_classes_to_watch, callable) def on_setup(self, payload): """ @@ -212,7 +216,8 @@ class Channeler(Attache): To be called by on_close, when it needs to be notified just one more time. """ - self.connection.watch_for_method(self.channel_id, (ChannelClose, ChannelCloseOk, BasicCancel, BasicCancelOk), + self.connection.watch_for_method(self.channel_id, ( + ChannelClose, ChannelCloseOk, BasicCancel, BasicCancelOk), self.on_close, on_fail=self.on_close) diff --git a/coolamqp/attaches/consumer.py b/coolamqp/attaches/consumer.py index 32085bdfb53955dda7ae34e29f72752b2b4b84d7..5deb6bb99cae36061922644f2c7b2e5f31f78eb6 100644 --- a/coolamqp/attaches/consumer.py +++ b/coolamqp/attaches/consumer.py @@ -1,20 +1,22 @@ # coding=UTF-8 from __future__ import absolute_import, division, print_function -import six + import io import logging import uuid -import warnings -from coolamqp.framing.frames import AMQPBodyFrame, AMQPHeaderFrame + +from concurrent.futures import Future + +from coolamqp.attaches.channeler import Channeler, ST_ONLINE, ST_OFFLINE +from coolamqp.exceptions import AMQPError from coolamqp.framing.definitions import ChannelOpenOk, BasicConsume, \ - BasicConsumeOk, QueueDeclare, QueueDeclareOk, ExchangeDeclare, ExchangeDeclareOk, \ + BasicConsumeOk, QueueDeclare, QueueDeclareOk, ExchangeDeclare, \ + ExchangeDeclareOk, \ QueueBind, QueueBindOk, ChannelClose, BasicDeliver, BasicCancel, \ BasicAck, BasicReject, RESOURCE_LOCKED, BasicCancelOk, BasicQos, BasicQosOk -from coolamqp.uplink import HeaderOrBodyWatch, MethodWatch -from concurrent.futures import Future +from coolamqp.framing.frames import AMQPBodyFrame, AMQPHeaderFrame from coolamqp.objects import Callable -from coolamqp.attaches.channeler import Channeler, ST_ONLINE, ST_OFFLINE -from coolamqp.exceptions import AMQPError +from coolamqp.uplink import HeaderOrBodyWatch, MethodWatch logger = logging.getLogger(__name__) @@ -73,7 +75,8 @@ class Consumer(Channeler): """ - def __init__(self, queue, on_message, no_ack=True, qos=None, cancel_on_failure=False, + def __init__(self, queue, on_message, no_ack=True, qos=None, + cancel_on_failure=False, future_to_notify=None, fail_on_first_time_resource_locked=False, body_receive_mode=BodyReceiveMode.BYTES @@ -137,8 +140,10 @@ class Consumer(Channeler): self.consumer_tag = None - self.on_cancel = Callable(oneshots=True) #: public, called on cancel for any reason - self.on_broker_cancel = Callable(oneshots=True) #: public, called on Customer Cancel Notification (RabbitMQ) + self.on_cancel = Callable( + oneshots=True) #: public, called on cancel for any reason + self.on_broker_cancel = Callable( + oneshots=True) #: public, called on Customer Cancel Notification (RabbitMQ) def set_qos(self, prefetch_size, prefetch_count): """ @@ -212,7 +217,8 @@ class Consumer(Channeler): """ if self.cancel_on_failure and (not self.cancelled): - logger.debug('Consumer is cancel_on_failure and failure seen, True->cancelled') + logger.debug( + 'Consumer is cancel_on_failure and failure seen, True->cancelled') self.cancelled = True self.on_cancel() @@ -231,7 +237,8 @@ class Consumer(Channeler): # on_close is a one_shot watch. We need to re-register it now. self.register_on_close_watch() - self.methods([BasicCancelOk(payload.consumer_tag), ChannelClose(0, b'Received basic.cancel', 0, 0)]) + self.methods([BasicCancelOk(payload.consumer_tag), + ChannelClose(0, b'Received basic.cancel', 0, 0)]) self.cancelled = True # wasn't I? self.on_cancel() self.on_broker_cancel() @@ -268,7 +275,8 @@ class Consumer(Channeler): old_con = self.connection - super(Consumer, self).on_close(payload) # this None's self.connection and returns port + super(Consumer, self).on_close( + payload) # this None's self.connection and returns port self.fail_on_first_time_resource_locked = False if self.future_to_notify_on_dead: # notify it was cancelled @@ -351,7 +359,8 @@ class Consumer(Channeler): if self.queue.exchange is not None: self.method_and_watch( QueueBind( - self.queue.name, self.queue.exchange.name.encode('utf8'), + self.queue.name, + self.queue.exchange.name.encode('utf8'), b'', False, []), QueueBindOk, self.on_setup @@ -369,10 +378,12 @@ class Consumer(Channeler): else: self.on_setup(BasicQosOk()) # pretend QoS went ok elif isinstance(payload, BasicQosOk): - self.consumer_tag = uuid.uuid4().hex.encode('utf8') # str in py2, unicode in py3 + self.consumer_tag = uuid.uuid4().hex.encode( + 'utf8') # str in py2, unicode in py3 self.method_and_watch( BasicConsume(self.queue.name, self.consumer_tag, - False, self.no_ack, self.queue.exclusive, False, []), + False, self.no_ack, self.queue.exclusive, False, + []), BasicConsumeOk, self.on_setup ) @@ -387,7 +398,8 @@ class Consumer(Channeler): self.connection.watch(self.hb_watch) # multi-shot watches need manual cleanup! - self.deliver_watch = MethodWatch(self.channel_id, BasicDeliver, self.on_delivery) + self.deliver_watch = MethodWatch(self.channel_id, BasicDeliver, + self.on_delivery) self.deliver_watch.oneshot = False self.connection.watch(self.deliver_watch) @@ -544,8 +556,10 @@ class MessageReceiver(object): self.bdeliver.routing_key, self.header.properties, self.bdeliver.delivery_tag, - None if self.consumer.no_ack else self.confirm(self.bdeliver.delivery_tag, True), - None if self.consumer.no_ack else self.confirm(self.bdeliver.delivery_tag, False), + None if self.consumer.no_ack else self.confirm( + self.bdeliver.delivery_tag, True), + None if self.consumer.no_ack else self.confirm( + self.bdeliver.delivery_tag, False), ) self.consumer.on_message(rm) diff --git a/coolamqp/attaches/declarer.py b/coolamqp/attaches/declarer.py index ba254448a71d4f129dcc5481bbf45e98f6d5f8f7..112a402da59e53623d83bc95e64d4d1cdae8b638 100644 --- a/coolamqp/attaches/declarer.py +++ b/coolamqp/attaches/declarer.py @@ -3,16 +3,19 @@ queue.declare, exchange.declare and that shit """ from __future__ import print_function, absolute_import, division -import six + import collections import logging -from coolamqp.framing.definitions import ChannelOpenOk, ExchangeDeclare, ExchangeDeclareOk, QueueDeclare, \ - QueueDeclareOk, ChannelClose, QueueDelete, QueueDeleteOk -from coolamqp.attaches.channeler import Channeler, ST_ONLINE, ST_OFFLINE -from coolamqp.attaches.utils import AtomicTagger, FutureConfirmableRejectable, Synchronized + from concurrent.futures import Future -from coolamqp.objects import Exchange, Queue, Callable + +from coolamqp.attaches.channeler import Channeler, ST_ONLINE +from coolamqp.attaches.utils import Synchronized from coolamqp.exceptions import AMQPError, ConnectionDead +from coolamqp.framing.definitions import ChannelOpenOk, ExchangeDeclare, \ + ExchangeDeclareOk, QueueDeclare, \ + QueueDeclareOk, ChannelClose, QueueDelete, QueueDeleteOk +from coolamqp.objects import Exchange, Queue, Callable logger = logging.getLogger(__name__) @@ -46,13 +49,16 @@ class Operation(object): """Attempt to perform this op.""" obj = self.obj if isinstance(obj, Exchange): - self.declarer.method_and_watch(ExchangeDeclare(self.obj.name.encode('utf8'), obj.type, False, obj.durable, - obj.auto_delete, False, False, []), - (ExchangeDeclareOk, ChannelClose), - self._callback) + self.declarer.method_and_watch( + ExchangeDeclare(self.obj.name.encode('utf8'), obj.type, False, + obj.durable, + obj.auto_delete, False, False, []), + (ExchangeDeclareOk, ChannelClose), + self._callback) elif isinstance(obj, Queue): self.declarer.method_and_watch( - QueueDeclare(obj.name, False, obj.durable, obj.exclusive, obj.auto_delete, False, []), + QueueDeclare(obj.name, False, obj.durable, obj.exclusive, + obj.auto_delete, False, []), (QueueDeclareOk, ChannelClose), self._callback) @@ -66,7 +72,8 @@ class Operation(object): else: # something that had no Future failed. Is it in declared? if self.obj in self.declarer.declared: - self.declarer.declared.remove(self.obj) # todo access not threadsafe + self.declarer.declared.remove( + self.obj) # todo access not threadsafe self.declarer.on_discard(self.obj) else: if self.fut is not None: @@ -83,9 +90,10 @@ class DeleteQueue(Operation): queue = self.obj print('bang') - self.declarer.method_and_watch(QueueDelete(queue.name, False, False, False), - (QueueDeleteOk, ChannelClose), - self._callback) + self.declarer.method_and_watch( + QueueDelete(queue.name, False, False, False), + (QueueDeleteOk, ChannelClose), + self._callback) def _callback(self, payload): print('got', payload) @@ -230,7 +238,8 @@ class Declarer(Channeler, Synchronized): To be called when it's possible that something can be done """ - if (self.state != ST_ONLINE) or len(self.left_to_declare) == 0 or (self.in_process is not None): + if (self.state != ST_ONLINE) or len(self.left_to_declare) == 0 or ( + self.in_process is not None): return self.in_process = self.left_to_declare.popleft() diff --git a/coolamqp/attaches/publisher.py b/coolamqp/attaches/publisher.py index 82d0390e207a0a506e5a3032711d2b367efa0013..9915dbaf381c711cf8395f303d22b44accc46aa9 100644 --- a/coolamqp/attaches/publisher.py +++ b/coolamqp/attaches/publisher.py @@ -12,22 +12,26 @@ from __future__ import absolute_import, division, print_function import collections import logging -import struct -import six import warnings -from coolamqp.framing.definitions import ChannelOpenOk, BasicPublish, Basic, BasicAck -from coolamqp.framing.frames import AMQPMethodFrame, AMQPBodyFrame, AMQPHeaderFrame +import six + +from coolamqp.framing.definitions import ChannelOpenOk, BasicPublish, Basic, \ + BasicAck +from coolamqp.framing.frames import AMQPMethodFrame, AMQPBodyFrame, \ + AMQPHeaderFrame try: # these extensions will be available - from coolamqp.framing.definitions import ConfirmSelect, ConfirmSelectOk, BasicNack + from coolamqp.framing.definitions import ConfirmSelect, ConfirmSelectOk, \ + BasicNack except ImportError: pass from coolamqp.attaches.channeler import Channeler, ST_ONLINE, ST_OFFLINE from coolamqp.uplink import PUBLISHER_CONFIRMS, MethodWatch, FailWatch -from coolamqp.attaches.utils import AtomicTagger, FutureConfirmableRejectable, Synchronized +from coolamqp.attaches.utils import AtomicTagger, FutureConfirmableRejectable, \ + Synchronized from concurrent.futures import Future from coolamqp.objects import Exchange @@ -35,8 +39,9 @@ from coolamqp.objects import Exchange logger = logging.getLogger(__name__) # for holding messages when MODE_CNPUB and link is down -CnpubMessageSendOrder = collections.namedtuple('CnpubMessageSendOrder', ('message', 'exchange_name', - 'routing_key', 'future')) +CnpubMessageSendOrder = collections.namedtuple('CnpubMessageSendOrder', + ('message', 'exchange_name', + 'routing_key', 'future')) # todo what if publisher in MODE_CNPUB fails mid message? they dont seem to be recovered @@ -126,8 +131,11 @@ class Publisher(Channeler, Synchronized): body = body[max_body_size:] self.connection.send([ - AMQPMethodFrame(self.channel_id, BasicPublish(exchange_name, routing_key, False, False)), - AMQPHeaderFrame(self.channel_id, Basic.INDEX, 0, len(message.body), message.properties) + AMQPMethodFrame(self.channel_id, + BasicPublish(exchange_name, routing_key, False, + False)), + AMQPHeaderFrame(self.channel_id, Basic.INDEX, 0, len(message.body), + message.properties) ]) # todo optimize it - if there's only one frame it can with previous send @@ -148,13 +156,14 @@ class Publisher(Channeler, Synchronized): try: msg, xchg, rk, fut = self.messages.popleft() except IndexError: - #todo see docs/casefile-0001 + # todo see docs/casefile-0001 break if not fut.set_running_or_notify_cancel(): continue # cancelled - self.tagger.deposit(self.tagger.get_key(), FutureConfirmableRejectable(fut)) + self.tagger.deposit(self.tagger.get_key(), + FutureConfirmableRejectable(fut)) assert isinstance(xchg, (six.binary_type, six.text_type)) self._pub(msg, xchg, rk) @@ -203,7 +212,8 @@ class Publisher(Channeler, Synchronized): if self.mode == Publisher.MODE_NOACK: # If we are not connected right now, drop the message on the floor and log it with DEBUG if self.state != ST_ONLINE: - logger.debug(u'Publish request, but not connected - dropping the message') + logger.debug( + u'Publish request, but not connected - dropping the message') else: self._pub(message, exchange, routing_key) @@ -223,7 +233,8 @@ class Publisher(Channeler, Synchronized): def on_operational(self, operational): state = {True: u'up', False: u'down'}[operational] - mode = {Publisher.MODE_NOACK: u'noack', Publisher.MODE_CNPUB: u'cnpub'}[self.mode] + mode = {Publisher.MODE_NOACK: u'noack', Publisher.MODE_CNPUB: u'cnpub'}[ + self.mode] logger.info('Publisher %s is %s', mode, state) @@ -232,8 +243,9 @@ class Publisher(Channeler, Synchronized): # Assert that mode is OK if self.mode == Publisher.MODE_CNPUB: if PUBLISHER_CONFIRMS not in self.connection.extensions: - warnings.warn(u'Broker does not support publisher_confirms, refusing to start publisher', - RuntimeWarning) + warnings.warn( + u'Broker does not support publisher_confirms, refusing to start publisher', + RuntimeWarning) self.state = ST_OFFLINE self.critically_failed = True return @@ -245,7 +257,8 @@ class Publisher(Channeler, Synchronized): # the functionality. if self.mode == Publisher.MODE_CNPUB: - self.method_and_watch(ConfirmSelect(False), ConfirmSelectOk, self.on_setup) + self.method_and_watch(ConfirmSelect(False), ConfirmSelectOk, + self.on_setup) elif self.mode == Publisher.MODE_NOACK: # A-OK! Boot it. self.state = ST_ONLINE @@ -263,7 +276,8 @@ class Publisher(Channeler, Synchronized): # now we need to listen for BasicAck and BasicNack - mw = MethodWatch(self.channel_id, (BasicAck, BasicNack), self._on_cnpub_delivery) + mw = MethodWatch(self.channel_id, (BasicAck, BasicNack), + self._on_cnpub_delivery) mw.oneshot = False self.connection.watch(mw) self._mode_cnpub_process_deliveries() diff --git a/coolamqp/attaches/utils.py b/coolamqp/attaches/utils.py index a7e1bee88ee9eb7165ae84ad70d20e520565aa0c..35c915f72d6c7f3290cb43fdf9a8b47c9440ba99 100644 --- a/coolamqp/attaches/utils.py +++ b/coolamqp/attaches/utils.py @@ -1,9 +1,9 @@ # coding=UTF-8 from __future__ import print_function, absolute_import, division -import six + +import functools import logging import threading -import functools logger = logging.getLogger(__name__) @@ -112,7 +112,8 @@ class AtomicTagger(object): i = len(self.tags) - 1 # start index while i > 0: # this will terminate at i=0 - if self.tags[i][0] > tag: # this means we should insert it here... + if self.tags[i][ + 0] > tag: # this means we should insert it here... break i -= 1 # previousl index diff --git a/coolamqp/clustering/__init__.py b/coolamqp/clustering/__init__.py index ca17e3262079602596775eb6f515e5c50bbab2ed..3258579bd1f57b9093aa28746155b5e2b07ea0f0 100644 --- a/coolamqp/clustering/__init__.py +++ b/coolamqp/clustering/__init__.py @@ -12,4 +12,5 @@ logger = logging.getLogger(__name__) __all__ = ('Cluster') from coolamqp.clustering.cluster import Cluster -from coolamqp.clustering.events import MessageReceived, NothingMuch, ConnectionLost +from coolamqp.clustering.events import MessageReceived, NothingMuch, \ + ConnectionLost diff --git a/coolamqp/clustering/events.py b/coolamqp/clustering/events.py index d5fa9ef62ed0e4f612317a256a42e5437132abfa..0e3894251f578b8c221094e6967bef9897358cec 100644 --- a/coolamqp/clustering/events.py +++ b/coolamqp/clustering/events.py @@ -5,10 +5,9 @@ Cluster will emit Events. They mean that something, like, happened. """ from __future__ import print_function, absolute_import, division -import six -import time + import logging -import monotonic + from coolamqp.objects import ReceivedMessage logger = logging.getLogger(__name__) @@ -44,6 +43,8 @@ class MessageReceived(ReceivedMessage, Event): def __init__(self, msg): """:type msg: ReceivedMessage""" - ReceivedMessage.__init__(self, msg.body, msg.exchange_name, msg.routing_key, - properties=msg.properties, delivery_tag=msg.delivery_tag, + ReceivedMessage.__init__(self, msg.body, msg.exchange_name, + msg.routing_key, + properties=msg.properties, + delivery_tag=msg.delivery_tag, ack=msg.ack, nack=msg.nack) diff --git a/coolamqp/exceptions.py b/coolamqp/exceptions.py index be3bdedda8e39d40ec6ada06f5451bea82e5d1f8..18388eefba4b3c0df7b6df0065c73993606bf8c5 100644 --- a/coolamqp/exceptions.py +++ b/coolamqp/exceptions.py @@ -1,10 +1,7 @@ # coding=UTF-8 from __future__ import absolute_import, division, print_function -import six -from coolamqp.framing.definitions import HARD_ERRORS, SOFT_ERRORS, CONNECTION_FORCED, INVALID_PATH, FRAME_ERROR, \ - SYNTAX_ERROR, COMMAND_INVALID, CHANNEL_ERROR, UNEXPECTED_FRAME, RESOURCE_ERROR, NOT_ALLOWED, NOT_IMPLEMENTED, \ - INTERNAL_ERROR, CONTENT_TOO_LARGE, NO_CONSUMERS, ACCESS_REFUSED, NOT_FOUND, RESOURCE_LOCKED, PRECONDITION_FAILED +from coolamqp.framing.definitions import HARD_ERRORS class CoolAMQPError(Exception): diff --git a/coolamqp/framing/base.py b/coolamqp/framing/base.py index 31753c93c5e9e1bc99f6792c1709a492ff626b61..75f74c34614123d9ad76a4daec7043244f17edba 100644 --- a/coolamqp/framing/base.py +++ b/coolamqp/framing/base.py @@ -2,8 +2,6 @@ from __future__ import absolute_import, division, print_function import logging -import six -import struct logger = logging.getLogger(__name__) @@ -14,10 +12,14 @@ BASIC_TYPES = {u'bit': (None, None, "0", None), # special case u'octet': (1, 'B', "b'\\x00'", 1), u'short': (2, 'H', "b'\\x00\\x00'", 2), u'long': (4, 'I', "b'\\x00\\x00\\x00\\x00'", 4), - u'longlong': (8, 'Q', "b'\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00'", 8), - u'timestamp': (8, 'Q', "b'\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00'", 8), - u'table': (None, None, "b'\\x00\\x00\\x00\\x00'", 4), # special case - u'longstr': (None, None, "b'\\x00\\x00\\x00\\x00'", 4), # special case + u'longlong': ( + 8, 'Q', "b'\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00'", 8), + u'timestamp': ( + 8, 'Q', "b'\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00'", 8), + u'table': (None, None, "b'\\x00\\x00\\x00\\x00'", 4), + # special case + u'longstr': (None, None, "b'\\x00\\x00\\x00\\x00'", 4), + # special case u'shortstr': (None, None, "b'\\x00'", 1), # special case } @@ -37,7 +39,8 @@ class AMQPFrame(object): # base class for framing This writes type and channel ID. """ # DO NOT UNCOMMENT buf.write(struct.pack('!BH', self.FRAME_TYPE, self.channel)) - raise NotImplementedError('Please write the frame type and channel in child classes, its faster that way ') + raise NotImplementedError( + 'Please write the frame type and channel in child classes, its faster that way ') @staticmethod def unserialize(channel, payload_as_buffer): @@ -151,7 +154,8 @@ class AMQPMethodPayload(AMQPPayload): :return: int, size of argument section """ if self.IS_CONTENT_STATIC: - return len(self.STATIC_CONTENT) - 4 - 4 - 1 # minus length, class, method, frame_end + return len( + self.STATIC_CONTENT) - 4 - 4 - 1 # minus length, class, method, frame_end raise NotImplementedError() diff --git a/coolamqp/framing/compilation/compile_definitions.py b/coolamqp/framing/compilation/compile_definitions.py index 62a957d53a2ba88897478d87fae438cd1cd6f008..426c5775217432a33f022e0031cf05a047eaa2ec 100644 --- a/coolamqp/framing/compilation/compile_definitions.py +++ b/coolamqp/framing/compilation/compile_definitions.py @@ -7,8 +7,10 @@ from xml.etree import ElementTree import six -from coolamqp.framing.compilation.utilities import get_constants, get_classes, get_domains, \ - name_class, format_method_class_name, format_field_name, ffmt, to_docstring, pythonify_name, to_code_binary, \ +from coolamqp.framing.compilation.utilities import get_constants, get_classes, \ + get_domains, \ + name_class, format_method_class_name, format_field_name, ffmt, to_docstring, \ + pythonify_name, to_code_binary, \ frepr, get_size TYPE_TRANSLATOR = { @@ -24,7 +26,8 @@ TYPE_TRANSLATOR = { } -def compile_definitions(xml_file='resources/amqp0-9-1.extended.xml', out_file='coolamqp/framing/definitions.py'): +def compile_definitions(xml_file='resources/amqp0-9-1.extended.xml', + out_file='coolamqp/framing/definitions.py'): """parse resources/amqp-0-9-1.xml into """ xml = ElementTree.parse(xml_file) @@ -99,13 +102,15 @@ Field = collections.namedtuple('Field', ('name', 'type', 'basic_type', 'reserved con_classes[constant.kind].append(pythonify_name(constant.name)) for constant_kind, constants in con_classes.items(): - line('\n%sS = [%s]', pythonify_name(constant_kind), u', '.join(constants)) + line('\n%sS = [%s]', pythonify_name(constant_kind), + u', '.join(constants)) # get domains domain_to_basic_type = {} line('\n\n\nDOMAIN_TO_BASIC_TYPE = {\n') for domain in get_domains(xml): - line(u' %s: %s,\n', frepr(domain.name), frepr(None if domain.elementary else domain.type)) + line(u' %s: %s,\n', frepr(domain.name), + frepr(None if domain.elementary else domain.type)) domain_to_basic_type[domain.name] = domain.type line('}\n') @@ -119,7 +124,9 @@ Field = collections.namedtuple('Field', ('name', 'type', 'basic_type', 'reserved # Output classes for cls in get_classes(xml): - cls = cls._replace(properties=[p._replace(basic_type=domain_to_basic_type[p.type]) for p in cls.properties]) + cls = cls._replace( + properties=[p._replace(basic_type=domain_to_basic_type[p.type]) for + p in cls.properties]) line('''\nclass %s(AMQPClass): """ @@ -129,10 +136,12 @@ Field = collections.namedtuple('Field', ('name', 'type', 'basic_type', 'reserved INDEX = %s ''', - name_class(cls.name), to_docstring(None, cls.docs), frepr(cls.name), cls.index) + name_class(cls.name), to_docstring(None, cls.docs), + frepr(cls.name), cls.index) if len(cls.properties) > 0: - class_id_to_contentpropertylist[cls.index] = name_class(cls.name) + 'ContentPropertyList' + class_id_to_contentpropertylist[cls.index] = name_class( + cls.name) + 'ContentPropertyList' line('''\nclass %sContentPropertyList(AMQPContentPropertyList): """ @@ -141,15 +150,19 @@ Field = collections.namedtuple('Field', ('name', 'type', 'basic_type', 'reserved FIELDS = [ ''', - name_class(cls.name), to_docstring(None, cls.docs), frepr(cls.name), cls.index, name_class(cls.name)) + name_class(cls.name), to_docstring(None, cls.docs), + frepr(cls.name), cls.index, name_class(cls.name)) - is_static = all(property.basic_type not in ('table', 'longstr', 'shortstr') for property in cls.properties) + is_static = all( + property.basic_type not in ('table', 'longstr', 'shortstr') for + property in cls.properties) for property in cls.properties: if property.basic_type == 'bit': raise ValueError('bit properties are not supported!' ) - line(' Field(%s, %s, %s, %s),\n', frepr(property.name), frepr(property.type), + line(' Field(%s, %s, %s, %s),\n', frepr(property.name), + frepr(property.type), frepr(property.basic_type), repr(property.reserved)) line(''' ] # A dictionary from a zero property list to a class typized with @@ -159,7 +172,8 @@ Field = collections.namedtuple('Field', ('name', 'type', 'basic_type', 'reserved name_class(cls.name)) if any(prop.basic_type == 'bit' for prop in cls.properties): - raise NotImplementedError('I should emit a custom zero_property_list staticmethod :(') + raise NotImplementedError( + 'I should emit a custom zero_property_list staticmethod :(') line(u''' def __new__(self, **kwargs): """ Return a property list. @@ -168,8 +182,10 @@ Field = collections.namedtuple('Field', ('name', 'type', 'basic_type', 'reserved my_props = [prop for prop in cls.properties if (not prop.reserved)] for property in my_props: - line(' :param %s: %s\n', format_field_name(property.name), property.label) - line(' :type %s: %s (AMQP as %s)\n', format_field_name(property.name), + line(' :param %s: %s\n', + format_field_name(property.name), property.label) + line(' :type %s: %s (AMQP as %s)\n', + format_field_name(property.name), TYPE_TRANSLATOR[property.basic_type], property.basic_type) line(' """\n') zpf_len = int(math.ceil(len(cls.properties) // 15)) @@ -187,14 +203,16 @@ Field = collections.namedtuple('Field', ('name', 'type', 'basic_type', 'reserved if field.reserved or field.basic_type == 'bit': pass # zero anyway else: - byte_chunk.append(u"(('%s' in kwargs) << %s)" % (format_field_name(field.name), piece_index)) + byte_chunk.append(u"(('%s' in kwargs) << %s)" % ( + format_field_name(field.name), piece_index)) piece_index -= 1 else: if first_byte: if field.reserved or field.basic_type == 'bit': pass # zero anyway else: - byte_chunk.append(u"int('%s' in kwargs)" % (format_field_name(field.name),)) + byte_chunk.append(u"int('%s' in kwargs)" % ( + format_field_name(field.name),)) else: # this is the "do we need moar flags" section byte_chunk.append(u"kwargs['%s']" % ( @@ -209,7 +227,8 @@ Field = collections.namedtuple('Field', ('name', 'type', 'basic_type', 'reserved fields_remaining -= 1 if len(byte_chunk) > 0: - line(u' %s\n', u' | '.join(byte_chunk)) # We did not finish + line(u' %s\n', + u' | '.join(byte_chunk)) # We did not finish line(u' ])\n zpf = six.binary_type(zpf)\n') line(u''' @@ -237,7 +256,8 @@ Field = collections.namedtuple('Field', ('name', 'type', 'basic_type', 'reserved c = compile_particular_content_property_list_class(zpf, %s.FIELDS) %s.PARTICULAR_CLASSES[zpf] = c return c(**kwargs) -'''.replace('%s', name_class(cls.name) + 'ContentPropertyList').replace('%d', '%s')) +'''.replace('%s', name_class(cls.name) + 'ContentPropertyList').replace('%d', + '%s')) line(u''' @staticmethod @@ -256,14 +276,16 @@ Field = collections.namedtuple('Field', ('name', 'type', 'basic_type', 'reserved if field.reserved or field.basic_type == 'bit': pass # zero else: - byte_chunk.append(u"(('%s' in fields) << %s)" % (format_field_name(field.name), piece_index)) + byte_chunk.append(u"(('%s' in fields) << %s)" % ( + format_field_name(field.name), piece_index)) piece_index -= 1 else: if first_byte: if field.reserved or field.basic_type == 'bit': pass # zero else: - byte_chunk.append(u"int('%s' in kwargs)" % (format_field_name(field.name),)) + byte_chunk.append(u"int('%s' in kwargs)" % ( + format_field_name(field.name),)) else: # this is the "do we need moar flags" section byte_chunk.append(u"kwargs['%s']" % ( @@ -278,7 +300,8 @@ Field = collections.namedtuple('Field', ('name', 'type', 'basic_type', 'reserved fields_remaining -= 1 if len(byte_chunk) > 0: - line(u' %s\n', u' | '.join(byte_chunk)) # We did not finish + line(u' %s\n', + u' | '.join(byte_chunk)) # We did not finish line(u''' ]) zpf = six.binary_type(zpf) @@ -289,7 +312,8 @@ Field = collections.namedtuple('Field', ('name', 'type', 'basic_type', 'reserved c = compile_particular_content_property_list_class(zpf, %s.FIELDS) %s.PARTICULAR_CLASSES[zpf] = c return c -'''.replace("%s", name_class(cls.name) + 'ContentPropertyList').replace('%d', '%s')) +'''.replace("%s", name_class(cls.name) + 'ContentPropertyList').replace('%d', + '%s')) line(u''' @staticmethod @@ -315,27 +339,35 @@ Field = collections.namedtuple('Field', ('name', 'type', 'basic_type', 'reserved %s.PARTICULAR_CLASSES[zpf] = c return c.from_buffer(buf, offset) -'''.replace('%s', name_class(cls.name) + 'ContentPropertyList').replace("%d", "%s")) +'''.replace('%s', name_class(cls.name) + 'ContentPropertyList').replace("%d", + "%s")) # ============================================ Do methods for this class for method in cls.methods: - full_class_name = u'%s%s' % (name_class(cls.name), format_method_class_name(method.name)) + full_class_name = u'%s%s' % ( + name_class(cls.name), format_method_class_name(method.name)) # annotate types - method.fields = [field._replace(basic_type=domain_to_basic_type[field.type]) for field in method.fields] + method.fields = [ + field._replace(basic_type=domain_to_basic_type[field.type]) for + field in method.fields] - non_reserved_fields = [field for field in method.fields if not field.reserved] + non_reserved_fields = [field for field in method.fields if + not field.reserved] is_static = method.is_static() if is_static: static_size = get_size(method.fields) - is_content_static = len([f for f in method.fields if not f.reserved]) == 0 + is_content_static = len( + [f for f in method.fields if not f.reserved]) == 0 if len(non_reserved_fields) == 0: slots = u'' else: - slots = (u', '.join(map(lambda f: frepr(format_field_name(f.name)), non_reserved_fields))) + u', ' + slots = (u', '.join( + map(lambda f: frepr(format_field_name(f.name)), + non_reserved_fields))) + u', ' line('''\nclass %s(AMQPMethodPayload): """ @@ -366,19 +398,24 @@ Field = collections.namedtuple('Field', ('name', 'type', 'basic_type', 'reserved repr(is_content_static) ) - _namify = lambda x: name_class(cls.name) + format_method_class_name(x) + _namify = lambda x: name_class(cls.name) + format_method_class_name( + x) methods_that_are_replies_for[full_class_name] = [] for response in method.response: - methods_that_are_reply_reasons_for[_namify(response)] = full_class_name - methods_that_are_replies_for[full_class_name].append(_namify(response)) + methods_that_are_reply_reasons_for[ + _namify(response)] = full_class_name + methods_that_are_replies_for[full_class_name].append( + _namify(response)) if is_content_static: line(''' STATIC_CONTENT = %s # spans LENGTH, CLASS ID, METHOD ID, ....., FRAME_END ''', - to_code_binary(struct.pack('!LHH', static_size + 4, cls.index, method.index) + \ - method.get_static_body() + \ - struct.pack('!B', FRAME_END))) + to_code_binary( + struct.pack('!LHH', static_size + 4, cls.index, + method.index) + \ + method.get_static_body() + \ + struct.pack('!B', FRAME_END))) # fields if len(method.fields) > 0: @@ -386,7 +423,8 @@ Field = collections.namedtuple('Field', ('name', 'type', 'basic_type', 'reserved line(' FIELDS = [ \n') for field in method.fields: - line(' Field(%s, %s, %s, reserved=%s),\n', frepr(field.name), frepr(field.type), + line(' Field(%s, %s, %s, reserved=%s),\n', + frepr(field.name), frepr(field.type), frepr(field.basic_type), repr(field.reserved)) line(' ]\n') @@ -396,7 +434,9 @@ Field = collections.namedtuple('Field', ('name', 'type', 'basic_type', 'reserved """ Create frame %s ''', - u', '.join(['self'] + [format_field_name(field.name) for field in non_reserved_fields]), + u', '.join( + ['self'] + [format_field_name(field.name) for field in + non_reserved_fields]), cls.name + '.' + method.name, ) @@ -405,23 +445,28 @@ Field = collections.namedtuple('Field', ('name', 'type', 'basic_type', 'reserved for field in non_reserved_fields: if (field.label is not None) or (field.docs is not None): - line(' :param %s: %s\n', format_field_name(field.name), - to_docstring(field.label, field.docs, prefix=12, blank=False)) + line(' :param %s: %s\n', + format_field_name(field.name), + to_docstring(field.label, field.docs, prefix=12, + blank=False)) - line(' :type %s: %s (%s in AMQP)\n', format_field_name(field.name), + line(' :type %s: %s (%s in AMQP)\n', + format_field_name(field.name), TYPE_TRANSLATOR[field.basic_type], field.type) line(' """\n') for field in non_reserved_fields: - line(' self.%s = %s\n', format_field_name(field.name), format_field_name(field.name)) + line(' self.%s = %s\n', format_field_name(field.name), + format_field_name(field.name)) if len(non_reserved_fields) == 0: line('\n') # end if not is_content_static: - from coolamqp.framing.compilation.textcode_fields import get_serializer, get_counter, get_from_buffer + from coolamqp.framing.compilation.textcode_fields import \ + get_serializer, get_counter, get_from_buffer line('\n def write_arguments(self, buf):\n') line(get_serializer(method.fields, 'self.', 2)) @@ -433,11 +478,14 @@ Field = collections.namedtuple('Field', ('name', 'type', 'basic_type', 'reserved offset = start_offset ''') - line(get_from_buffer(method.fields, '', 2, remark=(method.name == 'deliver'))) + line(get_from_buffer(method.fields, '', 2, + remark=(method.name == 'deliver'))) line(" return %s(%s)", full_class_name, - u', '.join(format_field_name(field.name) for field in method.fields if not field.reserved)) + u', '.join( + format_field_name(field.name) for field in method.fields if + not field.reserved)) line('\n\n') diff --git a/coolamqp/framing/compilation/content_property.py b/coolamqp/framing/compilation/content_property.py index d533a9c60bdc71b5d0e77fdff64fc12053d44891..93a8cfdaca3d6e82f60fc2e90709e68174d41089 100644 --- a/coolamqp/framing/compilation/content_property.py +++ b/coolamqp/framing/compilation/content_property.py @@ -3,11 +3,9 @@ from __future__ import absolute_import, division, print_function """Generate serializers/unserializers/length getters for given property_flags""" import six -import struct import logging -from coolamqp.framing.compilation.textcode_fields import get_counter, get_from_buffer, get_serializer -from coolamqp.framing.base import AMQPContentPropertyList -from coolamqp.framing.field_table import enframe_table, deframe_table, frame_table_size +from coolamqp.framing.compilation.textcode_fields import get_counter, \ + get_from_buffer, get_serializer logger = logging.getLogger(__name__) @@ -40,7 +38,8 @@ def _compile_particular_content_property_list_class(zpf, fields): zpf_length = len(zpf) # 1 here does not mean that field is present. All bit fields are present, but 0 in a ZPF. Fix this. - zpf_bits = [zpf_bit or field.type == 'bit' for zpf_bit, field in zip(zpf_bits, fields)] + zpf_bits = [zpf_bit or field.type == 'bit' for zpf_bit, field in + zip(zpf_bits, fields)] mod = [u'''class ParticularContentTypeList(AMQPContentPropertyList): """ @@ -48,7 +47,8 @@ def _compile_particular_content_property_list_class(zpf, fields): '''] for field in fields: - mod.append(u' * %s::%s' % (format_field_name(field.name), field.type)) + mod.append( + u' * %s::%s' % (format_field_name(field.name), field.type)) if field.reserved: mod.append(u' (reserved)') mod.append(u'\n') @@ -57,7 +57,8 @@ def _compile_particular_content_property_list_class(zpf, fields): if not x.startswith('b'): x = 'b' + x - present_fields = [field for field, present in zip(fields, zpf_bits) if present] + present_fields = [field for field, present in zip(fields, zpf_bits) if + present] mod.append(u''' """ @@ -66,7 +67,9 @@ def _compile_particular_content_property_list_class(zpf, fields): if len(present_fields) == 0: slots = u'' else: - slots = (u', '.join((u"u'%s'" % format_field_name(field.name) for field in present_fields))) + u', ' + slots = (u', '.join( + (u"u'%s'" % format_field_name(field.name) for field in + present_fields))) + u', ' mod.append(u''' __slots__ = (%s) @@ -83,7 +86,8 @@ def _compile_particular_content_property_list_class(zpf, fields): ''' % (u', '.join(format_field_name(field.name) for field in present_fields))) for field in present_fields: - mod.append(u' self.%s = %s\n'.replace(u'%s', format_field_name(field.name))) + mod.append(u' self.%s = %s\n'.replace(u'%s', format_field_name( + field.name))) # Let's do write_to mod.append(u'\n def write_to(self, buf):\n') @@ -99,16 +103,20 @@ def _compile_particular_content_property_list_class(zpf, fields): # from_buffer # note that non-bit values mod.append(u' @classmethod\n') - mod.append(u' def from_buffer(cls, buf, start_offset):\n offset = start_offset + %s\n' % (zpf_length,)) + mod.append( + u' def from_buffer(cls, buf, start_offset):\n offset = start_offset + %s\n' % ( + zpf_length,)) mod.append(get_from_buffer( present_fields , prefix='', indent_level=2)) mod.append(u' return cls(%s)\n' % - u', '.join(format_field_name(field.name) for field in present_fields)) + u', '.join( + format_field_name(field.name) for field in present_fields)) # get_size mod.append(u'\n def get_size(self):\n') - mod.append(get_counter(present_fields, prefix=u'self.', indent_level=2)[:-1]) # skip eol + mod.append(get_counter(present_fields, prefix=u'self.', indent_level=2)[ + :-1]) # skip eol mod.append(u' + %s\n' % (zpf_length,)) # account for pf length return u''.join(mod) diff --git a/coolamqp/framing/compilation/textcode_fields.py b/coolamqp/framing/compilation/textcode_fields.py index a20969c3ec812dccad2fcb876c8085b5e1d079f3..88d99d9ebe53ec6e88bfd3bff437d34ee32b570b 100644 --- a/coolamqp/framing/compilation/textcode_fields.py +++ b/coolamqp/framing/compilation/textcode_fields.py @@ -16,10 +16,11 @@ a module that has following ok: """ from __future__ import absolute_import, division, print_function + import math -from coolamqp.framing.base import BASIC_TYPES, DYNAMIC_BASIC_TYPES -from coolamqp.framing.compilation.utilities import format_field_name, get_size +from coolamqp.framing.base import BASIC_TYPES +from coolamqp.framing.compilation.utilities import format_field_name def get_counter(fields, prefix=u'', indent_level=2): @@ -64,7 +65,8 @@ def get_counter(fields, prefix=u'', indent_level=2): if bits > 0: # sync bits accumulator += int(math.ceil(bits / 8)) - return (u' ' * indent_level) + u'return ' + (u' + '.join([str(accumulator)] + parts)) + u'\n' + return (u' ' * indent_level) + u'return ' + ( + u' + '.join([str(accumulator)] + parts)) + u'\n' def get_from_buffer(fields, prefix='', indent_level=2, remark=False): @@ -115,7 +117,8 @@ def get_from_buffer(fields, prefix='', indent_level=2, remark=False): return fffnames = [a for a, b in to_struct if a != u'_'] # skip reserved ffffmts = [b for a, b in to_struct] - emit("%s, = struct.unpack_from('!%s', buf, offset)", u', '.join(fffnames), u''.join(ffffmts)) + emit("%s, = struct.unpack_from('!%s', buf, offset)", + u', '.join(fffnames), u''.join(ffffmts)) emit("offset += %s", ln['ln']) ln['ln'] = 0 del to_struct[:] @@ -137,7 +140,8 @@ def get_from_buffer(fields, prefix='', indent_level=2, remark=False): assert len(bits) == 0 if field.reserved: - to_struct.append((u'_', '%sx' % (BASIC_TYPES[field.basic_type][0],))) + to_struct.append( + (u'_', '%sx' % (BASIC_TYPES[field.basic_type][0],))) else: to_struct.append((fieldname, BASIC_TYPES[field.basic_type][1])) @@ -203,12 +207,14 @@ def get_serializer(fields, prefix='', indent_level=2): else: for bit_name, modif in zip(bits, range(8)): if bit_name != 'False': - p.append('(' + bit_name + ' << %s)' % (modif,)) # yes you can << bools + p.append('(' + bit_name + ' << %s)' % ( + modif,)) # yes you can << bools format_args.append(u' | '.join(p)) del bits[:] def emit_single_struct_pack(): - emit("buf.write(struct.pack('!%s', %s))", u''.join(formats), u', '.join(format_args)) + emit("buf.write(struct.pack('!%s', %s))", u''.join(formats), + u', '.join(format_args)) del formats[:] del format_args[:] diff --git a/coolamqp/framing/compilation/utilities.py b/coolamqp/framing/compilation/utilities.py index d332441c90b8f86f9fc81aa0d3f610bd70c29655..dd93515cd22ab58f317ad93ba164210bfc0967a5 100644 --- a/coolamqp/framing/compilation/utilities.py +++ b/coolamqp/framing/compilation/utilities.py @@ -10,16 +10,21 @@ from coolamqp.framing.base import BASIC_TYPES, DYNAMIC_BASIC_TYPES # docs may be None -Constant = namedtuple('Constant', ('name', 'value', 'kind', 'docs')) # kind is AMQP constant class # value is int -Field = namedtuple('Field', ('name', 'type', 'label', 'docs', 'reserved', 'basic_type')) # reserved is bool +Constant = namedtuple('Constant', ( +'name', 'value', 'kind', 'docs')) # kind is AMQP constant class # value is int +Field = namedtuple('Field', ( +'name', 'type', 'label', 'docs', 'reserved', 'basic_type')) # reserved is bool # synchronous is bool, constant is bool # repponse is a list of method.name -Class_ = namedtuple('Class_', ('name', 'index', 'docs', 'methods', 'properties')) # label is int -Domain = namedtuple('Domain', ('name', 'type', 'elementary')) # elementary is bool +Class_ = namedtuple('Class_', ( +'name', 'index', 'docs', 'methods', 'properties')) # label is int +Domain = namedtuple('Domain', + ('name', 'type', 'elementary')) # elementary is bool class Method(object): - def __init__(self, name, synchronous, index, label, docs, fields, response, sent_by_client, sent_by_server): + def __init__(self, name, synchronous, index, label, docs, fields, response, + sent_by_client, sent_by_server): self.name = name self.synchronous = synchronous self.index = index @@ -68,7 +73,8 @@ def get_size(fields): # assume all fields have static length if field.basic_type == 'bit': bits += 1 else: - size += len(BASIC_TYPES[field.basic_type][2]) # default minimum entry + size += len( + BASIC_TYPES[field.basic_type][2]) # default minimum entry else: size += BASIC_TYPES[field.basic_type][0] @@ -100,7 +106,8 @@ def for_domain(elem): def for_field(elem): # for <field> in <method> """Parse method. Return fields""" a = elem.attrib - return Field(six.text_type(a['name']), a['domain'] if 'domain' in a else a['type'], + return Field(six.text_type(a['name']), + a['domain'] if 'domain' in a else a['type'], a.get('label', None), get_docs(elem), a.get('reserved', '0') == '1', @@ -110,29 +117,38 @@ def for_field(elem): # for <field> in <method> def for_method(elem): # for <method> """Parse class, return methods""" a = elem.attrib - return Method(six.text_type(a['name']), bool(int(a.get('synchronous', '0'))), int(a['index']), a.get('label', None), + return Method(six.text_type(a['name']), + bool(int(a.get('synchronous', '0'))), int(a['index']), + a.get('label', None), get_docs(elem), - [for_field(fie) for fie in elem.getchildren() if fie.tag == 'field'], + [for_field(fie) for fie in elem.getchildren() if + fie.tag == 'field'], [e.attrib['name'] for e in elem.findall('response')], # if chassis=server that means server has to accept it - any([e.attrib.get('name', '') == 'server' for e in elem.getchildren() if e.tag == 'chassis']), - any([e.attrib.get('name', '') == 'client' for e in elem.getchildren() if e.tag == 'chassis']) + any([e.attrib.get('name', '') == 'server' for e in + elem.getchildren() if e.tag == 'chassis']), + any([e.attrib.get('name', '') == 'client' for e in + elem.getchildren() if e.tag == 'chassis']) ) def for_class(elem): # for <class> """Parse XML, return classes""" a = elem.attrib - methods = sorted([for_method(me) for me in elem.getchildren() if me.tag == 'method'], - key=lambda m: (m.name.strip('-')[0], -len(m.response))) - return Class_(six.text_type(a['name']), int(a['index']), get_docs(elem) or a['label'], methods, - [for_field(e) for e in elem.getchildren() if e.tag == 'field']) + methods = sorted( + [for_method(me) for me in elem.getchildren() if me.tag == 'method'], + key=lambda m: (m.name.strip('-')[0], -len(m.response))) + return Class_(six.text_type(a['name']), int(a['index']), + get_docs(elem) or a['label'], methods, + [for_field(e) for e in elem.getchildren() if + e.tag == 'field']) def for_constant(elem): # for <constant> """Parse XML, return constants""" a = elem.attrib - return Constant(a['name'], int(a['value']), a.get('class', ''), get_docs(elem)) + return Constant(a['name'], int(a['value']), a.get('class', ''), + get_docs(elem)) def get_constants(xml): @@ -168,7 +184,8 @@ def name_class(classname): def format_method_class_name(methodname): if '-' in methodname: i = methodname.find('-') - return methodname[0:i].capitalize() + methodname[i + 1].upper() + methodname[i + 2:] + return methodname[0:i].capitalize() + methodname[ + i + 1].upper() + methodname[i + 2:] else: return methodname.capitalize() @@ -185,7 +202,8 @@ def frepr(p, sop=six.text_type): p = sop(p) s = repr(p) - if isinstance(p, (six.binary_type, six.text_type)) and not s.startswith('u'): + if isinstance(p, (six.binary_type, six.text_type)) and not s.startswith( + 'u'): return ('u' if sop == six.text_type else 'b') + s else: return s @@ -214,9 +232,11 @@ def try_to_int(p): return p -def to_docstring(label, doc, prefix=4, blank=True): # output a full docstring section +def to_docstring(label, doc, prefix=4, + blank=True): # output a full docstring section label = [] if label is None else [label] - doc = [] if doc is None else [q.strip() for q in doc.split(u'\n') if len(q.strip()) > 0] + doc = [] if doc is None else [q.strip() for q in doc.split(u'\n') if + len(q.strip()) > 0] pre = u' ' * prefix doc = label + doc diff --git a/coolamqp/framing/definitions.py b/coolamqp/framing/definitions.py index e472c3d376f2de66819fb6592677c6cfe8b5dd92..4a8cb351bb0cc54caa98dccfb3770b7fc0653922 100644 --- a/coolamqp/framing/definitions.py +++ b/coolamqp/framing/definitions.py @@ -1,5 +1,6 @@ # coding=UTF-8 from __future__ import print_function, absolute_import + """ A Python version of the AMQP machine-readable specification. @@ -25,13 +26,17 @@ Only thing that isn't are field names in tables. import struct, collections, logging, six -from coolamqp.framing.base import AMQPClass, AMQPMethodPayload, AMQPContentPropertyList -from coolamqp.framing.field_table import enframe_table, deframe_table, frame_table_size -from coolamqp.framing.compilation.content_property import compile_particular_content_property_list_class +from coolamqp.framing.base import AMQPClass, AMQPMethodPayload, \ + AMQPContentPropertyList +from coolamqp.framing.field_table import enframe_table, deframe_table, \ + frame_table_size +from coolamqp.framing.compilation.content_property import \ + compile_particular_content_property_list_class logger = logging.getLogger(__name__) -Field = collections.namedtuple('Field', ('name', 'type', 'basic_type', 'reserved')) +Field = collections.namedtuple('Field', + ('name', 'type', 'basic_type', 'reserved')) # Core constants FRAME_METHOD = 1 @@ -53,67 +58,69 @@ FRAME_END_BYTE = b'\xce' REPLY_SUCCESS = 200 REPLY_SUCCESS_BYTE = b'\xc8' - # Indicates that the method completed successfully. This reply code is - # reserved for future use - the current protocol design does not use positive - # confirmation and reply codes are sent only in case of an error. +# Indicates that the method completed successfully. This reply code is +# reserved for future use - the current protocol design does not use positive +# confirmation and reply codes are sent only in case of an error. CONTENT_TOO_LARGE = 311 - # The client attempted to transfer content larger than the server could accept - # at the present time. The client may retry at a later time. +# The client attempted to transfer content larger than the server could accept +# at the present time. The client may retry at a later time. NO_CONSUMERS = 313 - # When the exchange cannot deliver to a consumer when the immediate flag is - # set. As a result of pending data on the queue or the absence of any - # consumers of the queue. +# When the exchange cannot deliver to a consumer when the immediate flag is +# set. As a result of pending data on the queue or the absence of any +# consumers of the queue. CONNECTION_FORCED = 320 - # An operator intervened to close the connection for some reason. The client - # may retry at some later date. +# An operator intervened to close the connection for some reason. The client +# may retry at some later date. INVALID_PATH = 402 - # The client tried to work with an unknown virtual host. +# The client tried to work with an unknown virtual host. ACCESS_REFUSED = 403 - # The client attempted to work with a server entity to which it has no - # access due to security settings. +# The client attempted to work with a server entity to which it has no +# access due to security settings. NOT_FOUND = 404 - # The client attempted to work with a server entity that does not exist. +# The client attempted to work with a server entity that does not exist. RESOURCE_LOCKED = 405 - # The client attempted to work with a server entity to which it has no - # access because another client is working with it. +# The client attempted to work with a server entity to which it has no +# access because another client is working with it. PRECONDITION_FAILED = 406 - # The client requested a method that was not allowed because some precondition - # failed. +# The client requested a method that was not allowed because some precondition +# failed. FRAME_ERROR = 501 - # The sender sent a malformed frame that the recipient could not decode. - # This strongly implies a programming error in the sending peer. +# The sender sent a malformed frame that the recipient could not decode. +# This strongly implies a programming error in the sending peer. SYNTAX_ERROR = 502 - # The sender sent a frame that contained illegal values for one or more - # fields. This strongly implies a programming error in the sending peer. +# The sender sent a frame that contained illegal values for one or more +# fields. This strongly implies a programming error in the sending peer. COMMAND_INVALID = 503 - # The client sent an invalid sequence of frames, attempting to perform an - # operation that was considered invalid by the server. This usually implies - # a programming error in the client. +# The client sent an invalid sequence of frames, attempting to perform an +# operation that was considered invalid by the server. This usually implies +# a programming error in the client. CHANNEL_ERROR = 504 - # The client attempted to work with a channel that had not been correctly - # opened. This most likely indicates a fault in the client layer. +# The client attempted to work with a channel that had not been correctly +# opened. This most likely indicates a fault in the client layer. UNEXPECTED_FRAME = 505 - # The peer sent a frame that was not expected, usually in the context of - # a content header and body. This strongly indicates a fault in the peer's - # content processing. +# The peer sent a frame that was not expected, usually in the context of +# a content header and body. This strongly indicates a fault in the peer's +# content processing. RESOURCE_ERROR = 506 - # The server could not complete the method because it lacked sufficient - # resources. This may be due to the client creating too many of some type - # of entity. +# The server could not complete the method because it lacked sufficient +# resources. This may be due to the client creating too many of some type +# of entity. NOT_ALLOWED = 530 - # The client tried to work with some entity in a manner that is prohibited - # by the server, due to security settings or by some other criteria. +# The client tried to work with some entity in a manner that is prohibited +# by the server, due to security settings or by some other criteria. NOT_IMPLEMENTED = 540 - # The client tried to use functionality that is not implemented in the - # server. +# The client tried to use functionality that is not implemented in the +# server. INTERNAL_ERROR = 541 - # The server could not complete the method because of an internal error. - # The server may require intervention by an operator in order to resume - # normal operations. - -HARD_ERRORS = [CONNECTION_FORCED, INVALID_PATH, FRAME_ERROR, SYNTAX_ERROR, COMMAND_INVALID, CHANNEL_ERROR, UNEXPECTED_FRAME, RESOURCE_ERROR, NOT_ALLOWED, NOT_IMPLEMENTED, INTERNAL_ERROR] -SOFT_ERRORS = [CONTENT_TOO_LARGE, NO_CONSUMERS, ACCESS_REFUSED, NOT_FOUND, RESOURCE_LOCKED, PRECONDITION_FAILED] +# The server could not complete the method because of an internal error. +# The server may require intervention by an operator in order to resume +# normal operations. +HARD_ERRORS = [CONNECTION_FORCED, INVALID_PATH, FRAME_ERROR, SYNTAX_ERROR, + COMMAND_INVALID, CHANNEL_ERROR, UNEXPECTED_FRAME, RESOURCE_ERROR, + NOT_ALLOWED, NOT_IMPLEMENTED, INTERNAL_ERROR] +SOFT_ERRORS = [CONTENT_TOO_LARGE, NO_CONSUMERS, ACCESS_REFUSED, NOT_FOUND, + RESOURCE_LOCKED, PRECONDITION_FAILED] DOMAIN_TO_BASIC_TYPE = { u'class-id': u'short', @@ -142,6 +149,7 @@ DOMAIN_TO_BASIC_TYPE = { u'table': None, } + class Connection(AMQPClass): """ The connection class provides methods for a client to establish a network connection to @@ -158,20 +166,20 @@ class ConnectionBlocked(AMQPMethodPayload): and does not accept new publishes. """ - __slots__ = (u'reason', ) + __slots__ = (u'reason',) NAME = u'connection.blocked' - INDEX = (10, 60) # (Class ID, Method ID) - BINARY_HEADER = b'\x00\x0A\x00\x3C' # CLASS ID + METHOD ID + INDEX = (10, 60) # (Class ID, Method ID) + BINARY_HEADER = b'\x00\x0A\x00\x3C' # CLASS ID + METHOD ID SENT_BY_CLIENT, SENT_BY_SERVER = True, True - IS_SIZE_STATIC = False # this means that argument part has always the same length + IS_SIZE_STATIC = False # this means that argument part has always the same length IS_CONTENT_STATIC = False # this means that argument part has always the same content # See constructor pydoc for details - FIELDS = [ + FIELDS = [ Field(u'reason', u'shortstr', u'shortstr', reserved=False), ] @@ -186,7 +194,7 @@ class ConnectionBlocked(AMQPMethodPayload): def write_arguments(self, buf): buf.write(struct.pack('!B', len(self.reason))) buf.write(self.reason) - + def get_size(self): return 1 + len(self.reason) @@ -195,7 +203,7 @@ class ConnectionBlocked(AMQPMethodPayload): offset = start_offset s_len, = struct.unpack_from('!B', buf, offset) offset += 1 - reason = buf[offset:offset+s_len] + reason = buf[offset:offset + s_len] offset += s_len return ConnectionBlocked(reason) @@ -209,20 +217,20 @@ class ConnectionClose(AMQPMethodPayload): a specific method, i.e. an exception. When a close is due to an exception, the sender provides the class and method id of the method which caused the exception. """ - __slots__ = (u'reply_code', u'reply_text', u'class_id', u'method_id', ) + __slots__ = (u'reply_code', u'reply_text', u'class_id', u'method_id',) NAME = u'connection.close' - INDEX = (10, 50) # (Class ID, Method ID) - BINARY_HEADER = b'\x00\x0A\x00\x32' # CLASS ID + METHOD ID + INDEX = (10, 50) # (Class ID, Method ID) + BINARY_HEADER = b'\x00\x0A\x00\x32' # CLASS ID + METHOD ID SENT_BY_CLIENT, SENT_BY_SERVER = True, True - IS_SIZE_STATIC = False # this means that argument part has always the same length + IS_SIZE_STATIC = False # this means that argument part has always the same length IS_CONTENT_STATIC = False # this means that argument part has always the same content # See constructor pydoc for details - FIELDS = [ + FIELDS = [ Field(u'reply-code', u'reply-code', u'short', reserved=False), Field(u'reply-text', u'reply-text', u'shortstr', reserved=False), Field(u'class-id', u'class-id', u'short', reserved=False), @@ -252,7 +260,7 @@ class ConnectionClose(AMQPMethodPayload): buf.write(struct.pack('!HB', self.reply_code, len(self.reply_text))) buf.write(self.reply_text) buf.write(struct.pack('!HH', self.class_id, self.method_id)) - + def get_size(self): return 7 + len(self.reply_text) @@ -261,7 +269,7 @@ class ConnectionClose(AMQPMethodPayload): offset = start_offset reply_code, s_len, = struct.unpack_from('!HB', buf, offset) offset += 3 - reply_text = buf[offset:offset+s_len] + reply_text = buf[offset:offset + s_len] offset += s_len class_id, method_id, = struct.unpack_from('!HH', buf, offset) offset += 4 @@ -279,12 +287,12 @@ class ConnectionCloseOk(AMQPMethodPayload): NAME = u'connection.close-ok' - INDEX = (10, 51) # (Class ID, Method ID) - BINARY_HEADER = b'\x00\x0A\x00\x33' # CLASS ID + METHOD ID + INDEX = (10, 51) # (Class ID, Method ID) + BINARY_HEADER = b'\x00\x0A\x00\x33' # CLASS ID + METHOD ID SENT_BY_CLIENT, SENT_BY_SERVER = True, True - IS_SIZE_STATIC = True # this means that argument part has always the same length + IS_SIZE_STATIC = True # this means that argument part has always the same length IS_CONTENT_STATIC = True # this means that argument part has always the same content STATIC_CONTENT = b'\x00\x00\x00\x04\x00\x0A\x00\x33\xCE' # spans LENGTH, CLASS ID, METHOD ID, ....., FRAME_END @@ -293,7 +301,6 @@ class ConnectionCloseOk(AMQPMethodPayload): Create frame connection.close-ok """ - @staticmethod def from_buffer(buf, start_offset): offset = start_offset @@ -309,20 +316,20 @@ class ConnectionOpen(AMQPMethodPayload): The server may apply arbitrary limits per virtual host, such as the number of each type of entity that may be used, per connection and/or in total. """ - __slots__ = (u'virtual_host', ) + __slots__ = (u'virtual_host',) NAME = u'connection.open' - INDEX = (10, 40) # (Class ID, Method ID) - BINARY_HEADER = b'\x00\x0A\x00\x28' # CLASS ID + METHOD ID + INDEX = (10, 40) # (Class ID, Method ID) + BINARY_HEADER = b'\x00\x0A\x00\x28' # CLASS ID + METHOD ID SENT_BY_CLIENT, SENT_BY_SERVER = True, False - IS_SIZE_STATIC = False # this means that argument part has always the same length + IS_SIZE_STATIC = False # this means that argument part has always the same length IS_CONTENT_STATIC = False # this means that argument part has always the same content # See constructor pydoc for details - FIELDS = [ + FIELDS = [ Field(u'virtual-host', u'path', u'shortstr', reserved=False), Field(u'reserved-1', u'shortstr', u'shortstr', reserved=True), Field(u'reserved-2', u'bit', u'bit', reserved=True), @@ -343,7 +350,7 @@ class ConnectionOpen(AMQPMethodPayload): buf.write(self.virtual_host) buf.write(b'\x00') buf.write(struct.pack('!B', 0)) - + def get_size(self): return 3 + len(self.virtual_host) @@ -352,11 +359,11 @@ class ConnectionOpen(AMQPMethodPayload): offset = start_offset s_len, = struct.unpack_from('!B', buf, offset) offset += 1 - virtual_host = buf[offset:offset+s_len] + virtual_host = buf[offset:offset + s_len] offset += s_len s_len, = struct.unpack_from('!B', buf, offset) offset += 1 - offset += s_len # reserved field! + offset += s_len # reserved field! offset += 1 return ConnectionOpen(virtual_host) @@ -371,17 +378,17 @@ class ConnectionOpenOk(AMQPMethodPayload): NAME = u'connection.open-ok' - INDEX = (10, 41) # (Class ID, Method ID) - BINARY_HEADER = b'\x00\x0A\x00\x29' # CLASS ID + METHOD ID + INDEX = (10, 41) # (Class ID, Method ID) + BINARY_HEADER = b'\x00\x0A\x00\x29' # CLASS ID + METHOD ID SENT_BY_CLIENT, SENT_BY_SERVER = False, True - IS_SIZE_STATIC = False # this means that argument part has always the same length + IS_SIZE_STATIC = False # this means that argument part has always the same length IS_CONTENT_STATIC = True # this means that argument part has always the same content STATIC_CONTENT = b'\x00\x00\x00\x04\x00\x0A\x00\x29\x00\xCE' # spans LENGTH, CLASS ID, METHOD ID, ....., FRAME_END # See constructor pydoc for details - FIELDS = [ + FIELDS = [ Field(u'reserved-1', u'shortstr', u'shortstr', reserved=True), ] @@ -390,13 +397,12 @@ class ConnectionOpenOk(AMQPMethodPayload): Create frame connection.open-ok """ - @staticmethod def from_buffer(buf, start_offset): offset = start_offset s_len, = struct.unpack_from('!B', buf, offset) offset += 1 - offset += s_len # reserved field! + offset += s_len # reserved field! return ConnectionOpenOk() @@ -408,28 +414,32 @@ class ConnectionStart(AMQPMethodPayload): protocol version that the server proposes, along with a list of security mechanisms which the client can use for authentication. """ - __slots__ = (u'version_major', u'version_minor', u'server_properties', u'mechanisms', u'locales', ) + __slots__ = ( + u'version_major', u'version_minor', u'server_properties', u'mechanisms', + u'locales',) NAME = u'connection.start' - INDEX = (10, 10) # (Class ID, Method ID) - BINARY_HEADER = b'\x00\x0A\x00\x0A' # CLASS ID + METHOD ID + INDEX = (10, 10) # (Class ID, Method ID) + BINARY_HEADER = b'\x00\x0A\x00\x0A' # CLASS ID + METHOD ID SENT_BY_CLIENT, SENT_BY_SERVER = False, True - IS_SIZE_STATIC = False # this means that argument part has always the same length + IS_SIZE_STATIC = False # this means that argument part has always the same length IS_CONTENT_STATIC = False # this means that argument part has always the same content # See constructor pydoc for details - FIELDS = [ + FIELDS = [ Field(u'version-major', u'octet', u'octet', reserved=False), Field(u'version-minor', u'octet', u'octet', reserved=False), - Field(u'server-properties', u'peer-properties', u'table', reserved=False), + Field(u'server-properties', u'peer-properties', u'table', + reserved=False), Field(u'mechanisms', u'longstr', u'longstr', reserved=False), Field(u'locales', u'longstr', u'longstr', reserved=False), ] - def __init__(self, version_major, version_minor, server_properties, mechanisms, locales): + def __init__(self, version_major, version_minor, server_properties, + mechanisms, locales): """ Create frame connection.start @@ -469,9 +479,10 @@ class ConnectionStart(AMQPMethodPayload): buf.write(self.mechanisms) buf.write(struct.pack('!I', len(self.locales))) buf.write(self.locales) - + def get_size(self): - return 10 + frame_table_size(self.server_properties) + len(self.mechanisms) + len(self.locales) + return 10 + frame_table_size(self.server_properties) + len( + self.mechanisms) + len(self.locales) @staticmethod def from_buffer(buf, start_offset): @@ -482,13 +493,14 @@ class ConnectionStart(AMQPMethodPayload): offset += delta s_len, = struct.unpack_from('!L', buf, offset) offset += 4 - mechanisms = buf[offset:offset+s_len] + mechanisms = buf[offset:offset + s_len] offset += s_len s_len, = struct.unpack_from('!L', buf, offset) offset += 4 - locales = buf[offset:offset+s_len] + locales = buf[offset:offset + s_len] offset += s_len - return ConnectionStart(version_major, version_minor, server_properties, mechanisms, locales) + return ConnectionStart(version_major, version_minor, server_properties, + mechanisms, locales) class ConnectionSecure(AMQPMethodPayload): @@ -499,20 +511,20 @@ class ConnectionSecure(AMQPMethodPayload): received sufficient information to authenticate each other. This method challenges the client to provide more information. """ - __slots__ = (u'challenge', ) + __slots__ = (u'challenge',) NAME = u'connection.secure' - INDEX = (10, 20) # (Class ID, Method ID) - BINARY_HEADER = b'\x00\x0A\x00\x14' # CLASS ID + METHOD ID + INDEX = (10, 20) # (Class ID, Method ID) + BINARY_HEADER = b'\x00\x0A\x00\x14' # CLASS ID + METHOD ID SENT_BY_CLIENT, SENT_BY_SERVER = False, True - IS_SIZE_STATIC = False # this means that argument part has always the same length + IS_SIZE_STATIC = False # this means that argument part has always the same length IS_CONTENT_STATIC = False # this means that argument part has always the same content # See constructor pydoc for details - FIELDS = [ + FIELDS = [ Field(u'challenge', u'longstr', u'longstr', reserved=False), ] @@ -530,7 +542,7 @@ class ConnectionSecure(AMQPMethodPayload): def write_arguments(self, buf): buf.write(struct.pack('!I', len(self.challenge))) buf.write(self.challenge) - + def get_size(self): return 4 + len(self.challenge) @@ -539,7 +551,7 @@ class ConnectionSecure(AMQPMethodPayload): offset = start_offset s_len, = struct.unpack_from('!L', buf, offset) offset += 4 - challenge = buf[offset:offset+s_len] + challenge = buf[offset:offset + s_len] offset += s_len return ConnectionSecure(challenge) @@ -550,21 +562,22 @@ class ConnectionStartOk(AMQPMethodPayload): This method selects a SASL security mechanism. """ - __slots__ = (u'client_properties', u'mechanism', u'response', u'locale', ) + __slots__ = (u'client_properties', u'mechanism', u'response', u'locale',) NAME = u'connection.start-ok' - INDEX = (10, 11) # (Class ID, Method ID) - BINARY_HEADER = b'\x00\x0A\x00\x0B' # CLASS ID + METHOD ID + INDEX = (10, 11) # (Class ID, Method ID) + BINARY_HEADER = b'\x00\x0A\x00\x0B' # CLASS ID + METHOD ID SENT_BY_CLIENT, SENT_BY_SERVER = True, False - IS_SIZE_STATIC = False # this means that argument part has always the same length + IS_SIZE_STATIC = False # this means that argument part has always the same length IS_CONTENT_STATIC = False # this means that argument part has always the same content # See constructor pydoc for details - FIELDS = [ - Field(u'client-properties', u'peer-properties', u'table', reserved=False), + FIELDS = [ + Field(u'client-properties', u'peer-properties', u'table', + reserved=False), Field(u'mechanism', u'shortstr', u'shortstr', reserved=False), Field(u'response', u'longstr', u'longstr', reserved=False), Field(u'locale', u'shortstr', u'shortstr', reserved=False), @@ -606,9 +619,10 @@ class ConnectionStartOk(AMQPMethodPayload): buf.write(self.response) buf.write(struct.pack('!B', len(self.locale))) buf.write(self.locale) - + def get_size(self): - return 6 + frame_table_size(self.client_properties) + len(self.mechanism) + len(self.response) + len(self.locale) + return 6 + frame_table_size(self.client_properties) + len( + self.mechanism) + len(self.response) + len(self.locale) @staticmethod def from_buffer(buf, start_offset): @@ -617,15 +631,15 @@ class ConnectionStartOk(AMQPMethodPayload): offset += delta s_len, = struct.unpack_from('!B', buf, offset) offset += 1 - mechanism = buf[offset:offset+s_len] + mechanism = buf[offset:offset + s_len] offset += s_len s_len, = struct.unpack_from('!L', buf, offset) offset += 4 - response = buf[offset:offset+s_len] + response = buf[offset:offset + s_len] offset += s_len s_len, = struct.unpack_from('!B', buf, offset) offset += 1 - locale = buf[offset:offset+s_len] + locale = buf[offset:offset + s_len] offset += s_len return ConnectionStartOk(client_properties, mechanism, response, locale) @@ -637,20 +651,20 @@ class ConnectionSecureOk(AMQPMethodPayload): This method attempts to authenticate, passing a block of SASL data for the security mechanism at the server side. """ - __slots__ = (u'response', ) + __slots__ = (u'response',) NAME = u'connection.secure-ok' - INDEX = (10, 21) # (Class ID, Method ID) - BINARY_HEADER = b'\x00\x0A\x00\x15' # CLASS ID + METHOD ID + INDEX = (10, 21) # (Class ID, Method ID) + BINARY_HEADER = b'\x00\x0A\x00\x15' # CLASS ID + METHOD ID SENT_BY_CLIENT, SENT_BY_SERVER = True, False - IS_SIZE_STATIC = False # this means that argument part has always the same length + IS_SIZE_STATIC = False # this means that argument part has always the same length IS_CONTENT_STATIC = False # this means that argument part has always the same content # See constructor pydoc for details - FIELDS = [ + FIELDS = [ Field(u'response', u'longstr', u'longstr', reserved=False), ] @@ -668,7 +682,7 @@ class ConnectionSecureOk(AMQPMethodPayload): def write_arguments(self, buf): buf.write(struct.pack('!I', len(self.response))) buf.write(self.response) - + def get_size(self): return 4 + len(self.response) @@ -677,7 +691,7 @@ class ConnectionSecureOk(AMQPMethodPayload): offset = start_offset s_len, = struct.unpack_from('!L', buf, offset) offset += 4 - response = buf[offset:offset+s_len] + response = buf[offset:offset + s_len] offset += s_len return ConnectionSecureOk(response) @@ -689,20 +703,20 @@ class ConnectionTune(AMQPMethodPayload): This method proposes a set of connection configuration values to the client. The client can accept and/or adjust these. """ - __slots__ = (u'channel_max', u'frame_max', u'heartbeat', ) + __slots__ = (u'channel_max', u'frame_max', u'heartbeat',) NAME = u'connection.tune' - INDEX = (10, 30) # (Class ID, Method ID) - BINARY_HEADER = b'\x00\x0A\x00\x1E' # CLASS ID + METHOD ID + INDEX = (10, 30) # (Class ID, Method ID) + BINARY_HEADER = b'\x00\x0A\x00\x1E' # CLASS ID + METHOD ID SENT_BY_CLIENT, SENT_BY_SERVER = False, True - IS_SIZE_STATIC = True # this means that argument part has always the same length + IS_SIZE_STATIC = True # this means that argument part has always the same length IS_CONTENT_STATIC = False # this means that argument part has always the same content # See constructor pydoc for details - FIELDS = [ + FIELDS = [ Field(u'channel-max', u'short', u'short', reserved=False), Field(u'frame-max', u'long', u'long', reserved=False), Field(u'heartbeat', u'short', u'short', reserved=False), @@ -732,15 +746,17 @@ class ConnectionTune(AMQPMethodPayload): self.heartbeat = heartbeat def write_arguments(self, buf): - buf.write(struct.pack('!HIH', self.channel_max, self.frame_max, self.heartbeat)) - + buf.write(struct.pack('!HIH', self.channel_max, self.frame_max, + self.heartbeat)) + def get_size(self): return 8 @staticmethod def from_buffer(buf, start_offset): offset = start_offset - channel_max, frame_max, heartbeat, = struct.unpack_from('!HIH', buf, offset) + channel_max, frame_max, heartbeat, = struct.unpack_from('!HIH', buf, + offset) offset += 8 return ConnectionTune(channel_max, frame_max, heartbeat) @@ -752,20 +768,20 @@ class ConnectionTuneOk(AMQPMethodPayload): This method sends the client's connection tuning parameters to the server. Certain fields are negotiated, others provide capability information. """ - __slots__ = (u'channel_max', u'frame_max', u'heartbeat', ) + __slots__ = (u'channel_max', u'frame_max', u'heartbeat',) NAME = u'connection.tune-ok' - INDEX = (10, 31) # (Class ID, Method ID) - BINARY_HEADER = b'\x00\x0A\x00\x1F' # CLASS ID + METHOD ID + INDEX = (10, 31) # (Class ID, Method ID) + BINARY_HEADER = b'\x00\x0A\x00\x1F' # CLASS ID + METHOD ID SENT_BY_CLIENT, SENT_BY_SERVER = True, False - IS_SIZE_STATIC = True # this means that argument part has always the same length + IS_SIZE_STATIC = True # this means that argument part has always the same length IS_CONTENT_STATIC = False # this means that argument part has always the same content # See constructor pydoc for details - FIELDS = [ + FIELDS = [ Field(u'channel-max', u'short', u'short', reserved=False), Field(u'frame-max', u'long', u'long', reserved=False), Field(u'heartbeat', u'short', u'short', reserved=False), @@ -795,15 +811,17 @@ class ConnectionTuneOk(AMQPMethodPayload): self.heartbeat = heartbeat def write_arguments(self, buf): - buf.write(struct.pack('!HIH', self.channel_max, self.frame_max, self.heartbeat)) - + buf.write(struct.pack('!HIH', self.channel_max, self.frame_max, + self.heartbeat)) + def get_size(self): return 8 @staticmethod def from_buffer(buf, start_offset): offset = start_offset - channel_max, frame_max, heartbeat, = struct.unpack_from('!HIH', buf, offset) + channel_max, frame_max, heartbeat, = struct.unpack_from('!HIH', buf, + offset) offset += 8 return ConnectionTuneOk(channel_max, frame_max, heartbeat) @@ -818,12 +836,12 @@ class ConnectionUnblocked(AMQPMethodPayload): NAME = u'connection.unblocked' - INDEX = (10, 61) # (Class ID, Method ID) - BINARY_HEADER = b'\x00\x0A\x00\x3D' # CLASS ID + METHOD ID + INDEX = (10, 61) # (Class ID, Method ID) + BINARY_HEADER = b'\x00\x0A\x00\x3D' # CLASS ID + METHOD ID SENT_BY_CLIENT, SENT_BY_SERVER = True, True - IS_SIZE_STATIC = True # this means that argument part has always the same length + IS_SIZE_STATIC = True # this means that argument part has always the same length IS_CONTENT_STATIC = True # this means that argument part has always the same content STATIC_CONTENT = b'\x00\x00\x00\x04\x00\x0A\x00\x3D\xCE' # spans LENGTH, CLASS ID, METHOD ID, ....., FRAME_END @@ -832,7 +850,6 @@ class ConnectionUnblocked(AMQPMethodPayload): Create frame connection.unblocked """ - @staticmethod def from_buffer(buf, start_offset): offset = start_offset @@ -858,20 +875,20 @@ class ChannelClose(AMQPMethodPayload): method, i.e. an exception. When a close is due to an exception, the sender provides the class and method id of the method which caused the exception. """ - __slots__ = (u'reply_code', u'reply_text', u'class_id', u'method_id', ) + __slots__ = (u'reply_code', u'reply_text', u'class_id', u'method_id',) NAME = u'channel.close' - INDEX = (20, 40) # (Class ID, Method ID) - BINARY_HEADER = b'\x00\x14\x00\x28' # CLASS ID + METHOD ID + INDEX = (20, 40) # (Class ID, Method ID) + BINARY_HEADER = b'\x00\x14\x00\x28' # CLASS ID + METHOD ID SENT_BY_CLIENT, SENT_BY_SERVER = True, True - IS_SIZE_STATIC = False # this means that argument part has always the same length + IS_SIZE_STATIC = False # this means that argument part has always the same length IS_CONTENT_STATIC = False # this means that argument part has always the same content # See constructor pydoc for details - FIELDS = [ + FIELDS = [ Field(u'reply-code', u'reply-code', u'short', reserved=False), Field(u'reply-text', u'reply-text', u'shortstr', reserved=False), Field(u'class-id', u'class-id', u'short', reserved=False), @@ -901,7 +918,7 @@ class ChannelClose(AMQPMethodPayload): buf.write(struct.pack('!HB', self.reply_code, len(self.reply_text))) buf.write(self.reply_text) buf.write(struct.pack('!HH', self.class_id, self.method_id)) - + def get_size(self): return 7 + len(self.reply_text) @@ -910,7 +927,7 @@ class ChannelClose(AMQPMethodPayload): offset = start_offset reply_code, s_len, = struct.unpack_from('!HB', buf, offset) offset += 3 - reply_text = buf[offset:offset+s_len] + reply_text = buf[offset:offset + s_len] offset += s_len class_id, method_id, = struct.unpack_from('!HH', buf, offset) offset += 4 @@ -928,12 +945,12 @@ class ChannelCloseOk(AMQPMethodPayload): NAME = u'channel.close-ok' - INDEX = (20, 41) # (Class ID, Method ID) - BINARY_HEADER = b'\x00\x14\x00\x29' # CLASS ID + METHOD ID + INDEX = (20, 41) # (Class ID, Method ID) + BINARY_HEADER = b'\x00\x14\x00\x29' # CLASS ID + METHOD ID SENT_BY_CLIENT, SENT_BY_SERVER = True, True - IS_SIZE_STATIC = True # this means that argument part has always the same length + IS_SIZE_STATIC = True # this means that argument part has always the same length IS_CONTENT_STATIC = True # this means that argument part has always the same content STATIC_CONTENT = b'\x00\x00\x00\x04\x00\x14\x00\x29\xCE' # spans LENGTH, CLASS ID, METHOD ID, ....., FRAME_END @@ -942,7 +959,6 @@ class ChannelCloseOk(AMQPMethodPayload): Create frame channel.close-ok """ - @staticmethod def from_buffer(buf, start_offset): offset = start_offset @@ -959,20 +975,20 @@ class ChannelFlow(AMQPMethodPayload): it can process. Note that this method is not intended for window control. It does not affect contents returned by Basic.Get-Ok methods. """ - __slots__ = (u'active', ) + __slots__ = (u'active',) NAME = u'channel.flow' - INDEX = (20, 20) # (Class ID, Method ID) - BINARY_HEADER = b'\x00\x14\x00\x14' # CLASS ID + METHOD ID + INDEX = (20, 20) # (Class ID, Method ID) + BINARY_HEADER = b'\x00\x14\x00\x14' # CLASS ID + METHOD ID SENT_BY_CLIENT, SENT_BY_SERVER = True, True - IS_SIZE_STATIC = True # this means that argument part has always the same length + IS_SIZE_STATIC = True # this means that argument part has always the same length IS_CONTENT_STATIC = False # this means that argument part has always the same content # See constructor pydoc for details - FIELDS = [ + FIELDS = [ Field(u'active', u'bit', u'bit', reserved=False), ] @@ -989,7 +1005,7 @@ class ChannelFlow(AMQPMethodPayload): def write_arguments(self, buf): buf.write(struct.pack('!B', (self.active << 0))) - + def get_size(self): return 1 @@ -1009,20 +1025,20 @@ class ChannelFlowOk(AMQPMethodPayload): Confirms to the peer that a flow command was received and processed. """ - __slots__ = (u'active', ) + __slots__ = (u'active',) NAME = u'channel.flow-ok' - INDEX = (20, 21) # (Class ID, Method ID) - BINARY_HEADER = b'\x00\x14\x00\x15' # CLASS ID + METHOD ID + INDEX = (20, 21) # (Class ID, Method ID) + BINARY_HEADER = b'\x00\x14\x00\x15' # CLASS ID + METHOD ID SENT_BY_CLIENT, SENT_BY_SERVER = True, True - IS_SIZE_STATIC = True # this means that argument part has always the same length + IS_SIZE_STATIC = True # this means that argument part has always the same length IS_CONTENT_STATIC = False # this means that argument part has always the same content # See constructor pydoc for details - FIELDS = [ + FIELDS = [ Field(u'active', u'bit', u'bit', reserved=False), ] @@ -1039,7 +1055,7 @@ class ChannelFlowOk(AMQPMethodPayload): def write_arguments(self, buf): buf.write(struct.pack('!B', (self.active << 0))) - + def get_size(self): return 1 @@ -1063,17 +1079,17 @@ class ChannelOpen(AMQPMethodPayload): NAME = u'channel.open' - INDEX = (20, 10) # (Class ID, Method ID) - BINARY_HEADER = b'\x00\x14\x00\x0A' # CLASS ID + METHOD ID + INDEX = (20, 10) # (Class ID, Method ID) + BINARY_HEADER = b'\x00\x14\x00\x0A' # CLASS ID + METHOD ID SENT_BY_CLIENT, SENT_BY_SERVER = True, False - IS_SIZE_STATIC = False # this means that argument part has always the same length + IS_SIZE_STATIC = False # this means that argument part has always the same length IS_CONTENT_STATIC = True # this means that argument part has always the same content STATIC_CONTENT = b'\x00\x00\x00\x05\x00\x14\x00\x0A\x00\xCE' # spans LENGTH, CLASS ID, METHOD ID, ....., FRAME_END # See constructor pydoc for details - FIELDS = [ + FIELDS = [ Field(u'reserved-1', u'shortstr', u'shortstr', reserved=True), ] @@ -1082,13 +1098,12 @@ class ChannelOpen(AMQPMethodPayload): Create frame channel.open """ - @staticmethod def from_buffer(buf, start_offset): offset = start_offset s_len, = struct.unpack_from('!B', buf, offset) offset += 1 - offset += s_len # reserved field! + offset += s_len # reserved field! return ChannelOpen() @@ -1102,17 +1117,17 @@ class ChannelOpenOk(AMQPMethodPayload): NAME = u'channel.open-ok' - INDEX = (20, 11) # (Class ID, Method ID) - BINARY_HEADER = b'\x00\x14\x00\x0B' # CLASS ID + METHOD ID + INDEX = (20, 11) # (Class ID, Method ID) + BINARY_HEADER = b'\x00\x14\x00\x0B' # CLASS ID + METHOD ID SENT_BY_CLIENT, SENT_BY_SERVER = False, True - IS_SIZE_STATIC = False # this means that argument part has always the same length + IS_SIZE_STATIC = False # this means that argument part has always the same length IS_CONTENT_STATIC = True # this means that argument part has always the same content STATIC_CONTENT = b'\x00\x00\x00\x05\x00\x14\x00\x0B\x00\x00\x00\x00\xCE' # spans LENGTH, CLASS ID, METHOD ID, ....., FRAME_END # See constructor pydoc for details - FIELDS = [ + FIELDS = [ Field(u'reserved-1', u'longstr', u'longstr', reserved=True), ] @@ -1121,13 +1136,12 @@ class ChannelOpenOk(AMQPMethodPayload): Create frame channel.open-ok """ - @staticmethod def from_buffer(buf, start_offset): offset = start_offset s_len, = struct.unpack_from('!L', buf, offset) offset += 4 - offset += s_len # reserved field! + offset += s_len # reserved field! return ChannelOpenOk() @@ -1147,20 +1161,21 @@ class ExchangeBind(AMQPMethodPayload): This method binds an exchange to an exchange. """ - __slots__ = (u'destination', u'source', u'routing_key', u'no_wait', u'arguments', ) + __slots__ = ( + u'destination', u'source', u'routing_key', u'no_wait', u'arguments',) NAME = u'exchange.bind' - INDEX = (40, 30) # (Class ID, Method ID) - BINARY_HEADER = b'\x00\x28\x00\x1E' # CLASS ID + METHOD ID + INDEX = (40, 30) # (Class ID, Method ID) + BINARY_HEADER = b'\x00\x28\x00\x1E' # CLASS ID + METHOD ID SENT_BY_CLIENT, SENT_BY_SERVER = True, False - IS_SIZE_STATIC = False # this means that argument part has always the same length + IS_SIZE_STATIC = False # this means that argument part has always the same length IS_CONTENT_STATIC = False # this means that argument part has always the same content # See constructor pydoc for details - FIELDS = [ + FIELDS = [ Field(u'reserved-1', u'short', u'short', reserved=True), Field(u'destination', u'exchange-name', u'shortstr', reserved=False), Field(u'source', u'exchange-name', u'shortstr', reserved=False), @@ -1207,24 +1222,25 @@ class ExchangeBind(AMQPMethodPayload): buf.write(self.routing_key) buf.write(struct.pack('!B', (self.no_wait << 0))) enframe_table(buf, self.arguments) - + def get_size(self): - return 6 + len(self.destination) + len(self.source) + len(self.routing_key) + frame_table_size(self.arguments) + return 6 + len(self.destination) + len(self.source) + len( + self.routing_key) + frame_table_size(self.arguments) @staticmethod def from_buffer(buf, start_offset): offset = start_offset s_len, = struct.unpack_from('!2xB', buf, offset) offset += 3 - destination = buf[offset:offset+s_len] + destination = buf[offset:offset + s_len] offset += s_len s_len, = struct.unpack_from('!B', buf, offset) offset += 1 - source = buf[offset:offset+s_len] + source = buf[offset:offset + s_len] offset += s_len s_len, = struct.unpack_from('!B', buf, offset) offset += 1 - routing_key = buf[offset:offset+s_len] + routing_key = buf[offset:offset + s_len] offset += s_len _bit, = struct.unpack_from('!B', buf, offset) offset += 0 @@ -1232,7 +1248,8 @@ class ExchangeBind(AMQPMethodPayload): offset += 1 arguments, delta = deframe_table(buf, offset) offset += delta - return ExchangeBind(destination, source, routing_key, no_wait, arguments) + return ExchangeBind(destination, source, routing_key, no_wait, + arguments) class ExchangeBindOk(AMQPMethodPayload): @@ -1245,12 +1262,12 @@ class ExchangeBindOk(AMQPMethodPayload): NAME = u'exchange.bind-ok' - INDEX = (40, 31) # (Class ID, Method ID) - BINARY_HEADER = b'\x00\x28\x00\x1F' # CLASS ID + METHOD ID + INDEX = (40, 31) # (Class ID, Method ID) + BINARY_HEADER = b'\x00\x28\x00\x1F' # CLASS ID + METHOD ID SENT_BY_CLIENT, SENT_BY_SERVER = False, True - IS_SIZE_STATIC = True # this means that argument part has always the same length + IS_SIZE_STATIC = True # this means that argument part has always the same length IS_CONTENT_STATIC = True # this means that argument part has always the same content STATIC_CONTENT = b'\x00\x00\x00\x04\x00\x28\x00\x1F\xCE' # spans LENGTH, CLASS ID, METHOD ID, ....., FRAME_END @@ -1259,7 +1276,6 @@ class ExchangeBindOk(AMQPMethodPayload): Create frame exchange.bind-ok """ - @staticmethod def from_buffer(buf, start_offset): offset = start_offset @@ -1273,20 +1289,22 @@ class ExchangeDeclare(AMQPMethodPayload): This method creates an exchange if it does not already exist, and if the exchange exists, verifies that it is of the correct and expected class. """ - __slots__ = (u'exchange', u'type_', u'passive', u'durable', u'auto_delete', u'internal', u'no_wait', u'arguments', ) + __slots__ = ( + u'exchange', u'type_', u'passive', u'durable', u'auto_delete', u'internal', + u'no_wait', u'arguments',) NAME = u'exchange.declare' - INDEX = (40, 10) # (Class ID, Method ID) - BINARY_HEADER = b'\x00\x28\x00\x0A' # CLASS ID + METHOD ID + INDEX = (40, 10) # (Class ID, Method ID) + BINARY_HEADER = b'\x00\x28\x00\x0A' # CLASS ID + METHOD ID SENT_BY_CLIENT, SENT_BY_SERVER = True, False - IS_SIZE_STATIC = False # this means that argument part has always the same length + IS_SIZE_STATIC = False # this means that argument part has always the same length IS_CONTENT_STATIC = False # this means that argument part has always the same content # See constructor pydoc for details - FIELDS = [ + FIELDS = [ Field(u'reserved-1', u'short', u'short', reserved=True), Field(u'exchange', u'exchange-name', u'shortstr', reserved=False), Field(u'type', u'shortstr', u'shortstr', reserved=False), @@ -1298,7 +1316,8 @@ class ExchangeDeclare(AMQPMethodPayload): Field(u'arguments', u'table', u'table', reserved=False), ] - def __init__(self, exchange, type_, passive, durable, auto_delete, internal, no_wait, arguments): + def __init__(self, exchange, type_, passive, durable, auto_delete, internal, + no_wait, arguments): """ Create frame exchange.declare @@ -1356,22 +1375,26 @@ class ExchangeDeclare(AMQPMethodPayload): buf.write(self.exchange) buf.write(struct.pack('!B', len(self.type_))) buf.write(self.type_) - buf.write(struct.pack('!B', (self.passive << 0) | (self.durable << 1) | (self.auto_delete << 2) | (self.internal << 3) | (self.no_wait << 4))) + buf.write(struct.pack('!B', + (self.passive << 0) | (self.durable << 1) | ( + self.auto_delete << 2) | (self.internal << 3) | ( + self.no_wait << 4))) enframe_table(buf, self.arguments) - + def get_size(self): - return 5 + len(self.exchange) + len(self.type_) + frame_table_size(self.arguments) + return 5 + len(self.exchange) + len(self.type_) + frame_table_size( + self.arguments) @staticmethod def from_buffer(buf, start_offset): offset = start_offset s_len, = struct.unpack_from('!2xB', buf, offset) offset += 3 - exchange = buf[offset:offset+s_len] + exchange = buf[offset:offset + s_len] offset += s_len s_len, = struct.unpack_from('!B', buf, offset) offset += 1 - type_ = buf[offset:offset+s_len] + type_ = buf[offset:offset + s_len] offset += s_len _bit, = struct.unpack_from('!B', buf, offset) offset += 0 @@ -1383,7 +1406,8 @@ class ExchangeDeclare(AMQPMethodPayload): offset += 1 arguments, delta = deframe_table(buf, offset) offset += delta - return ExchangeDeclare(exchange, type_, passive, durable, auto_delete, internal, no_wait, arguments) + return ExchangeDeclare(exchange, type_, passive, durable, auto_delete, + internal, no_wait, arguments) class ExchangeDelete(AMQPMethodPayload): @@ -1393,20 +1417,20 @@ class ExchangeDelete(AMQPMethodPayload): This method deletes an exchange. When an exchange is deleted all queue bindings on the exchange are cancelled. """ - __slots__ = (u'exchange', u'if_unused', u'no_wait', ) + __slots__ = (u'exchange', u'if_unused', u'no_wait',) NAME = u'exchange.delete' - INDEX = (40, 20) # (Class ID, Method ID) - BINARY_HEADER = b'\x00\x28\x00\x14' # CLASS ID + METHOD ID + INDEX = (40, 20) # (Class ID, Method ID) + BINARY_HEADER = b'\x00\x28\x00\x14' # CLASS ID + METHOD ID SENT_BY_CLIENT, SENT_BY_SERVER = True, False - IS_SIZE_STATIC = False # this means that argument part has always the same length + IS_SIZE_STATIC = False # this means that argument part has always the same length IS_CONTENT_STATIC = False # this means that argument part has always the same content # See constructor pydoc for details - FIELDS = [ + FIELDS = [ Field(u'reserved-1', u'short', u'short', reserved=True), Field(u'exchange', u'exchange-name', u'shortstr', reserved=False), Field(u'if-unused', u'bit', u'bit', reserved=False), @@ -1434,8 +1458,9 @@ class ExchangeDelete(AMQPMethodPayload): buf.write(b'\x00\x00') buf.write(struct.pack('!B', len(self.exchange))) buf.write(self.exchange) - buf.write(struct.pack('!B', (self.if_unused << 0) | (self.no_wait << 1))) - + buf.write( + struct.pack('!B', (self.if_unused << 0) | (self.no_wait << 1))) + def get_size(self): return 4 + len(self.exchange) @@ -1444,7 +1469,7 @@ class ExchangeDelete(AMQPMethodPayload): offset = start_offset s_len, = struct.unpack_from('!2xB', buf, offset) offset += 3 - exchange = buf[offset:offset+s_len] + exchange = buf[offset:offset + s_len] offset += s_len _bit, = struct.unpack_from('!B', buf, offset) offset += 0 @@ -1465,12 +1490,12 @@ class ExchangeDeclareOk(AMQPMethodPayload): NAME = u'exchange.declare-ok' - INDEX = (40, 11) # (Class ID, Method ID) - BINARY_HEADER = b'\x00\x28\x00\x0B' # CLASS ID + METHOD ID + INDEX = (40, 11) # (Class ID, Method ID) + BINARY_HEADER = b'\x00\x28\x00\x0B' # CLASS ID + METHOD ID SENT_BY_CLIENT, SENT_BY_SERVER = False, True - IS_SIZE_STATIC = True # this means that argument part has always the same length + IS_SIZE_STATIC = True # this means that argument part has always the same length IS_CONTENT_STATIC = True # this means that argument part has always the same content STATIC_CONTENT = b'\x00\x00\x00\x04\x00\x28\x00\x0B\xCE' # spans LENGTH, CLASS ID, METHOD ID, ....., FRAME_END @@ -1479,7 +1504,6 @@ class ExchangeDeclareOk(AMQPMethodPayload): Create frame exchange.declare-ok """ - @staticmethod def from_buffer(buf, start_offset): offset = start_offset @@ -1496,12 +1520,12 @@ class ExchangeDeleteOk(AMQPMethodPayload): NAME = u'exchange.delete-ok' - INDEX = (40, 21) # (Class ID, Method ID) - BINARY_HEADER = b'\x00\x28\x00\x15' # CLASS ID + METHOD ID + INDEX = (40, 21) # (Class ID, Method ID) + BINARY_HEADER = b'\x00\x28\x00\x15' # CLASS ID + METHOD ID SENT_BY_CLIENT, SENT_BY_SERVER = False, True - IS_SIZE_STATIC = True # this means that argument part has always the same length + IS_SIZE_STATIC = True # this means that argument part has always the same length IS_CONTENT_STATIC = True # this means that argument part has always the same content STATIC_CONTENT = b'\x00\x00\x00\x04\x00\x28\x00\x15\xCE' # spans LENGTH, CLASS ID, METHOD ID, ....., FRAME_END @@ -1510,7 +1534,6 @@ class ExchangeDeleteOk(AMQPMethodPayload): Create frame exchange.delete-ok """ - @staticmethod def from_buffer(buf, start_offset): offset = start_offset @@ -1523,20 +1546,21 @@ class ExchangeUnbind(AMQPMethodPayload): This method unbinds an exchange from an exchange. """ - __slots__ = (u'destination', u'source', u'routing_key', u'no_wait', u'arguments', ) + __slots__ = ( + u'destination', u'source', u'routing_key', u'no_wait', u'arguments',) NAME = u'exchange.unbind' - INDEX = (40, 40) # (Class ID, Method ID) - BINARY_HEADER = b'\x00\x28\x00\x28' # CLASS ID + METHOD ID + INDEX = (40, 40) # (Class ID, Method ID) + BINARY_HEADER = b'\x00\x28\x00\x28' # CLASS ID + METHOD ID SENT_BY_CLIENT, SENT_BY_SERVER = True, False - IS_SIZE_STATIC = False # this means that argument part has always the same length + IS_SIZE_STATIC = False # this means that argument part has always the same length IS_CONTENT_STATIC = False # this means that argument part has always the same content # See constructor pydoc for details - FIELDS = [ + FIELDS = [ Field(u'reserved-1', u'short', u'short', reserved=True), Field(u'destination', u'exchange-name', u'shortstr', reserved=False), Field(u'source', u'exchange-name', u'shortstr', reserved=False), @@ -1577,24 +1601,25 @@ class ExchangeUnbind(AMQPMethodPayload): buf.write(self.routing_key) buf.write(struct.pack('!B', (self.no_wait << 0))) enframe_table(buf, self.arguments) - + def get_size(self): - return 6 + len(self.destination) + len(self.source) + len(self.routing_key) + frame_table_size(self.arguments) + return 6 + len(self.destination) + len(self.source) + len( + self.routing_key) + frame_table_size(self.arguments) @staticmethod def from_buffer(buf, start_offset): offset = start_offset s_len, = struct.unpack_from('!2xB', buf, offset) offset += 3 - destination = buf[offset:offset+s_len] + destination = buf[offset:offset + s_len] offset += s_len s_len, = struct.unpack_from('!B', buf, offset) offset += 1 - source = buf[offset:offset+s_len] + source = buf[offset:offset + s_len] offset += s_len s_len, = struct.unpack_from('!B', buf, offset) offset += 1 - routing_key = buf[offset:offset+s_len] + routing_key = buf[offset:offset + s_len] offset += s_len _bit, = struct.unpack_from('!B', buf, offset) offset += 0 @@ -1602,7 +1627,8 @@ class ExchangeUnbind(AMQPMethodPayload): offset += 1 arguments, delta = deframe_table(buf, offset) offset += delta - return ExchangeUnbind(destination, source, routing_key, no_wait, arguments) + return ExchangeUnbind(destination, source, routing_key, no_wait, + arguments) class ExchangeUnbindOk(AMQPMethodPayload): @@ -1615,12 +1641,12 @@ class ExchangeUnbindOk(AMQPMethodPayload): NAME = u'exchange.unbind-ok' - INDEX = (40, 51) # (Class ID, Method ID) - BINARY_HEADER = b'\x00\x28\x00\x33' # CLASS ID + METHOD ID + INDEX = (40, 51) # (Class ID, Method ID) + BINARY_HEADER = b'\x00\x28\x00\x33' # CLASS ID + METHOD ID SENT_BY_CLIENT, SENT_BY_SERVER = False, True - IS_SIZE_STATIC = True # this means that argument part has always the same length + IS_SIZE_STATIC = True # this means that argument part has always the same length IS_CONTENT_STATIC = True # this means that argument part has always the same content STATIC_CONTENT = b'\x00\x00\x00\x04\x00\x28\x00\x33\xCE' # spans LENGTH, CLASS ID, METHOD ID, ....., FRAME_END @@ -1629,7 +1655,6 @@ class ExchangeUnbindOk(AMQPMethodPayload): Create frame exchange.unbind-ok """ - @staticmethod def from_buffer(buf, start_offset): offset = start_offset @@ -1656,20 +1681,21 @@ class QueueBind(AMQPMethodPayload): are bound to a direct exchange and subscription queues are bound to a topic exchange. """ - __slots__ = (u'queue', u'exchange', u'routing_key', u'no_wait', u'arguments', ) + __slots__ = ( + u'queue', u'exchange', u'routing_key', u'no_wait', u'arguments',) NAME = u'queue.bind' - INDEX = (50, 20) # (Class ID, Method ID) - BINARY_HEADER = b'\x00\x32\x00\x14' # CLASS ID + METHOD ID + INDEX = (50, 20) # (Class ID, Method ID) + BINARY_HEADER = b'\x00\x32\x00\x14' # CLASS ID + METHOD ID SENT_BY_CLIENT, SENT_BY_SERVER = True, False - IS_SIZE_STATIC = False # this means that argument part has always the same length + IS_SIZE_STATIC = False # this means that argument part has always the same length IS_CONTENT_STATIC = False # this means that argument part has always the same content # See constructor pydoc for details - FIELDS = [ + FIELDS = [ Field(u'reserved-1', u'short', u'short', reserved=True), Field(u'queue', u'queue-name', u'shortstr', reserved=False), Field(u'exchange', u'exchange-name', u'shortstr', reserved=False), @@ -1719,24 +1745,25 @@ class QueueBind(AMQPMethodPayload): buf.write(self.routing_key) buf.write(struct.pack('!B', (self.no_wait << 0))) enframe_table(buf, self.arguments) - + def get_size(self): - return 6 + len(self.queue) + len(self.exchange) + len(self.routing_key) + frame_table_size(self.arguments) + return 6 + len(self.queue) + len(self.exchange) + len( + self.routing_key) + frame_table_size(self.arguments) @staticmethod def from_buffer(buf, start_offset): offset = start_offset s_len, = struct.unpack_from('!2xB', buf, offset) offset += 3 - queue = buf[offset:offset+s_len] + queue = buf[offset:offset + s_len] offset += s_len s_len, = struct.unpack_from('!B', buf, offset) offset += 1 - exchange = buf[offset:offset+s_len] + exchange = buf[offset:offset + s_len] offset += s_len s_len, = struct.unpack_from('!B', buf, offset) offset += 1 - routing_key = buf[offset:offset+s_len] + routing_key = buf[offset:offset + s_len] offset += s_len _bit, = struct.unpack_from('!B', buf, offset) offset += 0 @@ -1757,12 +1784,12 @@ class QueueBindOk(AMQPMethodPayload): NAME = u'queue.bind-ok' - INDEX = (50, 21) # (Class ID, Method ID) - BINARY_HEADER = b'\x00\x32\x00\x15' # CLASS ID + METHOD ID + INDEX = (50, 21) # (Class ID, Method ID) + BINARY_HEADER = b'\x00\x32\x00\x15' # CLASS ID + METHOD ID SENT_BY_CLIENT, SENT_BY_SERVER = False, True - IS_SIZE_STATIC = True # this means that argument part has always the same length + IS_SIZE_STATIC = True # this means that argument part has always the same length IS_CONTENT_STATIC = True # this means that argument part has always the same content STATIC_CONTENT = b'\x00\x00\x00\x04\x00\x32\x00\x15\xCE' # spans LENGTH, CLASS ID, METHOD ID, ....., FRAME_END @@ -1771,7 +1798,6 @@ class QueueBindOk(AMQPMethodPayload): Create frame queue.bind-ok """ - @staticmethod def from_buffer(buf, start_offset): offset = start_offset @@ -1786,20 +1812,22 @@ class QueueDeclare(AMQPMethodPayload): specify various properties that control the durability of the queue and its contents, and the level of sharing for the queue. """ - __slots__ = (u'queue', u'passive', u'durable', u'exclusive', u'auto_delete', u'no_wait', u'arguments', ) + __slots__ = ( + u'queue', u'passive', u'durable', u'exclusive', u'auto_delete', u'no_wait', + u'arguments',) NAME = u'queue.declare' - INDEX = (50, 10) # (Class ID, Method ID) - BINARY_HEADER = b'\x00\x32\x00\x0A' # CLASS ID + METHOD ID + INDEX = (50, 10) # (Class ID, Method ID) + BINARY_HEADER = b'\x00\x32\x00\x0A' # CLASS ID + METHOD ID SENT_BY_CLIENT, SENT_BY_SERVER = True, False - IS_SIZE_STATIC = False # this means that argument part has always the same length + IS_SIZE_STATIC = False # this means that argument part has always the same length IS_CONTENT_STATIC = False # this means that argument part has always the same content # See constructor pydoc for details - FIELDS = [ + FIELDS = [ Field(u'reserved-1', u'short', u'short', reserved=True), Field(u'queue', u'queue-name', u'shortstr', reserved=False), Field(u'passive', u'bit', u'bit', reserved=False), @@ -1810,7 +1838,8 @@ class QueueDeclare(AMQPMethodPayload): Field(u'arguments', u'table', u'table', reserved=False), ] - def __init__(self, queue, passive, durable, exclusive, auto_delete, no_wait, arguments): + def __init__(self, queue, passive, durable, exclusive, auto_delete, no_wait, + arguments): """ Create frame queue.declare @@ -1862,9 +1891,12 @@ class QueueDeclare(AMQPMethodPayload): buf.write(b'\x00\x00') buf.write(struct.pack('!B', len(self.queue))) buf.write(self.queue) - buf.write(struct.pack('!B', (self.passive << 0) | (self.durable << 1) | (self.exclusive << 2) | (self.auto_delete << 3) | (self.no_wait << 4))) + buf.write(struct.pack('!B', + (self.passive << 0) | (self.durable << 1) | ( + self.exclusive << 2) | (self.auto_delete << 3) | ( + self.no_wait << 4))) enframe_table(buf, self.arguments) - + def get_size(self): return 4 + len(self.queue) + frame_table_size(self.arguments) @@ -1873,7 +1905,7 @@ class QueueDeclare(AMQPMethodPayload): offset = start_offset s_len, = struct.unpack_from('!2xB', buf, offset) offset += 3 - queue = buf[offset:offset+s_len] + queue = buf[offset:offset + s_len] offset += s_len _bit, = struct.unpack_from('!B', buf, offset) offset += 0 @@ -1885,7 +1917,8 @@ class QueueDeclare(AMQPMethodPayload): offset += 1 arguments, delta = deframe_table(buf, offset) offset += delta - return QueueDeclare(queue, passive, durable, exclusive, auto_delete, no_wait, arguments) + return QueueDeclare(queue, passive, durable, exclusive, auto_delete, + no_wait, arguments) class QueueDelete(AMQPMethodPayload): @@ -1896,20 +1929,20 @@ class QueueDelete(AMQPMethodPayload): to a dead-letter queue if this is defined in the server configuration, and all consumers on the queue are cancelled. """ - __slots__ = (u'queue', u'if_unused', u'if_empty', u'no_wait', ) + __slots__ = (u'queue', u'if_unused', u'if_empty', u'no_wait',) NAME = u'queue.delete' - INDEX = (50, 40) # (Class ID, Method ID) - BINARY_HEADER = b'\x00\x32\x00\x28' # CLASS ID + METHOD ID + INDEX = (50, 40) # (Class ID, Method ID) + BINARY_HEADER = b'\x00\x32\x00\x28' # CLASS ID + METHOD ID SENT_BY_CLIENT, SENT_BY_SERVER = True, False - IS_SIZE_STATIC = False # this means that argument part has always the same length + IS_SIZE_STATIC = False # this means that argument part has always the same length IS_CONTENT_STATIC = False # this means that argument part has always the same content # See constructor pydoc for details - FIELDS = [ + FIELDS = [ Field(u'reserved-1', u'short', u'short', reserved=True), Field(u'queue', u'queue-name', u'shortstr', reserved=False), Field(u'if-unused', u'bit', u'bit', reserved=False), @@ -1942,8 +1975,10 @@ class QueueDelete(AMQPMethodPayload): buf.write(b'\x00\x00') buf.write(struct.pack('!B', len(self.queue))) buf.write(self.queue) - buf.write(struct.pack('!B', (self.if_unused << 0) | (self.if_empty << 1) | (self.no_wait << 2))) - + buf.write(struct.pack('!B', + (self.if_unused << 0) | (self.if_empty << 1) | ( + self.no_wait << 2))) + def get_size(self): return 4 + len(self.queue) @@ -1952,7 +1987,7 @@ class QueueDelete(AMQPMethodPayload): offset = start_offset s_len, = struct.unpack_from('!2xB', buf, offset) offset += 3 - queue = buf[offset:offset+s_len] + queue = buf[offset:offset + s_len] offset += s_len _bit, = struct.unpack_from('!B', buf, offset) offset += 0 @@ -1970,20 +2005,20 @@ class QueueDeclareOk(AMQPMethodPayload): This method confirms a Declare method and confirms the name of the queue, essential for automatically-named queues. """ - __slots__ = (u'queue', u'message_count', u'consumer_count', ) + __slots__ = (u'queue', u'message_count', u'consumer_count',) NAME = u'queue.declare-ok' - INDEX = (50, 11) # (Class ID, Method ID) - BINARY_HEADER = b'\x00\x32\x00\x0B' # CLASS ID + METHOD ID + INDEX = (50, 11) # (Class ID, Method ID) + BINARY_HEADER = b'\x00\x32\x00\x0B' # CLASS ID + METHOD ID SENT_BY_CLIENT, SENT_BY_SERVER = False, True - IS_SIZE_STATIC = False # this means that argument part has always the same length + IS_SIZE_STATIC = False # this means that argument part has always the same length IS_CONTENT_STATIC = False # this means that argument part has always the same content # See constructor pydoc for details - FIELDS = [ + FIELDS = [ Field(u'queue', u'queue-name', u'shortstr', reserved=False), Field(u'message-count', u'message-count', u'long', reserved=False), Field(u'consumer-count', u'long', u'long', reserved=False), @@ -2010,7 +2045,7 @@ class QueueDeclareOk(AMQPMethodPayload): buf.write(struct.pack('!B', len(self.queue))) buf.write(self.queue) buf.write(struct.pack('!II', self.message_count, self.consumer_count)) - + def get_size(self): return 9 + len(self.queue) @@ -2019,7 +2054,7 @@ class QueueDeclareOk(AMQPMethodPayload): offset = start_offset s_len, = struct.unpack_from('!B', buf, offset) offset += 1 - queue = buf[offset:offset+s_len] + queue = buf[offset:offset + s_len] offset += s_len message_count, consumer_count, = struct.unpack_from('!II', buf, offset) offset += 8 @@ -2032,20 +2067,20 @@ class QueueDeleteOk(AMQPMethodPayload): This method confirms the deletion of a queue. """ - __slots__ = (u'message_count', ) + __slots__ = (u'message_count',) NAME = u'queue.delete-ok' - INDEX = (50, 41) # (Class ID, Method ID) - BINARY_HEADER = b'\x00\x32\x00\x29' # CLASS ID + METHOD ID + INDEX = (50, 41) # (Class ID, Method ID) + BINARY_HEADER = b'\x00\x32\x00\x29' # CLASS ID + METHOD ID SENT_BY_CLIENT, SENT_BY_SERVER = False, True - IS_SIZE_STATIC = True # this means that argument part has always the same length + IS_SIZE_STATIC = True # this means that argument part has always the same length IS_CONTENT_STATIC = False # this means that argument part has always the same content # See constructor pydoc for details - FIELDS = [ + FIELDS = [ Field(u'message-count', u'message-count', u'long', reserved=False), ] @@ -2060,7 +2095,7 @@ class QueueDeleteOk(AMQPMethodPayload): def write_arguments(self, buf): buf.write(struct.pack('!I', self.message_count)) - + def get_size(self): return 4 @@ -2079,20 +2114,20 @@ class QueuePurge(AMQPMethodPayload): This method removes all messages from a queue which are not awaiting acknowledgment. """ - __slots__ = (u'queue', u'no_wait', ) + __slots__ = (u'queue', u'no_wait',) NAME = u'queue.purge' - INDEX = (50, 30) # (Class ID, Method ID) - BINARY_HEADER = b'\x00\x32\x00\x1E' # CLASS ID + METHOD ID + INDEX = (50, 30) # (Class ID, Method ID) + BINARY_HEADER = b'\x00\x32\x00\x1E' # CLASS ID + METHOD ID SENT_BY_CLIENT, SENT_BY_SERVER = True, False - IS_SIZE_STATIC = False # this means that argument part has always the same length + IS_SIZE_STATIC = False # this means that argument part has always the same length IS_CONTENT_STATIC = False # this means that argument part has always the same content # See constructor pydoc for details - FIELDS = [ + FIELDS = [ Field(u'reserved-1', u'short', u'short', reserved=True), Field(u'queue', u'queue-name', u'shortstr', reserved=False), Field(u'no-wait', u'no-wait', u'bit', reserved=False), @@ -2114,7 +2149,7 @@ class QueuePurge(AMQPMethodPayload): buf.write(struct.pack('!B', len(self.queue))) buf.write(self.queue) buf.write(struct.pack('!B', (self.no_wait << 0))) - + def get_size(self): return 4 + len(self.queue) @@ -2123,7 +2158,7 @@ class QueuePurge(AMQPMethodPayload): offset = start_offset s_len, = struct.unpack_from('!2xB', buf, offset) offset += 3 - queue = buf[offset:offset+s_len] + queue = buf[offset:offset + s_len] offset += s_len _bit, = struct.unpack_from('!B', buf, offset) offset += 0 @@ -2138,20 +2173,20 @@ class QueuePurgeOk(AMQPMethodPayload): This method confirms the purge of a queue. """ - __slots__ = (u'message_count', ) + __slots__ = (u'message_count',) NAME = u'queue.purge-ok' - INDEX = (50, 31) # (Class ID, Method ID) - BINARY_HEADER = b'\x00\x32\x00\x1F' # CLASS ID + METHOD ID + INDEX = (50, 31) # (Class ID, Method ID) + BINARY_HEADER = b'\x00\x32\x00\x1F' # CLASS ID + METHOD ID SENT_BY_CLIENT, SENT_BY_SERVER = False, True - IS_SIZE_STATIC = True # this means that argument part has always the same length + IS_SIZE_STATIC = True # this means that argument part has always the same length IS_CONTENT_STATIC = False # this means that argument part has always the same content # See constructor pydoc for details - FIELDS = [ + FIELDS = [ Field(u'message-count', u'message-count', u'long', reserved=False), ] @@ -2166,7 +2201,7 @@ class QueuePurgeOk(AMQPMethodPayload): def write_arguments(self, buf): buf.write(struct.pack('!I', self.message_count)) - + def get_size(self): return 4 @@ -2184,20 +2219,20 @@ class QueueUnbind(AMQPMethodPayload): This method unbinds a queue from an exchange. """ - __slots__ = (u'queue', u'exchange', u'routing_key', u'arguments', ) + __slots__ = (u'queue', u'exchange', u'routing_key', u'arguments',) NAME = u'queue.unbind' - INDEX = (50, 50) # (Class ID, Method ID) - BINARY_HEADER = b'\x00\x32\x00\x32' # CLASS ID + METHOD ID + INDEX = (50, 50) # (Class ID, Method ID) + BINARY_HEADER = b'\x00\x32\x00\x32' # CLASS ID + METHOD ID SENT_BY_CLIENT, SENT_BY_SERVER = True, False - IS_SIZE_STATIC = False # this means that argument part has always the same length + IS_SIZE_STATIC = False # this means that argument part has always the same length IS_CONTENT_STATIC = False # this means that argument part has always the same content # See constructor pydoc for details - FIELDS = [ + FIELDS = [ Field(u'reserved-1', u'short', u'short', reserved=True), Field(u'queue', u'queue-name', u'shortstr', reserved=False), Field(u'exchange', u'exchange-name', u'shortstr', reserved=False), @@ -2234,24 +2269,25 @@ class QueueUnbind(AMQPMethodPayload): buf.write(struct.pack('!B', len(self.routing_key))) buf.write(self.routing_key) enframe_table(buf, self.arguments) - + def get_size(self): - return 5 + len(self.queue) + len(self.exchange) + len(self.routing_key) + frame_table_size(self.arguments) + return 5 + len(self.queue) + len(self.exchange) + len( + self.routing_key) + frame_table_size(self.arguments) @staticmethod def from_buffer(buf, start_offset): offset = start_offset s_len, = struct.unpack_from('!2xB', buf, offset) offset += 3 - queue = buf[offset:offset+s_len] + queue = buf[offset:offset + s_len] offset += s_len s_len, = struct.unpack_from('!B', buf, offset) offset += 1 - exchange = buf[offset:offset+s_len] + exchange = buf[offset:offset + s_len] offset += s_len s_len, = struct.unpack_from('!B', buf, offset) offset += 1 - routing_key = buf[offset:offset+s_len] + routing_key = buf[offset:offset + s_len] offset += s_len arguments, delta = deframe_table(buf, offset) offset += delta @@ -2268,12 +2304,12 @@ class QueueUnbindOk(AMQPMethodPayload): NAME = u'queue.unbind-ok' - INDEX = (50, 51) # (Class ID, Method ID) - BINARY_HEADER = b'\x00\x32\x00\x33' # CLASS ID + METHOD ID + INDEX = (50, 51) # (Class ID, Method ID) + BINARY_HEADER = b'\x00\x32\x00\x33' # CLASS ID + METHOD ID SENT_BY_CLIENT, SENT_BY_SERVER = False, True - IS_SIZE_STATIC = True # this means that argument part has always the same length + IS_SIZE_STATIC = True # this means that argument part has always the same length IS_CONTENT_STATIC = True # this means that argument part has always the same content STATIC_CONTENT = b'\x00\x00\x00\x04\x00\x32\x00\x33\xCE' # spans LENGTH, CLASS ID, METHOD ID, ....., FRAME_END @@ -2282,7 +2318,6 @@ class QueueUnbindOk(AMQPMethodPayload): Create frame queue.unbind-ok """ - @staticmethod def from_buffer(buf, start_offset): offset = start_offset @@ -2354,48 +2389,68 @@ class BasicContentPropertyList(AMQPContentPropertyList): :type reserved: binary type (max length 255) (AMQP as shortstr) """ zpf = bytearray([ - (('content_type' in kwargs) << 7) | (('content_encoding' in kwargs) << 6) | (('headers' in kwargs) << 5) | (('delivery_mode' in kwargs) << 4) | (('priority' in kwargs) << 3) | (('correlation_id' in kwargs) << 2) | (('reply_to' in kwargs) << 1) | int('expiration' in kwargs), - (('message_id' in kwargs) << 7) | (('timestamp' in kwargs) << 6) | (('type_' in kwargs) << 5) | (('user_id' in kwargs) << 4) | (('app_id' in kwargs) << 3) | (('reserved' in kwargs) << 2) + (('content_type' in kwargs) << 7) | ( + ('content_encoding' in kwargs) << 6) | ( + ('headers' in kwargs) << 5) | (('delivery_mode' in kwargs) << 4) | ( + ('priority' in kwargs) << 3) | ( + ('correlation_id' in kwargs) << 2) | ( + ('reply_to' in kwargs) << 1) | int('expiration' in kwargs), + (('message_id' in kwargs) << 7) | (('timestamp' in kwargs) << 6) | ( + ('type_' in kwargs) << 5) | (('user_id' in kwargs) << 4) | ( + ('app_id' in kwargs) << 3) | (('reserved' in kwargs) << 2) ]) zpf = six.binary_type(zpf) -# If you know in advance what properties you will be using, use typized constructors like -# -# runs once -# my_type = BasicContentPropertyList.typize('content_type', 'content_encoding') -# -# runs many times -# props = my_type('text/plain', 'utf8') -# -# instead of -# -# # runs many times -# props = BasicContentPropertyList(content_type='text/plain', content_encoding='utf8') -# -# This way you will be faster. -# -# If you do not know in advance what properties you will be using, it is correct to use -# this constructor. + # If you know in advance what properties you will be using, use typized constructors like + # + # runs once + # my_type = BasicContentPropertyList.typize('content_type', 'content_encoding') + # + # runs many times + # props = my_type('text/plain', 'utf8') + # + # instead of + # + # # runs many times + # props = BasicContentPropertyList(content_type='text/plain', content_encoding='utf8') + # + # This way you will be faster. + # + # If you do not know in advance what properties you will be using, it is correct to use + # this constructor. if zpf in BasicContentPropertyList.PARTICULAR_CLASSES: return BasicContentPropertyList.PARTICULAR_CLASSES[zpf](**kwargs) else: - logger.debug('Property field (BasicContentPropertyList:%s) not seen yet, compiling', repr(zpf)) - c = compile_particular_content_property_list_class(zpf, BasicContentPropertyList.FIELDS) + logger.debug( + 'Property field (BasicContentPropertyList:%s) not seen yet, compiling', + repr(zpf)) + c = compile_particular_content_property_list_class(zpf, + BasicContentPropertyList.FIELDS) BasicContentPropertyList.PARTICULAR_CLASSES[zpf] = c return c(**kwargs) @staticmethod def typize(*fields): zpf = bytearray([ - (('content_type' in fields) << 7) | (('content_encoding' in fields) << 6) | (('headers' in fields) << 5) | (('delivery_mode' in fields) << 4) | (('priority' in fields) << 3) | (('correlation_id' in fields) << 2) | (('reply_to' in fields) << 1) | int('expiration' in kwargs), - (('message_id' in fields) << 7) | (('timestamp' in fields) << 6) | (('type_' in fields) << 5) | (('user_id' in fields) << 4) | (('app_id' in fields) << 3) | (('reserved' in fields) << 2) + (('content_type' in fields) << 7) | ( + ('content_encoding' in fields) << 6) | ( + ('headers' in fields) << 5) | (('delivery_mode' in fields) << 4) | ( + ('priority' in fields) << 3) | ( + ('correlation_id' in fields) << 2) | ( + ('reply_to' in fields) << 1) | int('expiration' in kwargs), + (('message_id' in fields) << 7) | (('timestamp' in fields) << 6) | ( + ('type_' in fields) << 5) | (('user_id' in fields) << 4) | ( + ('app_id' in fields) << 3) | (('reserved' in fields) << 2) ]) zpf = six.binary_type(zpf) if zpf in BasicContentPropertyList.PARTICULAR_CLASSES: return BasicContentPropertyList.PARTICULAR_CLASSES[zpf] else: - logger.debug('Property field (BasicContentPropertyList:%s) not seen yet, compiling', repr(zpf)) - c = compile_particular_content_property_list_class(zpf, BasicContentPropertyList.FIELDS) + logger.debug( + 'Property field (BasicContentPropertyList:%s) not seen yet, compiling', + repr(zpf)) + c = compile_particular_content_property_list_class(zpf, + BasicContentPropertyList.FIELDS) BasicContentPropertyList.PARTICULAR_CLASSES[zpf] = c return c @@ -2413,12 +2468,17 @@ class BasicContentPropertyList(AMQPContentPropertyList): else: while buf[offset + pfl - 1] & 1: pfl += 2 - zpf = BasicContentPropertyList.zero_property_flags(buf[offset:offset+pfl]).tobytes() + zpf = BasicContentPropertyList.zero_property_flags( + buf[offset:offset + pfl]).tobytes() if zpf in BasicContentPropertyList.PARTICULAR_CLASSES: - return BasicContentPropertyList.PARTICULAR_CLASSES[zpf].from_buffer(buf, offset) + return BasicContentPropertyList.PARTICULAR_CLASSES[zpf].from_buffer( + buf, offset) else: - logger.debug('Property field (BasicContentPropertyList:%s) not seen yet, compiling', repr(zpf)) - c = compile_particular_content_property_list_class(zpf, BasicContentPropertyList.FIELDS) + logger.debug( + 'Property field (BasicContentPropertyList:%s) not seen yet, compiling', + repr(zpf)) + c = compile_particular_content_property_list_class(zpf, + BasicContentPropertyList.FIELDS) BasicContentPropertyList.PARTICULAR_CLASSES[zpf] = c return c.from_buffer(buf, offset) @@ -2435,20 +2495,20 @@ class BasicAck(AMQPMethodPayload): The acknowledgement can be for a single message or a set of messages up to and including a specific message. """ - __slots__ = (u'delivery_tag', u'multiple', ) + __slots__ = (u'delivery_tag', u'multiple',) NAME = u'basic.ack' - INDEX = (60, 80) # (Class ID, Method ID) - BINARY_HEADER = b'\x00\x3C\x00\x50' # CLASS ID + METHOD ID + INDEX = (60, 80) # (Class ID, Method ID) + BINARY_HEADER = b'\x00\x3C\x00\x50' # CLASS ID + METHOD ID SENT_BY_CLIENT, SENT_BY_SERVER = True, True - IS_SIZE_STATIC = True # this means that argument part has always the same length + IS_SIZE_STATIC = True # this means that argument part has always the same length IS_CONTENT_STATIC = False # this means that argument part has always the same content # See constructor pydoc for details - FIELDS = [ + FIELDS = [ Field(u'delivery-tag', u'delivery-tag', u'longlong', reserved=False), Field(u'multiple', u'bit', u'bit', reserved=False), ] @@ -2472,7 +2532,7 @@ class BasicAck(AMQPMethodPayload): def write_arguments(self, buf): buf.write(struct.pack('!QB', self.delivery_tag, (self.multiple << 0))) - + def get_size(self): return 9 @@ -2494,20 +2554,22 @@ class BasicConsume(AMQPMethodPayload): messages from a specific queue. Consumers last as long as the channel they were declared on, or until the client cancels them. """ - __slots__ = (u'queue', u'consumer_tag', u'no_local', u'no_ack', u'exclusive', u'no_wait', u'arguments', ) + __slots__ = ( + u'queue', u'consumer_tag', u'no_local', u'no_ack', u'exclusive', u'no_wait', + u'arguments',) NAME = u'basic.consume' - INDEX = (60, 20) # (Class ID, Method ID) - BINARY_HEADER = b'\x00\x3C\x00\x14' # CLASS ID + METHOD ID + INDEX = (60, 20) # (Class ID, Method ID) + BINARY_HEADER = b'\x00\x3C\x00\x14' # CLASS ID + METHOD ID SENT_BY_CLIENT, SENT_BY_SERVER = True, False - IS_SIZE_STATIC = False # this means that argument part has always the same length + IS_SIZE_STATIC = False # this means that argument part has always the same length IS_CONTENT_STATIC = False # this means that argument part has always the same content # See constructor pydoc for details - FIELDS = [ + FIELDS = [ Field(u'reserved-1', u'short', u'short', reserved=True), Field(u'queue', u'queue-name', u'shortstr', reserved=False), Field(u'consumer-tag', u'consumer-tag', u'shortstr', reserved=False), @@ -2518,7 +2580,8 @@ class BasicConsume(AMQPMethodPayload): Field(u'arguments', u'table', u'table', reserved=False), ] - def __init__(self, queue, consumer_tag, no_local, no_ack, exclusive, no_wait, arguments): + def __init__(self, queue, consumer_tag, no_local, no_ack, exclusive, + no_wait, arguments): """ Create frame basic.consume @@ -2554,22 +2617,25 @@ class BasicConsume(AMQPMethodPayload): buf.write(self.queue) buf.write(struct.pack('!B', len(self.consumer_tag))) buf.write(self.consumer_tag) - buf.write(struct.pack('!B', (self.no_local << 0) | (self.no_ack << 1) | (self.exclusive << 2) | (self.no_wait << 3))) + buf.write(struct.pack('!B', + (self.no_local << 0) | (self.no_ack << 1) | ( + self.exclusive << 2) | (self.no_wait << 3))) enframe_table(buf, self.arguments) - + def get_size(self): - return 5 + len(self.queue) + len(self.consumer_tag) + frame_table_size(self.arguments) + return 5 + len(self.queue) + len(self.consumer_tag) + frame_table_size( + self.arguments) @staticmethod def from_buffer(buf, start_offset): offset = start_offset s_len, = struct.unpack_from('!2xB', buf, offset) offset += 3 - queue = buf[offset:offset+s_len] + queue = buf[offset:offset + s_len] offset += s_len s_len, = struct.unpack_from('!B', buf, offset) offset += 1 - consumer_tag = buf[offset:offset+s_len] + consumer_tag = buf[offset:offset + s_len] offset += s_len _bit, = struct.unpack_from('!B', buf, offset) offset += 0 @@ -2580,7 +2646,8 @@ class BasicConsume(AMQPMethodPayload): offset += 1 arguments, delta = deframe_table(buf, offset) offset += delta - return BasicConsume(queue, consumer_tag, no_local, no_ack, exclusive, no_wait, arguments) + return BasicConsume(queue, consumer_tag, no_local, no_ack, exclusive, + no_wait, arguments) class BasicCancel(AMQPMethodPayload): @@ -2602,20 +2669,20 @@ class BasicCancel(AMQPMethodPayload): capable of accepting the method, through some means of capability negotiation. """ - __slots__ = (u'consumer_tag', u'no_wait', ) + __slots__ = (u'consumer_tag', u'no_wait',) NAME = u'basic.cancel' - INDEX = (60, 30) # (Class ID, Method ID) - BINARY_HEADER = b'\x00\x3C\x00\x1E' # CLASS ID + METHOD ID + INDEX = (60, 30) # (Class ID, Method ID) + BINARY_HEADER = b'\x00\x3C\x00\x1E' # CLASS ID + METHOD ID SENT_BY_CLIENT, SENT_BY_SERVER = True, True - IS_SIZE_STATIC = False # this means that argument part has always the same length + IS_SIZE_STATIC = False # this means that argument part has always the same length IS_CONTENT_STATIC = False # this means that argument part has always the same content # See constructor pydoc for details - FIELDS = [ + FIELDS = [ Field(u'consumer-tag', u'consumer-tag', u'shortstr', reserved=False), Field(u'no-wait', u'no-wait', u'bit', reserved=False), ] @@ -2634,7 +2701,7 @@ class BasicCancel(AMQPMethodPayload): buf.write(struct.pack('!B', len(self.consumer_tag))) buf.write(self.consumer_tag) buf.write(struct.pack('!B', (self.no_wait << 0))) - + def get_size(self): return 2 + len(self.consumer_tag) @@ -2643,7 +2710,7 @@ class BasicCancel(AMQPMethodPayload): offset = start_offset s_len, = struct.unpack_from('!B', buf, offset) offset += 1 - consumer_tag = buf[offset:offset+s_len] + consumer_tag = buf[offset:offset + s_len] offset += s_len _bit, = struct.unpack_from('!B', buf, offset) offset += 0 @@ -2659,20 +2726,20 @@ class BasicConsumeOk(AMQPMethodPayload): The server provides the client with a consumer tag, which is used by the client for methods called on the consumer at a later stage. """ - __slots__ = (u'consumer_tag', ) + __slots__ = (u'consumer_tag',) NAME = u'basic.consume-ok' - INDEX = (60, 21) # (Class ID, Method ID) - BINARY_HEADER = b'\x00\x3C\x00\x15' # CLASS ID + METHOD ID + INDEX = (60, 21) # (Class ID, Method ID) + BINARY_HEADER = b'\x00\x3C\x00\x15' # CLASS ID + METHOD ID SENT_BY_CLIENT, SENT_BY_SERVER = False, True - IS_SIZE_STATIC = False # this means that argument part has always the same length + IS_SIZE_STATIC = False # this means that argument part has always the same length IS_CONTENT_STATIC = False # this means that argument part has always the same content # See constructor pydoc for details - FIELDS = [ + FIELDS = [ Field(u'consumer-tag', u'consumer-tag', u'shortstr', reserved=False), ] @@ -2688,7 +2755,7 @@ class BasicConsumeOk(AMQPMethodPayload): def write_arguments(self, buf): buf.write(struct.pack('!B', len(self.consumer_tag))) buf.write(self.consumer_tag) - + def get_size(self): return 1 + len(self.consumer_tag) @@ -2697,7 +2764,7 @@ class BasicConsumeOk(AMQPMethodPayload): offset = start_offset s_len, = struct.unpack_from('!B', buf, offset) offset += 1 - consumer_tag = buf[offset:offset+s_len] + consumer_tag = buf[offset:offset + s_len] offset += s_len return BasicConsumeOk(consumer_tag) @@ -2708,20 +2775,20 @@ class BasicCancelOk(AMQPMethodPayload): This method confirms that the cancellation was completed. """ - __slots__ = (u'consumer_tag', ) + __slots__ = (u'consumer_tag',) NAME = u'basic.cancel-ok' - INDEX = (60, 31) # (Class ID, Method ID) - BINARY_HEADER = b'\x00\x3C\x00\x1F' # CLASS ID + METHOD ID + INDEX = (60, 31) # (Class ID, Method ID) + BINARY_HEADER = b'\x00\x3C\x00\x1F' # CLASS ID + METHOD ID SENT_BY_CLIENT, SENT_BY_SERVER = True, True - IS_SIZE_STATIC = False # this means that argument part has always the same length + IS_SIZE_STATIC = False # this means that argument part has always the same length IS_CONTENT_STATIC = False # this means that argument part has always the same content # See constructor pydoc for details - FIELDS = [ + FIELDS = [ Field(u'consumer-tag', u'consumer-tag', u'shortstr', reserved=False), ] @@ -2736,7 +2803,7 @@ class BasicCancelOk(AMQPMethodPayload): def write_arguments(self, buf): buf.write(struct.pack('!B', len(self.consumer_tag))) buf.write(self.consumer_tag) - + def get_size(self): return 1 + len(self.consumer_tag) @@ -2745,7 +2812,7 @@ class BasicCancelOk(AMQPMethodPayload): offset = start_offset s_len, = struct.unpack_from('!B', buf, offset) offset += 1 - consumer_tag = buf[offset:offset+s_len] + consumer_tag = buf[offset:offset + s_len] offset += s_len return BasicCancelOk(consumer_tag) @@ -2759,20 +2826,21 @@ class BasicDeliver(AMQPMethodPayload): the server responds with Deliver methods as and when messages arrive for that consumer. """ - __slots__ = (u'consumer_tag', u'delivery_tag', u'redelivered', u'exchange', u'routing_key', ) + __slots__ = (u'consumer_tag', u'delivery_tag', u'redelivered', u'exchange', + u'routing_key',) NAME = u'basic.deliver' - INDEX = (60, 60) # (Class ID, Method ID) - BINARY_HEADER = b'\x00\x3C\x00\x3C' # CLASS ID + METHOD ID + INDEX = (60, 60) # (Class ID, Method ID) + BINARY_HEADER = b'\x00\x3C\x00\x3C' # CLASS ID + METHOD ID SENT_BY_CLIENT, SENT_BY_SERVER = False, True - IS_SIZE_STATIC = False # this means that argument part has always the same length + IS_SIZE_STATIC = False # this means that argument part has always the same length IS_CONTENT_STATIC = False # this means that argument part has always the same content # See constructor pydoc for details - FIELDS = [ + FIELDS = [ Field(u'consumer-tag', u'consumer-tag', u'shortstr', reserved=False), Field(u'delivery-tag', u'delivery-tag', u'longlong', reserved=False), Field(u'redelivered', u'redelivered', u'bit', reserved=False), @@ -2780,7 +2848,8 @@ class BasicDeliver(AMQPMethodPayload): Field(u'routing-key', u'shortstr', u'shortstr', reserved=False), ] - def __init__(self, consumer_tag, delivery_tag, redelivered, exchange, routing_key): + def __init__(self, consumer_tag, delivery_tag, redelivered, exchange, + routing_key): """ Create frame basic.deliver @@ -2803,20 +2872,23 @@ class BasicDeliver(AMQPMethodPayload): def write_arguments(self, buf): buf.write(struct.pack('!B', len(self.consumer_tag))) buf.write(self.consumer_tag) - buf.write(struct.pack('!QBB', self.delivery_tag, (self.redelivered << 0), len(self.exchange))) + buf.write( + struct.pack('!QBB', self.delivery_tag, (self.redelivered << 0), + len(self.exchange))) buf.write(self.exchange) buf.write(struct.pack('!B', len(self.routing_key))) buf.write(self.routing_key) - + def get_size(self): - return 12 + len(self.consumer_tag) + len(self.exchange) + len(self.routing_key) + return 12 + len(self.consumer_tag) + len(self.exchange) + len( + self.routing_key) @staticmethod def from_buffer(buf, start_offset): offset = start_offset s_len, = struct.unpack_from('!B', buf, offset) offset += 1 - consumer_tag = buf[offset:offset+s_len] + consumer_tag = buf[offset:offset + s_len] offset += s_len delivery_tag, _bit, = struct.unpack_from('!QB', buf, offset) offset += 8 @@ -2824,13 +2896,14 @@ class BasicDeliver(AMQPMethodPayload): offset += 1 s_len, = struct.unpack_from('!B', buf, offset) offset += 1 - exchange = buf[offset:offset+s_len] + exchange = buf[offset:offset + s_len] offset += s_len s_len, = struct.unpack_from('!B', buf, offset) offset += 1 - routing_key = buf[offset:offset+s_len] + routing_key = buf[offset:offset + s_len] offset += s_len - return BasicDeliver(consumer_tag, delivery_tag, redelivered, exchange, routing_key) + return BasicDeliver(consumer_tag, delivery_tag, redelivered, exchange, + routing_key) class BasicGet(AMQPMethodPayload): @@ -2841,20 +2914,20 @@ class BasicGet(AMQPMethodPayload): dialogue that is designed for specific types of application where synchronous functionality is more important than performance. """ - __slots__ = (u'queue', u'no_ack', ) + __slots__ = (u'queue', u'no_ack',) NAME = u'basic.get' - INDEX = (60, 70) # (Class ID, Method ID) - BINARY_HEADER = b'\x00\x3C\x00\x46' # CLASS ID + METHOD ID + INDEX = (60, 70) # (Class ID, Method ID) + BINARY_HEADER = b'\x00\x3C\x00\x46' # CLASS ID + METHOD ID SENT_BY_CLIENT, SENT_BY_SERVER = True, False - IS_SIZE_STATIC = False # this means that argument part has always the same length + IS_SIZE_STATIC = False # this means that argument part has always the same length IS_CONTENT_STATIC = False # this means that argument part has always the same content # See constructor pydoc for details - FIELDS = [ + FIELDS = [ Field(u'reserved-1', u'short', u'short', reserved=True), Field(u'queue', u'queue-name', u'shortstr', reserved=False), Field(u'no-ack', u'no-ack', u'bit', reserved=False), @@ -2876,7 +2949,7 @@ class BasicGet(AMQPMethodPayload): buf.write(struct.pack('!B', len(self.queue))) buf.write(self.queue) buf.write(struct.pack('!B', (self.no_ack << 0))) - + def get_size(self): return 4 + len(self.queue) @@ -2885,7 +2958,7 @@ class BasicGet(AMQPMethodPayload): offset = start_offset s_len, = struct.unpack_from('!2xB', buf, offset) offset += 3 - queue = buf[offset:offset+s_len] + queue = buf[offset:offset + s_len] offset += s_len _bit, = struct.unpack_from('!B', buf, offset) offset += 0 @@ -2902,20 +2975,21 @@ class BasicGetOk(AMQPMethodPayload): delivered by 'get-ok' must be acknowledged unless the no-ack option was set in the get method. """ - __slots__ = (u'delivery_tag', u'redelivered', u'exchange', u'routing_key', u'message_count', ) + __slots__ = (u'delivery_tag', u'redelivered', u'exchange', u'routing_key', + u'message_count',) NAME = u'basic.get-ok' - INDEX = (60, 71) # (Class ID, Method ID) - BINARY_HEADER = b'\x00\x3C\x00\x47' # CLASS ID + METHOD ID + INDEX = (60, 71) # (Class ID, Method ID) + BINARY_HEADER = b'\x00\x3C\x00\x47' # CLASS ID + METHOD ID SENT_BY_CLIENT, SENT_BY_SERVER = False, True - IS_SIZE_STATIC = False # this means that argument part has always the same length + IS_SIZE_STATIC = False # this means that argument part has always the same length IS_CONTENT_STATIC = False # this means that argument part has always the same content # See constructor pydoc for details - FIELDS = [ + FIELDS = [ Field(u'delivery-tag', u'delivery-tag', u'longlong', reserved=False), Field(u'redelivered', u'redelivered', u'bit', reserved=False), Field(u'exchange', u'exchange-name', u'shortstr', reserved=False), @@ -2923,7 +2997,8 @@ class BasicGetOk(AMQPMethodPayload): Field(u'message-count', u'message-count', u'long', reserved=False), ] - def __init__(self, delivery_tag, redelivered, exchange, routing_key, message_count): + def __init__(self, delivery_tag, redelivered, exchange, routing_key, + message_count): """ Create frame basic.get-ok @@ -2944,12 +3019,14 @@ class BasicGetOk(AMQPMethodPayload): self.message_count = message_count def write_arguments(self, buf): - buf.write(struct.pack('!QBB', self.delivery_tag, (self.redelivered << 0), len(self.exchange))) + buf.write( + struct.pack('!QBB', self.delivery_tag, (self.redelivered << 0), + len(self.exchange))) buf.write(self.exchange) buf.write(struct.pack('!B', len(self.routing_key))) buf.write(self.routing_key) buf.write(struct.pack('!I', self.message_count)) - + def get_size(self): return 15 + len(self.exchange) + len(self.routing_key) @@ -2962,15 +3039,16 @@ class BasicGetOk(AMQPMethodPayload): offset += 1 s_len, = struct.unpack_from('!B', buf, offset) offset += 1 - exchange = buf[offset:offset+s_len] + exchange = buf[offset:offset + s_len] offset += s_len s_len, = struct.unpack_from('!B', buf, offset) offset += 1 - routing_key = buf[offset:offset+s_len] + routing_key = buf[offset:offset + s_len] offset += s_len message_count, = struct.unpack_from('!I', buf, offset) offset += 4 - return BasicGetOk(delivery_tag, redelivered, exchange, routing_key, message_count) + return BasicGetOk(delivery_tag, redelivered, exchange, routing_key, + message_count) class BasicGetEmpty(AMQPMethodPayload): @@ -2984,17 +3062,17 @@ class BasicGetEmpty(AMQPMethodPayload): NAME = u'basic.get-empty' - INDEX = (60, 72) # (Class ID, Method ID) - BINARY_HEADER = b'\x00\x3C\x00\x48' # CLASS ID + METHOD ID + INDEX = (60, 72) # (Class ID, Method ID) + BINARY_HEADER = b'\x00\x3C\x00\x48' # CLASS ID + METHOD ID SENT_BY_CLIENT, SENT_BY_SERVER = False, True - IS_SIZE_STATIC = False # this means that argument part has always the same length + IS_SIZE_STATIC = False # this means that argument part has always the same length IS_CONTENT_STATIC = True # this means that argument part has always the same content STATIC_CONTENT = b'\x00\x00\x00\x0D\x00\x3C\x00\x48\x00\xCE' # spans LENGTH, CLASS ID, METHOD ID, ....., FRAME_END # See constructor pydoc for details - FIELDS = [ + FIELDS = [ Field(u'reserved-1', u'shortstr', u'shortstr', reserved=True), ] @@ -3003,13 +3081,12 @@ class BasicGetEmpty(AMQPMethodPayload): Create frame basic.get-empty """ - @staticmethod def from_buffer(buf, start_offset): offset = start_offset s_len, = struct.unpack_from('!B', buf, offset) offset += 1 - offset += s_len # reserved field! + offset += s_len # reserved field! return BasicGetEmpty() @@ -3024,20 +3101,20 @@ class BasicNack(AMQPMethodPayload): confirm mode of unhandled messages. If a publisher receives this method, it probably needs to republish the offending messages. """ - __slots__ = (u'delivery_tag', u'multiple', u'requeue', ) + __slots__ = (u'delivery_tag', u'multiple', u'requeue',) NAME = u'basic.nack' - INDEX = (60, 120) # (Class ID, Method ID) - BINARY_HEADER = b'\x00\x3C\x00\x78' # CLASS ID + METHOD ID + INDEX = (60, 120) # (Class ID, Method ID) + BINARY_HEADER = b'\x00\x3C\x00\x78' # CLASS ID + METHOD ID SENT_BY_CLIENT, SENT_BY_SERVER = True, True - IS_SIZE_STATIC = True # this means that argument part has always the same length + IS_SIZE_STATIC = True # this means that argument part has always the same length IS_CONTENT_STATIC = False # this means that argument part has always the same content # See constructor pydoc for details - FIELDS = [ + FIELDS = [ Field(u'delivery-tag', u'delivery-tag', u'longlong', reserved=False), Field(u'multiple', u'bit', u'bit', reserved=False), Field(u'requeue', u'bit', u'bit', reserved=False), @@ -3067,8 +3144,9 @@ class BasicNack(AMQPMethodPayload): self.requeue = requeue def write_arguments(self, buf): - buf.write(struct.pack('!QB', self.delivery_tag, (self.multiple << 0) | (self.requeue << 1))) - + buf.write(struct.pack('!QB', self.delivery_tag, + (self.multiple << 0) | (self.requeue << 1))) + def get_size(self): return 9 @@ -3091,20 +3169,20 @@ class BasicPublish(AMQPMethodPayload): to queues as defined by the exchange configuration and distributed to any active consumers when the transaction, if any, is committed. """ - __slots__ = (u'exchange', u'routing_key', u'mandatory', u'immediate', ) + __slots__ = (u'exchange', u'routing_key', u'mandatory', u'immediate',) NAME = u'basic.publish' - INDEX = (60, 40) # (Class ID, Method ID) - BINARY_HEADER = b'\x00\x3C\x00\x28' # CLASS ID + METHOD ID + INDEX = (60, 40) # (Class ID, Method ID) + BINARY_HEADER = b'\x00\x3C\x00\x28' # CLASS ID + METHOD ID SENT_BY_CLIENT, SENT_BY_SERVER = True, False - IS_SIZE_STATIC = False # this means that argument part has always the same length + IS_SIZE_STATIC = False # this means that argument part has always the same length IS_CONTENT_STATIC = False # this means that argument part has always the same content # See constructor pydoc for details - FIELDS = [ + FIELDS = [ Field(u'reserved-1', u'short', u'short', reserved=True), Field(u'exchange', u'exchange-name', u'shortstr', reserved=False), Field(u'routing-key', u'shortstr', u'shortstr', reserved=False), @@ -3147,8 +3225,9 @@ class BasicPublish(AMQPMethodPayload): buf.write(self.exchange) buf.write(struct.pack('!B', len(self.routing_key))) buf.write(self.routing_key) - buf.write(struct.pack('!B', (self.mandatory << 0) | (self.immediate << 1))) - + buf.write( + struct.pack('!B', (self.mandatory << 0) | (self.immediate << 1))) + def get_size(self): return 5 + len(self.exchange) + len(self.routing_key) @@ -3157,11 +3236,11 @@ class BasicPublish(AMQPMethodPayload): offset = start_offset s_len, = struct.unpack_from('!2xB', buf, offset) offset += 3 - exchange = buf[offset:offset+s_len] + exchange = buf[offset:offset + s_len] offset += s_len s_len, = struct.unpack_from('!B', buf, offset) offset += 1 - routing_key = buf[offset:offset+s_len] + routing_key = buf[offset:offset + s_len] offset += s_len _bit, = struct.unpack_from('!B', buf, offset) offset += 0 @@ -3181,20 +3260,20 @@ class BasicQos(AMQPMethodPayload): qos method could in principle apply to both peers, it is currently meaningful only for the server. """ - __slots__ = (u'prefetch_size', u'prefetch_count', u'global_', ) + __slots__ = (u'prefetch_size', u'prefetch_count', u'global_',) NAME = u'basic.qos' - INDEX = (60, 10) # (Class ID, Method ID) - BINARY_HEADER = b'\x00\x3C\x00\x0A' # CLASS ID + METHOD ID + INDEX = (60, 10) # (Class ID, Method ID) + BINARY_HEADER = b'\x00\x3C\x00\x0A' # CLASS ID + METHOD ID SENT_BY_CLIENT, SENT_BY_SERVER = True, False - IS_SIZE_STATIC = True # this means that argument part has always the same length + IS_SIZE_STATIC = True # this means that argument part has always the same length IS_CONTENT_STATIC = False # this means that argument part has always the same content # See constructor pydoc for details - FIELDS = [ + FIELDS = [ Field(u'prefetch-size', u'long', u'long', reserved=False), Field(u'prefetch-count', u'short', u'short', reserved=False), Field(u'global', u'bit', u'bit', reserved=False), @@ -3236,15 +3315,17 @@ class BasicQos(AMQPMethodPayload): self.global_ = global_ def write_arguments(self, buf): - buf.write(struct.pack('!IHB', self.prefetch_size, self.prefetch_count, (self.global_ << 0))) - + buf.write(struct.pack('!IHB', self.prefetch_size, self.prefetch_count, + (self.global_ << 0))) + def get_size(self): return 7 @staticmethod def from_buffer(buf, start_offset): offset = start_offset - prefetch_size, prefetch_count, _bit, = struct.unpack_from('!IHB', buf, offset) + prefetch_size, prefetch_count, _bit, = struct.unpack_from('!IHB', buf, + offset) offset += 6 global_ = bool(_bit >> 0) offset += 1 @@ -3263,12 +3344,12 @@ class BasicQosOk(AMQPMethodPayload): NAME = u'basic.qos-ok' - INDEX = (60, 11) # (Class ID, Method ID) - BINARY_HEADER = b'\x00\x3C\x00\x0B' # CLASS ID + METHOD ID + INDEX = (60, 11) # (Class ID, Method ID) + BINARY_HEADER = b'\x00\x3C\x00\x0B' # CLASS ID + METHOD ID SENT_BY_CLIENT, SENT_BY_SERVER = False, True - IS_SIZE_STATIC = True # this means that argument part has always the same length + IS_SIZE_STATIC = True # this means that argument part has always the same length IS_CONTENT_STATIC = True # this means that argument part has always the same content STATIC_CONTENT = b'\x00\x00\x00\x04\x00\x3C\x00\x0B\xCE' # spans LENGTH, CLASS ID, METHOD ID, ....., FRAME_END @@ -3277,7 +3358,6 @@ class BasicQosOk(AMQPMethodPayload): Create frame basic.qos-ok """ - @staticmethod def from_buffer(buf, start_offset): offset = start_offset @@ -3293,20 +3373,20 @@ class BasicReturn(AMQPMethodPayload): reply code and text provide information about the reason that the message was undeliverable. """ - __slots__ = (u'reply_code', u'reply_text', u'exchange', u'routing_key', ) + __slots__ = (u'reply_code', u'reply_text', u'exchange', u'routing_key',) NAME = u'basic.return' - INDEX = (60, 50) # (Class ID, Method ID) - BINARY_HEADER = b'\x00\x3C\x00\x32' # CLASS ID + METHOD ID + INDEX = (60, 50) # (Class ID, Method ID) + BINARY_HEADER = b'\x00\x3C\x00\x32' # CLASS ID + METHOD ID SENT_BY_CLIENT, SENT_BY_SERVER = False, True - IS_SIZE_STATIC = False # this means that argument part has always the same length + IS_SIZE_STATIC = False # this means that argument part has always the same length IS_CONTENT_STATIC = False # this means that argument part has always the same content # See constructor pydoc for details - FIELDS = [ + FIELDS = [ Field(u'reply-code', u'reply-code', u'short', reserved=False), Field(u'reply-text', u'reply-text', u'shortstr', reserved=False), Field(u'exchange', u'exchange-name', u'shortstr', reserved=False), @@ -3338,24 +3418,25 @@ class BasicReturn(AMQPMethodPayload): buf.write(self.exchange) buf.write(struct.pack('!B', len(self.routing_key))) buf.write(self.routing_key) - + def get_size(self): - return 5 + len(self.reply_text) + len(self.exchange) + len(self.routing_key) + return 5 + len(self.reply_text) + len(self.exchange) + len( + self.routing_key) @staticmethod def from_buffer(buf, start_offset): offset = start_offset reply_code, s_len, = struct.unpack_from('!HB', buf, offset) offset += 3 - reply_text = buf[offset:offset+s_len] + reply_text = buf[offset:offset + s_len] offset += s_len s_len, = struct.unpack_from('!B', buf, offset) offset += 1 - exchange = buf[offset:offset+s_len] + exchange = buf[offset:offset + s_len] offset += s_len s_len, = struct.unpack_from('!B', buf, offset) offset += 1 - routing_key = buf[offset:offset+s_len] + routing_key = buf[offset:offset + s_len] offset += s_len return BasicReturn(reply_code, reply_text, exchange, routing_key) @@ -3368,20 +3449,20 @@ class BasicReject(AMQPMethodPayload): cancel large incoming messages, or return untreatable messages to their original queue. """ - __slots__ = (u'delivery_tag', u'requeue', ) + __slots__ = (u'delivery_tag', u'requeue',) NAME = u'basic.reject' - INDEX = (60, 90) # (Class ID, Method ID) - BINARY_HEADER = b'\x00\x3C\x00\x5A' # CLASS ID + METHOD ID + INDEX = (60, 90) # (Class ID, Method ID) + BINARY_HEADER = b'\x00\x3C\x00\x5A' # CLASS ID + METHOD ID SENT_BY_CLIENT, SENT_BY_SERVER = True, False - IS_SIZE_STATIC = True # this means that argument part has always the same length + IS_SIZE_STATIC = True # this means that argument part has always the same length IS_CONTENT_STATIC = False # this means that argument part has always the same content # See constructor pydoc for details - FIELDS = [ + FIELDS = [ Field(u'delivery-tag', u'delivery-tag', u'longlong', reserved=False), Field(u'requeue', u'bit', u'bit', reserved=False), ] @@ -3401,7 +3482,7 @@ class BasicReject(AMQPMethodPayload): def write_arguments(self, buf): buf.write(struct.pack('!QB', self.delivery_tag, (self.requeue << 0))) - + def get_size(self): return 9 @@ -3423,20 +3504,20 @@ class BasicRecoverAsync(AMQPMethodPayload): specified channel. Zero or more messages may be redelivered. This method is deprecated in favour of the synchronous Recover/Recover-Ok. """ - __slots__ = (u'requeue', ) + __slots__ = (u'requeue',) NAME = u'basic.recover-async' - INDEX = (60, 100) # (Class ID, Method ID) - BINARY_HEADER = b'\x00\x3C\x00\x64' # CLASS ID + METHOD ID + INDEX = (60, 100) # (Class ID, Method ID) + BINARY_HEADER = b'\x00\x3C\x00\x64' # CLASS ID + METHOD ID SENT_BY_CLIENT, SENT_BY_SERVER = True, False - IS_SIZE_STATIC = True # this means that argument part has always the same length + IS_SIZE_STATIC = True # this means that argument part has always the same length IS_CONTENT_STATIC = False # this means that argument part has always the same content # See constructor pydoc for details - FIELDS = [ + FIELDS = [ Field(u'requeue', u'bit', u'bit', reserved=False), ] @@ -3454,7 +3535,7 @@ class BasicRecoverAsync(AMQPMethodPayload): def write_arguments(self, buf): buf.write(struct.pack('!B', (self.requeue << 0))) - + def get_size(self): return 1 @@ -3476,20 +3557,20 @@ class BasicRecover(AMQPMethodPayload): specified channel. Zero or more messages may be redelivered. This method replaces the asynchronous Recover. """ - __slots__ = (u'requeue', ) + __slots__ = (u'requeue',) NAME = u'basic.recover' - INDEX = (60, 110) # (Class ID, Method ID) - BINARY_HEADER = b'\x00\x3C\x00\x6E' # CLASS ID + METHOD ID + INDEX = (60, 110) # (Class ID, Method ID) + BINARY_HEADER = b'\x00\x3C\x00\x6E' # CLASS ID + METHOD ID SENT_BY_CLIENT, SENT_BY_SERVER = True, False - IS_SIZE_STATIC = True # this means that argument part has always the same length + IS_SIZE_STATIC = True # this means that argument part has always the same length IS_CONTENT_STATIC = False # this means that argument part has always the same content # See constructor pydoc for details - FIELDS = [ + FIELDS = [ Field(u'requeue', u'bit', u'bit', reserved=False), ] @@ -3507,7 +3588,7 @@ class BasicRecover(AMQPMethodPayload): def write_arguments(self, buf): buf.write(struct.pack('!B', (self.requeue << 0))) - + def get_size(self): return 1 @@ -3531,12 +3612,12 @@ class BasicRecoverOk(AMQPMethodPayload): NAME = u'basic.recover-ok' - INDEX = (60, 111) # (Class ID, Method ID) - BINARY_HEADER = b'\x00\x3C\x00\x6F' # CLASS ID + METHOD ID + INDEX = (60, 111) # (Class ID, Method ID) + BINARY_HEADER = b'\x00\x3C\x00\x6F' # CLASS ID + METHOD ID SENT_BY_CLIENT, SENT_BY_SERVER = False, True - IS_SIZE_STATIC = True # this means that argument part has always the same length + IS_SIZE_STATIC = True # this means that argument part has always the same length IS_CONTENT_STATIC = True # this means that argument part has always the same content STATIC_CONTENT = b'\x00\x00\x00\x04\x00\x3C\x00\x6F\xCE' # spans LENGTH, CLASS ID, METHOD ID, ....., FRAME_END @@ -3545,7 +3626,6 @@ class BasicRecoverOk(AMQPMethodPayload): Create frame basic.recover-ok """ - @staticmethod def from_buffer(buf, start_offset): offset = start_offset @@ -3580,12 +3660,12 @@ class TxCommit(AMQPMethodPayload): NAME = u'tx.commit' - INDEX = (90, 20) # (Class ID, Method ID) - BINARY_HEADER = b'\x00\x5A\x00\x14' # CLASS ID + METHOD ID + INDEX = (90, 20) # (Class ID, Method ID) + BINARY_HEADER = b'\x00\x5A\x00\x14' # CLASS ID + METHOD ID SENT_BY_CLIENT, SENT_BY_SERVER = True, False - IS_SIZE_STATIC = True # this means that argument part has always the same length + IS_SIZE_STATIC = True # this means that argument part has always the same length IS_CONTENT_STATIC = True # this means that argument part has always the same content STATIC_CONTENT = b'\x00\x00\x00\x04\x00\x5A\x00\x14\xCE' # spans LENGTH, CLASS ID, METHOD ID, ....., FRAME_END @@ -3594,7 +3674,6 @@ class TxCommit(AMQPMethodPayload): Create frame tx.commit """ - @staticmethod def from_buffer(buf, start_offset): offset = start_offset @@ -3612,12 +3691,12 @@ class TxCommitOk(AMQPMethodPayload): NAME = u'tx.commit-ok' - INDEX = (90, 21) # (Class ID, Method ID) - BINARY_HEADER = b'\x00\x5A\x00\x15' # CLASS ID + METHOD ID + INDEX = (90, 21) # (Class ID, Method ID) + BINARY_HEADER = b'\x00\x5A\x00\x15' # CLASS ID + METHOD ID SENT_BY_CLIENT, SENT_BY_SERVER = False, True - IS_SIZE_STATIC = True # this means that argument part has always the same length + IS_SIZE_STATIC = True # this means that argument part has always the same length IS_CONTENT_STATIC = True # this means that argument part has always the same content STATIC_CONTENT = b'\x00\x00\x00\x04\x00\x5A\x00\x15\xCE' # spans LENGTH, CLASS ID, METHOD ID, ....., FRAME_END @@ -3626,7 +3705,6 @@ class TxCommitOk(AMQPMethodPayload): Create frame tx.commit-ok """ - @staticmethod def from_buffer(buf, start_offset): offset = start_offset @@ -3646,12 +3724,12 @@ class TxRollback(AMQPMethodPayload): NAME = u'tx.rollback' - INDEX = (90, 30) # (Class ID, Method ID) - BINARY_HEADER = b'\x00\x5A\x00\x1E' # CLASS ID + METHOD ID + INDEX = (90, 30) # (Class ID, Method ID) + BINARY_HEADER = b'\x00\x5A\x00\x1E' # CLASS ID + METHOD ID SENT_BY_CLIENT, SENT_BY_SERVER = True, False - IS_SIZE_STATIC = True # this means that argument part has always the same length + IS_SIZE_STATIC = True # this means that argument part has always the same length IS_CONTENT_STATIC = True # this means that argument part has always the same content STATIC_CONTENT = b'\x00\x00\x00\x04\x00\x5A\x00\x1E\xCE' # spans LENGTH, CLASS ID, METHOD ID, ....., FRAME_END @@ -3660,7 +3738,6 @@ class TxRollback(AMQPMethodPayload): Create frame tx.rollback """ - @staticmethod def from_buffer(buf, start_offset): offset = start_offset @@ -3678,12 +3755,12 @@ class TxRollbackOk(AMQPMethodPayload): NAME = u'tx.rollback-ok' - INDEX = (90, 31) # (Class ID, Method ID) - BINARY_HEADER = b'\x00\x5A\x00\x1F' # CLASS ID + METHOD ID + INDEX = (90, 31) # (Class ID, Method ID) + BINARY_HEADER = b'\x00\x5A\x00\x1F' # CLASS ID + METHOD ID SENT_BY_CLIENT, SENT_BY_SERVER = False, True - IS_SIZE_STATIC = True # this means that argument part has always the same length + IS_SIZE_STATIC = True # this means that argument part has always the same length IS_CONTENT_STATIC = True # this means that argument part has always the same content STATIC_CONTENT = b'\x00\x00\x00\x04\x00\x5A\x00\x1F\xCE' # spans LENGTH, CLASS ID, METHOD ID, ....., FRAME_END @@ -3692,7 +3769,6 @@ class TxRollbackOk(AMQPMethodPayload): Create frame tx.rollback-ok """ - @staticmethod def from_buffer(buf, start_offset): offset = start_offset @@ -3710,12 +3786,12 @@ class TxSelect(AMQPMethodPayload): NAME = u'tx.select' - INDEX = (90, 10) # (Class ID, Method ID) - BINARY_HEADER = b'\x00\x5A\x00\x0A' # CLASS ID + METHOD ID + INDEX = (90, 10) # (Class ID, Method ID) + BINARY_HEADER = b'\x00\x5A\x00\x0A' # CLASS ID + METHOD ID SENT_BY_CLIENT, SENT_BY_SERVER = True, False - IS_SIZE_STATIC = True # this means that argument part has always the same length + IS_SIZE_STATIC = True # this means that argument part has always the same length IS_CONTENT_STATIC = True # this means that argument part has always the same content STATIC_CONTENT = b'\x00\x00\x00\x04\x00\x5A\x00\x0A\xCE' # spans LENGTH, CLASS ID, METHOD ID, ....., FRAME_END @@ -3724,7 +3800,6 @@ class TxSelect(AMQPMethodPayload): Create frame tx.select """ - @staticmethod def from_buffer(buf, start_offset): offset = start_offset @@ -3742,12 +3817,12 @@ class TxSelectOk(AMQPMethodPayload): NAME = u'tx.select-ok' - INDEX = (90, 11) # (Class ID, Method ID) - BINARY_HEADER = b'\x00\x5A\x00\x0B' # CLASS ID + METHOD ID + INDEX = (90, 11) # (Class ID, Method ID) + BINARY_HEADER = b'\x00\x5A\x00\x0B' # CLASS ID + METHOD ID SENT_BY_CLIENT, SENT_BY_SERVER = False, True - IS_SIZE_STATIC = True # this means that argument part has always the same length + IS_SIZE_STATIC = True # this means that argument part has always the same length IS_CONTENT_STATIC = True # this means that argument part has always the same content STATIC_CONTENT = b'\x00\x00\x00\x04\x00\x5A\x00\x0B\xCE' # spans LENGTH, CLASS ID, METHOD ID, ....., FRAME_END @@ -3756,7 +3831,6 @@ class TxSelectOk(AMQPMethodPayload): Create frame tx.select-ok """ - @staticmethod def from_buffer(buf, start_offset): offset = start_offset @@ -3795,20 +3869,20 @@ class ConfirmSelect(AMQPMethodPayload): The client can only use this method on a non-transactional channel. """ - __slots__ = (u'nowait', ) + __slots__ = (u'nowait',) NAME = u'confirm.select' - INDEX = (85, 10) # (Class ID, Method ID) - BINARY_HEADER = b'\x00\x55\x00\x0A' # CLASS ID + METHOD ID + INDEX = (85, 10) # (Class ID, Method ID) + BINARY_HEADER = b'\x00\x55\x00\x0A' # CLASS ID + METHOD ID SENT_BY_CLIENT, SENT_BY_SERVER = True, False - IS_SIZE_STATIC = True # this means that argument part has always the same length + IS_SIZE_STATIC = True # this means that argument part has always the same length IS_CONTENT_STATIC = False # this means that argument part has always the same content # See constructor pydoc for details - FIELDS = [ + FIELDS = [ Field(u'nowait', u'bit', u'bit', reserved=False), ] @@ -3825,7 +3899,7 @@ class ConfirmSelect(AMQPMethodPayload): def write_arguments(self, buf): buf.write(struct.pack('!B', (self.nowait << 0))) - + def get_size(self): return 1 @@ -3849,12 +3923,12 @@ class ConfirmSelectOk(AMQPMethodPayload): NAME = u'confirm.select-ok' - INDEX = (85, 11) # (Class ID, Method ID) - BINARY_HEADER = b'\x00\x55\x00\x0B' # CLASS ID + METHOD ID + INDEX = (85, 11) # (Class ID, Method ID) + BINARY_HEADER = b'\x00\x55\x00\x0B' # CLASS ID + METHOD ID SENT_BY_CLIENT, SENT_BY_SERVER = False, True - IS_SIZE_STATIC = True # this means that argument part has always the same length + IS_SIZE_STATIC = True # this means that argument part has always the same length IS_CONTENT_STATIC = True # this means that argument part has always the same content STATIC_CONTENT = b'\x00\x00\x00\x04\x00\x55\x00\x0B\xCE' # spans LENGTH, CLASS ID, METHOD ID, ....., FRAME_END @@ -3863,7 +3937,6 @@ class ConfirmSelectOk(AMQPMethodPayload): Create frame confirm.select-ok """ - @staticmethod def from_buffer(buf, start_offset): offset = start_offset @@ -3935,7 +4008,6 @@ IDENT_TO_METHOD = { (50, 10): QueueDeclare, } - BINARY_HEADER_TO_METHOD = { b'\x00\x5A\x00\x15': TxCommitOk, b'\x00\x3C\x00\x64': BasicRecoverAsync, @@ -4001,7 +4073,6 @@ BINARY_HEADER_TO_METHOD = { b'\x00\x32\x00\x0A': QueueDeclare, } - CLASS_ID_TO_CONTENT_PROPERTY_LIST = { 60: BasicContentPropertyList, } @@ -4041,7 +4112,7 @@ REPLY_REASONS_FOR = { # Methods that are replies for other, ie. ConnectionOpenOk: ConnectionOpen # a method may be a reply for ONE or NONE other methods # if a method has no replies, it will have an empty list as value here -REPLIES_FOR= { +REPLIES_FOR = { BasicGetEmpty: [], BasicRecoverOk: [], BasicReturn: [], diff --git a/coolamqp/framing/field_table.py b/coolamqp/framing/field_table.py index e03d144ea4fc5eb09b46826143726d82d8ac9176..2c31fdec30163b7d7da9aabdd84b546625930ce9 100644 --- a/coolamqp/framing/field_table.py +++ b/coolamqp/framing/field_table.py @@ -12,7 +12,9 @@ A table is of form ( (name1::bytes, fv1), (name2::bytes, fv2), ...) NOTE: it's not buffers, it's memoryview all along """ from __future__ import absolute_import, division, print_function + import struct + import six @@ -67,10 +69,15 @@ FIELD_TYPES = { 'f': (4, '!f'), 'd': (8, '!d'), 'D': (5, None, enframe_decimal, deframe_decimal), # decimal-value - 's': (None, None, enframe_shortstr, deframe_shortstr, lambda val: len(val) + 1), # shortstr - 'S': (None, None, enframe_longstr, deframe_longstr, lambda val: len(val) + 4), # longstr + 's': ( + None, None, enframe_shortstr, deframe_shortstr, lambda val: len(val) + 1), +# shortstr + 'S': ( + None, None, enframe_longstr, deframe_longstr, lambda val: len(val) + 4), +# longstr 'T': (8, '!Q'), - 'V': (0, None, lambda buf, v: None, lambda buf, ofs: None, 0), # rendered as None + 'V': (0, None, lambda buf, v: None, lambda buf, ofs: None, 0), +# rendered as None } @@ -120,8 +127,9 @@ def deframe_array(buf, offset): values.append((v, t)) if offset != start_offset + 4 + ln: - raise ValueError('Array longer than expected, took %s, expected %s bytes', - (offset - (start_offset + ln + 4), ln + 4)) + raise ValueError( + 'Array longer than expected, took %s, expected %s bytes', + (offset - (start_offset + ln + 4), ln + 4)) return values, ln + 4 @@ -164,8 +172,9 @@ def deframe_table(buf, start_offset): # -> (table, bytes_consumed) fields.append((field_name.tobytes(), fv)) if offset > (start_offset + table_length + 4): - raise ValueError('Table turned out longer than expected! Found %s bytes expected %s', - (offset - start_offset, table_length)) + raise ValueError( + 'Table turned out longer than expected! Found %s bytes expected %s', + (offset - start_offset, table_length)) return fields, table_length + 4 diff --git a/coolamqp/framing/frames.py b/coolamqp/framing/frames.py index 3f36f911d37b58b324603159f758083149848bb1..0401e314eb5d51f763dc22980b5d6e01b87bf885 100644 --- a/coolamqp/framing/frames.py +++ b/coolamqp/framing/frames.py @@ -5,12 +5,13 @@ Concrete frame definitions from __future__ import absolute_import, division, print_function import struct + import six from coolamqp.framing.base import AMQPFrame -from coolamqp.framing.definitions import FRAME_METHOD, FRAME_HEARTBEAT, FRAME_BODY, FRAME_HEADER, FRAME_END, \ - IDENT_TO_METHOD, CLASS_ID_TO_CONTENT_PROPERTY_LIST, FRAME_METHOD_BYTE, FRAME_BODY_BYTE, FRAME_HEADER_BYTE, \ - FRAME_END_BYTE +from coolamqp.framing.definitions import FRAME_METHOD, FRAME_HEARTBEAT, \ + FRAME_BODY, FRAME_HEADER, FRAME_END, \ + IDENT_TO_METHOD, CLASS_ID_TO_CONTENT_PROPERTY_LIST, FRAME_END_BYTE class AMQPMethodFrame(AMQPFrame): @@ -71,15 +72,18 @@ class AMQPHeaderFrame(AMQPFrame): def write_to(self, buf): buf.write(struct.pack('!BHLHHQ', FRAME_HEADER, self.channel, - 12 + self.properties.get_size(), self.class_id, 0, self.body_size)) + 12 + self.properties.get_size(), self.class_id, 0, + self.body_size)) self.properties.write_to(buf) buf.write(FRAME_END_BYTE) @staticmethod def unserialize(channel, payload_as_buffer): # payload starts with class ID - class_id, weight, body_size = struct.unpack_from('!HHQ', payload_as_buffer, 0) - properties = CLASS_ID_TO_CONTENT_PROPERTY_LIST[class_id].from_buffer(payload_as_buffer, 12) + class_id, weight, body_size = struct.unpack_from('!HHQ', + payload_as_buffer, 0) + properties = CLASS_ID_TO_CONTENT_PROPERTY_LIST[class_id].from_buffer( + payload_as_buffer, 12) return AMQPHeaderFrame(channel, class_id, weight, body_size, properties) def get_size(self): diff --git a/coolamqp/objects.py b/coolamqp/objects.py index 96f620a121f1f4487e0672ff2f5b8683e84b3332..45e176d037400a6be399f33825b8a642bd2d679b 100644 --- a/coolamqp/objects.py +++ b/coolamqp/objects.py @@ -2,12 +2,13 @@ """ Core objects used in CoolAMQP """ +import logging import uuid + import six -import logging -import warnings -from coolamqp.framing.definitions import BasicContentPropertyList as MessageProperties +from coolamqp.framing.definitions import \ + BasicContentPropertyList as MessageProperties logger = logging.getLogger(__name__) @@ -126,13 +127,16 @@ class Exchange(object): direct = None # the direct exchange - def __init__(self, name=u'', type=b'direct', durable=True, auto_delete=False): + def __init__(self, name=u'', type=b'direct', durable=True, + auto_delete=False): """ :type name: unicode is preferred, binary type will get decoded to unicode with utf8 :param type: exchange type. binary/unicode """ - self.name = name.decode('utf8') if isinstance(name, six.binary_type) else name # must be unicode - self.type = type.encode('utf8') if isinstance(type, six.text_type) else type # must be bytes + self.name = name.decode('utf8') if isinstance(name, + six.binary_type) else name # must be unicode + self.type = type.encode('utf8') if isinstance(type, + six.text_type) else type # must be bytes self.durable = durable self.auto_delete = auto_delete @@ -141,7 +145,8 @@ class Exchange(object): def __repr__(self): return u'Exchange(%s, %s, %s, %s)' % ( - repr(self.name), repr(self.type), repr(self.durable), repr(self.auto_delete)) + repr(self.name), repr(self.type), repr(self.durable), + repr(self.auto_delete)) def __hash__(self): return self.name.__hash__() @@ -158,7 +163,8 @@ class Queue(object): This object represents a Queue that applications consume from or publish to. """ - def __init__(self, name=b'', durable=False, exchange=None, exclusive=False, auto_delete=False): + def __init__(self, name=b'', durable=False, exchange=None, exclusive=False, + auto_delete=False): """ Create a queue definition. @@ -173,14 +179,16 @@ class Queue(object): :param exclusive: Is this queue exclusive? :param auto_delete: Is this queue auto_delete ? """ - self.name = name.encode('utf8') if isinstance(name, six.text_type) else name #: public, must be bytes + self.name = name.encode('utf8') if isinstance(name, + six.text_type) else name #: public, must be bytes # if name is '', this will be filled in with broker-generated name upon declaration self.durable = durable self.exchange = exchange self.auto_delete = auto_delete self.exclusive = exclusive - self.anonymous = len(self.name) == 0 # if this queue is anonymous, it must be regenerated upon reconnect + self.anonymous = len( + self.name) == 0 # if this queue is anonymous, it must be regenerated upon reconnect self.consumer_tag = self.name if not self.anonymous else uuid.uuid4().hex.encode( 'utf8') # bytes, consumer tag to use in AMQP comms @@ -245,8 +253,11 @@ class NodeDefinition(object): self.virtual_host = '/' elif len(args) == 4: self.host, self.user, self.password, self.virtual_host = args - elif len(args) == 1 and isinstance(args[0], (six.text_type, six.binary_type)): - connstr = args[0].decode('utf8') if isinstance(args[0], six.binary_type) else args[0] + elif len(args) == 1 and isinstance(args[0], + (six.text_type, six.binary_type)): + connstr = args[0].decode('utf8') if isinstance(args[0], + six.binary_type) else \ + args[0] # AMQP connstring if not connstr.startswith(u'amqp://'): raise ValueError(u'should begin with amqp://') @@ -269,4 +280,5 @@ class NodeDefinition(object): def __str__(self): return six.text_type( - b'amqp://%s:%s@%s/%s'.encode('utf8') % (self.host, self.port, self.user, self.virtual_host)) + b'amqp://%s:%s@%s/%s'.encode('utf8') % ( + self.host, self.port, self.user, self.virtual_host)) diff --git a/coolamqp/uplink/__init__.py b/coolamqp/uplink/__init__.py index cef792f6a97f9813867f7ca9741d5f1783ee03bd..92f6f33a19cdf9c34c37003dea7b209378c12d0c 100644 --- a/coolamqp/uplink/__init__.py +++ b/coolamqp/uplink/__init__.py @@ -13,6 +13,7 @@ EVERYTHING HERE IS CALLED BY LISTENER THREAD UNLESS STATED OTHERWISE. """ from __future__ import absolute_import, division, print_function -from coolamqp.uplink.connection import Connection, HeaderOrBodyWatch, MethodWatch, AnyWatch, FailWatch -from coolamqp.uplink.listener import ListenerThread +from coolamqp.uplink.connection import Connection, HeaderOrBodyWatch, \ + MethodWatch, AnyWatch, FailWatch from coolamqp.uplink.handshake import PUBLISHER_CONFIRMS, CONSUMER_CANCEL_NOTIFY +from coolamqp.uplink.listener import ListenerThread diff --git a/coolamqp/uplink/connection/__init__.py b/coolamqp/uplink/connection/__init__.py index 7ec0d01c5042462dcf11aa52740f21c667e1f0ae..8a07f01dcff33f918aa859559d29255aec4a4e9f 100644 --- a/coolamqp/uplink/connection/__init__.py +++ b/coolamqp/uplink/connection/__init__.py @@ -12,5 +12,7 @@ Connection is something that can: from __future__ import absolute_import, division, print_function from coolamqp.uplink.connection.connection import Connection -from coolamqp.uplink.connection.watches import FailWatch, Watch, HeaderOrBodyWatch, MethodWatch, AnyWatch -from coolamqp.uplink.connection.states import ST_OFFLINE, ST_CONNECTING, ST_ONLINE +from coolamqp.uplink.connection.states import ST_OFFLINE, ST_CONNECTING, \ + ST_ONLINE +from coolamqp.uplink.connection.watches import FailWatch, Watch, \ + HeaderOrBodyWatch, MethodWatch, AnyWatch diff --git a/coolamqp/uplink/connection/recv_framer.py b/coolamqp/uplink/connection/recv_framer.py index 6c347ffc8ad9ce8f119f62183d57b8d3a0ade638..5d41cf4ae581f079cd2628792d6b6abca396939b 100644 --- a/coolamqp/uplink/connection/recv_framer.py +++ b/coolamqp/uplink/connection/recv_framer.py @@ -3,11 +3,14 @@ from __future__ import absolute_import, division, print_function import collections import io -import six import struct -from coolamqp.framing.frames import AMQPBodyFrame, AMQPHeaderFrame, AMQPHeartbeatFrame, AMQPMethodFrame -from coolamqp.framing.definitions import FRAME_HEADER, FRAME_HEARTBEAT, FRAME_END, FRAME_METHOD, FRAME_BODY +import six + +from coolamqp.framing.definitions import FRAME_HEADER, FRAME_HEARTBEAT, \ + FRAME_END, FRAME_METHOD, FRAME_BODY +from coolamqp.framing.frames import AMQPBodyFrame, AMQPHeaderFrame, \ + AMQPHeartbeatFrame, AMQPMethodFrame FRAME_TYPES = { FRAME_HEADER: AMQPHeaderFrame, @@ -61,8 +64,10 @@ class ReceivingFramer(object): while self._statemachine(): pass - def _extract(self, up_to): # return up to up_to bytes from current chunk, switch if necessary - assert self.total_data_len >= up_to, 'Tried to extract %s but %s remaining' % (up_to, self.total_data_len) + def _extract(self, + up_to): # return up to up_to bytes from current chunk, switch if necessary + assert self.total_data_len >= up_to, 'Tried to extract %s but %s remaining' % ( + up_to, self.total_data_len) if up_to >= len(self.chunks[0]): q = self.chunks.popleft() else: @@ -70,7 +75,8 @@ class ReceivingFramer(object): self.chunks[0] = self.chunks[0][up_to:] self.total_data_len -= len(q) - assert len(q) <= up_to, 'extracted %s but %s was requested' % (len(q), up_to) + assert len(q) <= up_to, 'extracted %s but %s was requested' % ( + len(q), up_to) return q def _statemachine(self): @@ -81,16 +87,19 @@ class ReceivingFramer(object): else: self.frame_type = ord(self._extract(1)[0]) - if self.frame_type not in (FRAME_HEARTBEAT, FRAME_HEADER, FRAME_METHOD, FRAME_BODY): + if self.frame_type not in ( + FRAME_HEARTBEAT, FRAME_HEADER, FRAME_METHOD, FRAME_BODY): raise ValueError('Invalid frame') return True # state rule 2 - elif (self.frame_type == FRAME_HEARTBEAT) and (self.total_data_len >= AMQPHeartbeatFrame.LENGTH - 1): + elif (self.frame_type == FRAME_HEARTBEAT) and ( + self.total_data_len >= AMQPHeartbeatFrame.LENGTH - 1): data = b'' while len(data) < AMQPHeartbeatFrame.LENGTH - 1: - data = data + self._extract(AMQPHeartbeatFrame.LENGTH - 1 - len(data)).tobytes() + data = data + self._extract( + AMQPHeartbeatFrame.LENGTH - 1 - len(data)).tobytes() if data != AMQPHeartbeatFrame.DATA[1:]: # Invalid heartbeat frame! @@ -102,8 +111,9 @@ class ReceivingFramer(object): return True # state rule 3 - elif (self.frame_type != FRAME_HEARTBEAT) and (self.frame_type is not None) and (self.frame_size is None) and ( - self.total_data_len > 6): + elif (self.frame_type != FRAME_HEARTBEAT) and ( + self.frame_type is not None) and (self.frame_size is None) and ( + self.total_data_len > 6): hdr = b'' while len(hdr) < 6: hdr = hdr + self._extract(6 - len(hdr)).tobytes() @@ -113,7 +123,8 @@ class ReceivingFramer(object): return True # state rule 4 - elif (self.frame_size is not None) and (self.total_data_len >= (self.frame_size + 1)): + elif (self.frame_size is not None) and ( + self.total_data_len >= (self.frame_size + 1)): if len(self.chunks[0]) >= self.frame_size: # We can subslice it - it's very fast @@ -122,7 +133,8 @@ class ReceivingFramer(object): # Construct a separate buffer :( payload = io.BytesIO() while payload.tell() < self.frame_size: - payload.write(self._extract(self.frame_size - payload.tell())) + payload.write( + self._extract(self.frame_size - payload.tell())) assert payload.tell() <= self.frame_size @@ -136,7 +148,8 @@ class ReceivingFramer(object): raise ValueError('Invalid frame end') try: - frame = FRAME_TYPES[self.frame_type].unserialize(self.frame_channel, payload) + frame = FRAME_TYPES[self.frame_type].unserialize( + self.frame_channel, payload) except ValueError: raise diff --git a/coolamqp/uplink/connection/send_framer.py b/coolamqp/uplink/connection/send_framer.py index 06e7abc2064e693f02fafdaffdbe5fd002937f45..b8d83e6cb918a864d706aa1951a8350cdf6ea7c0 100644 --- a/coolamqp/uplink/connection/send_framer.py +++ b/coolamqp/uplink/connection/send_framer.py @@ -1,10 +1,7 @@ # coding=UTF-8 from __future__ import absolute_import, division, print_function -import collections -import threading import io -import socket class SendingFramer(object): diff --git a/coolamqp/uplink/connection/watches.py b/coolamqp/uplink/connection/watches.py index 1518a346518deb97295c68a96ed0367a37e5e368..3f7893f344a44c7516f13291b808da93868945ae 100644 --- a/coolamqp/uplink/connection/watches.py +++ b/coolamqp/uplink/connection/watches.py @@ -1,7 +1,8 @@ # coding=UTF-8 from __future__ import absolute_import, division, print_function -from coolamqp.framing.frames import AMQPMethodFrame, AMQPHeartbeatFrame, AMQPHeaderFrame, AMQPBodyFrame +from coolamqp.framing.frames import AMQPMethodFrame, AMQPHeaderFrame, \ + AMQPBodyFrame class Watch(object): diff --git a/coolamqp/uplink/handshake.py b/coolamqp/uplink/handshake.py index aecc0bdeae111ff74b2814e1f992588b72a75db8..f2e0d693cd26220584190d604ad745512a196c59 100644 --- a/coolamqp/uplink/handshake.py +++ b/coolamqp/uplink/handshake.py @@ -6,7 +6,7 @@ Provides reactors that can authenticate an AQMP session """ import six from coolamqp.framing.definitions import ConnectionStart, ConnectionStartOk, \ - ConnectionTune, ConnectionTuneOk, ConnectionOpen, ConnectionOpenOk, ConnectionClose + ConnectionTune, ConnectionTuneOk, ConnectionOpen, ConnectionOpenOk from coolamqp.framing.frames import AMQPMethodFrame from coolamqp.uplink.connection.states import ST_ONLINE @@ -15,7 +15,8 @@ CONSUMER_CANCEL_NOTIFY = b'consumer_cancel_notify' SUPPORTED_EXTENSIONS = [ PUBLISHER_CONFIRMS, - CONSUMER_CANCEL_NOTIFY # half assed support - we just .cancel the consumer, see #12 + CONSUMER_CANCEL_NOTIFY + # half assed support - we just .cancel the consumer, see #12 ] CLIENT_DATA = [ @@ -25,8 +26,11 @@ CLIENT_DATA = [ (b'version', (b'0.91', 'S')), (b'copyright', (b'Copyright (C) 2016-2017 DMS Serwis', 'S')), ( - b'information', (b'Licensed under the MIT License.\nSee https://github.com/smok-serwis/coolamqp for details', 'S')), - (b'capabilities', ([(capa, (True, 't')) for capa in SUPPORTED_EXTENSIONS], 'F')), + b'information', ( + b'Licensed under the MIT License.\nSee https://github.com/smok-serwis/coolamqp for details', + 'S')), + (b'capabilities', + ([(capa, (True, 't')) for capa in SUPPORTED_EXTENSIONS], 'F')), ] WATCHDOG_TIMEOUT = 10 @@ -48,7 +52,8 @@ class Handshaker(object): self.password = node_definition.password.encode('utf8') self.virtual_host = node_definition.virtual_host.encode('utf8') self.heartbeat = node_definition.heartbeat or 0 - self.connection.watch_for_method(0, ConnectionStart, self.on_connection_start) + self.connection.watch_for_method(0, ConnectionStart, + self.on_connection_start) # Callbacks self.on_success = on_success @@ -83,7 +88,8 @@ class Handshaker(object): self.connection.extensions.append(label) self.connection.watchdog(WATCHDOG_TIMEOUT, self.on_watchdog) - self.connection.watch_for_method(0, ConnectionTune, self.on_connection_tune) + self.connection.watch_for_method(0, ConnectionTune, + self.on_connection_tune) self.connection.send([ AMQPMethodFrame(0, ConnectionStartOk(CLIENT_DATA, b'PLAIN', @@ -95,12 +101,16 @@ class Handshaker(object): def on_connection_tune(self, payload): self.connection.frame_max = payload.frame_max self.connection.heartbeat = min(payload.heartbeat, self.heartbeat) - for channel in six.moves.xrange(1, (65535 if payload.channel_max == 0 else payload.channel_max) + 1): + for channel in six.moves.xrange(1, ( + 65535 if payload.channel_max == 0 else payload.channel_max) + 1): self.connection.free_channels.append(channel) - self.connection.watch_for_method(0, ConnectionOpenOk, self.on_connection_open_ok) + self.connection.watch_for_method(0, ConnectionOpenOk, + self.on_connection_open_ok) self.connection.send([ - AMQPMethodFrame(0, ConnectionTuneOk(payload.channel_max, payload.frame_max, self.connection.heartbeat)), + AMQPMethodFrame(0, ConnectionTuneOk(payload.channel_max, + payload.frame_max, + self.connection.heartbeat)), AMQPMethodFrame(0, ConnectionOpen(self.virtual_host)) ]) diff --git a/coolamqp/uplink/heartbeat.py b/coolamqp/uplink/heartbeat.py index 54d4b9225f91d99249ad87d1ae7d50709569b0d0..4cb5a7aa0592104d732579ff9a8f72f6050764e8 100644 --- a/coolamqp/uplink/heartbeat.py +++ b/coolamqp/uplink/heartbeat.py @@ -1,5 +1,6 @@ # coding=UTF-8 from __future__ import absolute_import, division, print_function + import monotonic from coolamqp.framing.frames import AMQPHeartbeatFrame @@ -42,7 +43,8 @@ class Heartbeater(object): """Timer says we should send a heartbeat""" self.connection.send([AMQPHeartbeatFrame()], priority=True) - if (monotonic.monotonic() - self.last_heartbeat_on) > 2 * self.heartbeat_interval: + if ( + monotonic.monotonic() - self.last_heartbeat_on) > 2 * self.heartbeat_interval: # closing because of heartbeat self.connection.send(None) diff --git a/coolamqp/uplink/listener/epoll_listener.py b/coolamqp/uplink/listener/epoll_listener.py index a92033a9780aabbb55c12a701cb74f4cf109fc63..1a42b7129bc47499c9c647a028caeb5859a0a33b 100644 --- a/coolamqp/uplink/listener/epoll_listener.py +++ b/coolamqp/uplink/listener/epoll_listener.py @@ -1,12 +1,14 @@ # coding=UTF-8 from __future__ import absolute_import, division, print_function -import six + +import collections +import heapq import logging import select -import monotonic import socket -import collections -import heapq + +import monotonic +import six from coolamqp.uplink.listener.socket import SocketFailed, BaseSocket @@ -88,7 +90,8 @@ class EpollListener(object): sock.on_write() # I'm done with sending for now - if len(sock.data_to_send) == 0 and len(sock.priority_queue) == 0: + if len(sock.data_to_send) == 0 and len( + sock.priority_queue) == 0: self.epoll.modify(sock.fileno(), RO) except SocketFailed: diff --git a/coolamqp/uplink/listener/socket.py b/coolamqp/uplink/listener/socket.py index 34420c1eae411a4b7234e2b1c8c515cb1daee687..89a52dfc21be5e21e92fb5074bd4d533c4f0ac71 100644 --- a/coolamqp/uplink/listener/socket.py +++ b/coolamqp/uplink/listener/socket.py @@ -1,8 +1,8 @@ # coding=UTF-8 from __future__ import absolute_import, division, print_function + import collections import socket -import six class SocketFailed(IOError): diff --git a/coolamqp/uplink/listener/thread.py b/coolamqp/uplink/listener/thread.py index 4d82d8ce5b291384539569e693775c376b16d2e0..7fda4fe1b8dbecd8952d0b71f037e0970c3ef77a 100644 --- a/coolamqp/uplink/listener/thread.py +++ b/coolamqp/uplink/listener/thread.py @@ -3,8 +3,8 @@ from __future__ import absolute_import, division, print_function import threading -from coolamqp.uplink.listener.epoll_listener import EpollListener from coolamqp.objects import Callable +from coolamqp.uplink.listener.epoll_listener import EpollListener class ListenerThread(threading.Thread): diff --git a/setup.cfg b/setup.cfg index 81e69759c0dae482e4b49885492fbda43dd5f0e9..3ab7d192521aa0088dcc024eed2d945cc4af88bc 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,7 +1,7 @@ [metadata] description-file = README.md name = CoolAMQP -version = 0.92rc1 +version = 0.92rc2 license = MIT License classifiers = Programming Language :: Python