
    )Jf&                         d Z ddlZddlZddlZddlmZ ddlmZ ddlmZm	Z	 ddlm
Z
mZmZ d Zerd Znd	 Zd
 Zd Zd Zd ZdS )zAMQP Table Encoding/Decoding    N)datetime)
exceptions)PY2
basestring)unicode_typelongas_bytesc                     t          |          }t          |          }|dk    rt          j        |          |                     t          j        d|                     |                     |           d|z   S )zEncode a string value as short string and append it to pieces list
    returning the size of the encoded value.

    :param list pieces: Already encoded values
    :param str value: String value to encode
    :rtype: int

       B   )r	   lenr   ShortStringTooLongappendstructpack)piecesvalueencoded_valuelengths       L/home/alex/cs2snipeproduction/venv/lib/python3.11/site-packages/pika/data.pyencode_short_stringr      sr     UOOMF ||+M:::
MM&+c6**+++
MM-   v:    c                     t          j        d| |          d         }|dz  }| |||z            }	 t          |          }n# t          $ r Y nw xY w||z  }||fS )IDecode a short string value from ``encoded`` data at ``offset``.
        r   r   r   )r   unpack_frombytesUnicodeEncodeErrorencodedoffsetr   r   s       r   decode_short_stringr"   .   s     #C&99!<! v./	%LLEE! 	 	 	D	&f}s   A   
AAc                     t          j        d| |          d         }|dz  }| |||z            }	 |                    d          }n# t          $ r Y nw xY w||z  }||fS )r   r   r   r   utf8)r   r   decodeUnicodeDecodeErrorr   s       r   r"   r"   ?   s     #C&99!<!v./	LL((EE! 	 	 	D	&f}s   A 
AAc                    |pi }t          |           }|                     d           d}|                                D ]+\  }}|t          | |          z  }|t	          | |          z  },t          j        d|          | |<   |dz   S )zEncode a dict as an AMQP table appending the encded table to the
    pieces list passed in.

    :param list pieces: Already encoded frame pieces
    :param dict table: The dict to encode
    :rtype: int

    Nr   >I   )r   r   itemsr   encode_valuer   r   )r   tablelength_index	tablesizekeyr   s         r   encode_tabler0   M   s     KREv;;L
MM$I 1 1e(555	\&%000		!;tY77F<q=r   c           
      2   t           rt          |t                    rt          |t                    r|                    d          }|                     t          j        ddt          |                               |                     |           dt          |          z   S nt          |t                    rr|                    d          }|                     t          j        ddt          |                               |                     |           dt          |          z   S t          |t                    r]|                     t          j        ddt          |                               |                     |           dt          |          z   S t          |t                    r8|                     t          j        ddt          |                               dS t          |t                    r[|d	k     r*|                     t          j        d
d|                     n)|                     t          j        dd|                     dS t          |t                    r	 t          j        dd|          }|                     |           dS # t          j        $ rf |d	k     r$t          j        d
dt          |                    }n#t          j        ddt          |                    }|                     |           Y dS w xY wt          |t          j                  r|                                }|                                j        d	k     rl|                                j         }t          |t          j        d          |z  z            }|                     t          j        dd||                     n7|                     t          j        ddd	t          |                               dS t          |t&                    rO|                     t          j        ddt)          j        |                                                               dS t          |t.                    r;|                     t          j        dd                     dt1          | |          z   S t          |t2                    rg }|D ]}t5          ||           d                    |          }|                     t          j        ddt          |                               |                     |           dt          |          z   S |*|                     t          j        dd                     dS t9          j        | |          )zEncode the value passed in and append it to the pieces list returning
    the the size of the encoded value.

    :param list pieces: Already encoded values
    :param any value: The value to encode
    :rtype: int

    zutf-8z>cI   S      xz>cB   t   r   z>cq   Lz>cQ   l	   z>ci   I
   z>cBi   D      Tz>c   Fr   r      AN   V)r   
isinstancer   r   encoder   r   r   r   r   boolintr   errordecimalDecimal	normalizeas_tupleexponentr   calendartimegmutctimetupledictr0   listr+   joinr   UnsupportedAMQPFieldException)r   r   packeddecimalsrawlist_piecesvalpieces           r   r+   r+   b   s     "eZ(( 	"%.. .W--MM&+eT3u::>>???MM%   s5zz>!	" eZ(( 	"LL))EMM&+eT3u::>>???MM%   s5zz>!eU## 	"MM&+eT3u::>>???MM%   s5zz>!% fk%s5zz::;;;q% /F199MM&+eT599::::MM&+eT599:::q	E3		 )F
	[e44FMM&!!!1| 	 	 	qyyUD$u++>>UD$u++>>MM&!!!11	 
E7?	+	+ F!!>>$q((((11Hewr22H<=>>CMM&+fdHcBBCCCC MM&+fdAs5zzBBCCCq	E8	$	$ FKtX_U5G5G5I5I%J%JKK	M 	M 	Mq	E4	 	  Ffk$--...<....	E4	 	  F 	+ 	+Cc****%%fk%s5zz::;;;e3u::~	fk$--...q6vuEEEs   1+J A1LLc                     i }t          j        d| |          d         }|dz  }||z   }||k     r1t          | |          \  }}t          | |          \  }}|||<   ||k     1||fS )a  Decode the AMQP table passed in from the encoded value returning the
    decoded result and the number of bytes read plus the offset.

    :param str encoded: The binary encoded data to decode
    :param int offset: The starting byte offset
    :rtype: tuple

    r(   r   r)   )r   r   r"   decode_value)r    r!   resultr.   limitr/   r   s          r   decode_tabler]      s     F"4&99!<I
aKFYE
5..)'6::V$Wf55vs 5.. 6>r   c                 Z   | ||dz            }|dz  }|dk    r2t          j        d| |          d         }t          |          }|dz  }n|dk    r#t          j        d| |          d         }|dz  }n|dk    r#t          j        d| |          d         }|dz  }n|dk    r#t          j        d	| |          d         }|d
z  }nc|dk    r#t          j        d| |          d         }|d
z  }n:|dk    r#t          j        d| |          d         }|dz  }n|dk    r#t          j        d| |          d         }|dz  }n|dk    r0t          t          j        d| |          d                   }|dz  }n|dk    r0t          t          j        d| |          d                   }|dz  }n||dk    r0t          t          j        d| |          d                   }|dz  }nF|dk    r0t          t          j        d| |          d                   }|dz  }n|dk    rqt          j        d| |          d         }|dz  }t          j        d| |          d         }|dz  }t	          j        |          t	          j        d          | z  z  }n|dk    r#t          j        d	| |          d         }|d
z  }np|dk    r\t          j        d| |          d         }|dz  }| |||z            }	 |                    d           }n# t          $ r Y nw xY w||z  }n|d!k    r4t          j        d| |          d         }|dz  }| |||z            }||z  }n|d"k    r]t          j        d| |          d         }|dz  }||z   }g }||k     r.t          | |          \  }}|	                    |           ||k     .nq|d#k    r4t          j        t          j        d| |          d                   }|dz  }n7|d$k    rt          | |          \  }}n|d%k    rd&}nt          j        |          ||fS )'a5  Decode the value passed in returning the decoded value and the number
    of bytes read in addition to the starting offset.

    :param str encoded: The binary encoded data to decode
    :param int offset: The starting byte offset
    :rtype: tuple
    :raises: pika.exceptions.InvalidFieldTypeException

    r   r5   z>Br      b   Bz>b   Uz>hr6      uz>Hr:   z>ir)      ir(   r7   z>q   r8   z>Q   fz>f   dz>dr<   r   r;      sr2   r$   r4   r@   r>   r?   rA   N)r   r   rD   r   rG   rH   r%   r&   rZ   r   r   utcfromtimestampr]   r   InvalidFieldTypeException)	r    r!   kindr   rT   rU   r   
offset_endrW   s	            r   rZ   rZ      s    6&1*$%D
aKF t||"4&99!<U! 
"4&99!<! 
"4&99!<! 
"4&99!<! 
"4&99!<! 
"4&99!<! 
"4&99!<! 
V'gv>>qABB! 
V'gv>>qABB! 
V'gv>>qABB! 
V'gv>>qABB! 
%c7F;;A>! w77:!$$(;(;hY(FG 
"4&99!<! 
#D'6::1=!v./	LL((EE! 	 	 	D	&	#D'6::1=!v./& 
#D'6::1=!f_
z!!&w77KCLL z!!
 
)tWf55a8: :! 
&w77 
24888&=s   .L 
LL)__doc__r   rG   rL   r   pikar   pika.compatr   r   r   r   r	   r   r"   r0   r+   r]   rZ    r   r   <module>rp      s   " "                ' ' ' ' ' ' ' ' 4 4 4 4 4 4 4 4 4 4  >     "    *PF PF PFf  (    r   