class MethodReader(object):
    """
    Helper class to receive frames from the broker, combine them if
    necessary with content-headers and content-bodies into complete methods.

    Normally a method is represented as a tuple containing
    (channel, method_sig, args, content).

    In the case of a framing error, an AMQPConnectionException is placed
    in the queue.

    In the case of unexpected frames, a tuple made up of
    (channel, AMQPChannelException) is placed in the queue.

    """
    def __init__(self, source):
        self.source = source
        self.queue = Queue()
        self.running = False
        self.partial_messages = {}
        # For each channel, which type is expected next
        self.expected_types = defaultdict(lambda:1)


    def _next_method(self, timeout=None):
        """
        Read the next method from the source, once one complete method has
        been assembled it is placed in the internal queue.

        """
        while self.queue.empty():
            try:
                frame_type, channel, payload = self.source.read_frame(timeout)
            except Timeout:
                # Ok, let pass it through
                raise
            except Exception, e:
                #
                # Connection was closed?  Framing Error?
                #
                self.queue.put(e)
                break

            if self.expected_types[channel] != frame_type:
                self.queue.put((
                    channel,
                    Exception('Received frame type %s while expecting type: %s' %
                        (frame_type, self.expected_types[channel])
                        )
                    ))
            elif frame_type == 1:
                self._process_method_frame(channel, payload)
            elif frame_type == 2:
                self._process_content_header(channel, payload)
            elif frame_type == 3:
                self._process_content_body(channel, payload)
 def __init__(self, source):
     self.source = source
     self.queue = Queue()
     self.running = False
     self.partial_messages = {}
     # For each channel, which type is expected next
     self.expected_types = defaultdict(lambda:1)