U
    dW                     @   sj   d Z ddlZddlmZ ddlmZmZ ddlmZ ddl	m
Z
 dZd	d
dhZeedZG dd dZdS )zMessage class.    N   )
decompress)MessageStateErrorreraise)loads)
dictfilter)MessageACKREJECTEDREQUEUEDpypy_version_infoc                
   @   s   e Zd ZdZeZdZesdZdddZdddZ	dd	d
Z
d ddZd!ddZd"ddZdd Zdd Zdd Zedd Zedd Zdd ZdS )#r   a`  Base class for received messages.

    Keyword Arguments:

        channel (ChannelT): If message was received, this should be the
            channel that the message was received on.

        body (str): Message body.

        delivery_mode (bool): Set custom delivery mode.
            Defaults to :attr:`delivery_mode`.

        priority (int): Message priority, 0 to broker configured
            max priority, where higher is better.

        content_type (str): The messages content_type.  If content_type
            is set, no serialization occurs as it is assumed this is either
            a binary object, or you've done your own serialization.
            Leave blank if using built-in serialization as our library
            properly sets content_type.

        content_encoding (str): The character set in which this object
            is encoded. Use "binary" if sending in raw binary objects.
            Leave blank if using built-in serialization as our library
            properly sets content_encoding.

        properties (Dict): Message properties.

        headers (Dict): Message headers.
    N)_statechanneldelivery_tagcontent_typecontent_encodingdelivery_infoheaders
propertiesbody_decoded_cacheaccept__dict__c                 K   s  |si n|}| j d krg n| j | _ |
| _|| _|| _|| _|| _|pFi | _|pPi | _d | _d| _	|	| _
| jd}| j s|rzt||}W n$ tk
r   | j t  Y nX | j s|rt|trz||}W n$ tk
r   | j t  Y nX || _d S )NZRECEIVEDcompression)errorsr   r   r   r   r   r   r   r   r   r   getr   	Exceptionappendsysexc_info
isinstancestrencoder   )selfr   r   r   r   r   r   r   Z
postencoder   r   kwargsr    r%   1/tmp/pip-unpacked-wheel-hqfrjlvz/kombu/message.py__init__<   s0    


zMessage.__init__c              
   C   sJ   zt | jd   W n2 tk
rD } z|s* || | W 5 d }~X Y nX d S )Nr   )r   r   r   )r#   callbackexcr%   r%   r&   _reraise_error[   s    zMessage._reraise_errorFc                 C   s   | j dkr| d| j jdk	rTz| jd }W n tk
rB   Y nX || j jkrTdS | jrj| d| | j j| j|d d| _	dS )zAcknowledge this message as being processed.

        This will remove the message from the queue.

        Raises:
            MessageStateError: If the message has already been
                acknowledged/requeued/rejected.
        N.This message does not have a receiving channelconsumer_tag3Message already acknowledged with state: {0._state}multipler	   )
r   r   Zno_ack_consumersr   KeyErroracknowledgedformatZ	basic_ackr   r   )r#   r/   r,   r%   r%   r&   ackc   s&    	
zMessage.ackc              
   C   s   z| j |d W nl tk
rH } z|jd| j|dd  W 5 d }~X Y n6 |k
r| } z|jd| j|dd W 5 d }~X Y nX d S )Nr.   zCouldn't ack %r, reason:%rTr   )r3   BrokenPipeErrorcriticalr   )r#   loggerr   r/   r)   r%   r%   r&   ack_log_error~   s        zMessage.ack_log_errorc              
   C   sL   z| j |d W n6 |k
rF } z|jd| j|dd W 5 d }~X Y nX d S )NrequeuezCouldn't reject %r, reason: %rTr4   )rejectr6   r   )r#   r7   r   r:   r)   r%   r%   r&   reject_log_error   s      zMessage.reject_log_errorc                 C   sF   | j dkr| d| jr*| d| | j j| j|d d| _dS )zReject this message.

        The message will be discarded by the server.

        Raises:
            MessageStateError: If the message has already been
                acknowledged/requeued/rejected.
        Nr+   r-   r9   r
   r   r   r1   r2   Zbasic_rejectr   r   )r#   r:   r%   r%   r&   r;      s    	
zMessage.rejectc                 C   sF   | j dkr| d| jr*| d| | j j| jdd d| _dS )a.  Reject this message and put it back on the queue.

        Warning:
            You must not use this method as a means of selecting messages
            to process.

        Raises:
            MessageStateError: If the message has already been
                acknowledged/requeued/rejected.
        Nr+   r-   Tr9   r   r=   r#   r%   r%   r&   r:      s    
zMessage.requeuec                 C   s   | j s|  | _ | j S )zDeserialize the message body.

        Returning the original python structure sent by the publisher.

        Note:
            The return value is memoized, use `_decode` to force
            re-evaluation.
        )r   _decoder>   r%   r%   r&   decode   s    	
zMessage.decodec                 C   s   t | j| j| j| jdS )N)r   )r   r   r   r   r   r>   r%   r%   r&   r?      s    
 zMessage._decodec                 C   s
   | j tkS )z1Set to true if the message has been acknowledged.)r   
ACK_STATESr>   r%   r%   r&   r1      s    zMessage.acknowledgedc                 C   s   | j r| j S |  S )zThe decoded message body.)r   r@   r>   r%   r%   r&   payload   s    zMessage.payloadc                 C   sr   d t| jt| t| j| j| j| jd k	r4t	| jnd t| j
d| j
ddt| jd| jdddS )	Nz&<{} object at {:#x} with details {!r}>correlation_idtype)rC   rD   exchangerouting_key)rE   rF   )stater   r   body_lengthr   r   )r2   rD   __name__idr   r   r   r   r   lenr   r   r   r>   r%   r%   r&   __repr__   s$      



zMessage.__repr__)
NNNNNNNNNN)N)F)F)F)F)rI   
__module____qualname____doc__r   r   IS_PYPY	__slots__r'   r*   r3   r8   r<   r;   r:   r@   r?   propertyr1   rB   rL   r%   r%   r%   r&   r      s8                 







r   )rO   r   r   r   
exceptionsr   r   Zserializationr   Zutils.functionalr   __all__rA   hasattrrP   r   r%   r%   r%   r&   <module>   s   

