ó
Ì	g]c           @   sÔ   d  Z  d d l 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(   s   AMQP Table Encoding/DecodingiÿÿÿÿN(   t   datetime(   t
   exceptions(   t   PY2t
   basestring(   t   unicode_typet   longt   as_bytesc         C   sd   t  | ƒ } t | ƒ } | d k r6 t j | ƒ ‚ n  |  j t j d | ƒ ƒ |  j | ƒ d | S(   sâ   Encode 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

    iÿ   t   Bi   (   R   t   lenR   t   ShortStringTooLongt   appendt   structt   pack(   t   piecest   valuet   encoded_valuet   length(    (    s.   /srv/kernel/kteam-tools/dashboard/pika/data.pyt   encode_short_string   s    	c         C   sl   t  j d |  | ƒ d } | d 7} |  | | | !} y t | ƒ } Wn t k
 rW n X| | 7} | | f S(   sI   Decode a short string value from ``encoded`` data at ``offset``.
        R   i    i   (   R   t   unpack_fromt   bytest   UnicodeEncodeError(   t   encodedt   offsetR   R   (    (    s.   /srv/kernel/kteam-tools/dashboard/pika/data.pyt   decode_short_string/   s    

c         C   so   t  j d |  | ƒ d } | d 7} |  | | | !} y | j d ƒ } Wn t k
 rZ n X| | 7} | | f S(   sI   Decode a short string value from ``encoded`` data at ``offset``.
        R   i    i   t   utf8(   R   R   t   decodet   UnicodeDecodeError(   R   R   R   R   (    (    s.   /srv/kernel/kteam-tools/dashboard/pika/data.pyR   @   s    

c         C   sŒ   | p	 i  } t  |  ƒ } |  j d ƒ d } x@ | j ƒ  D]2 \ } } | t |  | ƒ 7} | t |  | ƒ 7} q8 Wt j d | ƒ |  | <| d S(   sÑ   Encode 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

    i    s   >Ii   N(   R   R
   t   Nonet   itemsR   t   encode_valueR   R   (   R   t   tablet   length_indext	   tablesizet   keyR   (    (    s.   /srv/kernel/kteam-tools/dashboard/pika/data.pyt   encode_tableN   s    	c      
   C   s  t  rv t | t ƒ rt | t ƒ r6 | j d ƒ } n  |  j t j d d t | ƒ ƒ ƒ |  j | ƒ d t | ƒ Sn§ t | t ƒ rÑ | j d ƒ } |  j t j d d t | ƒ ƒ ƒ |  j | ƒ d t | ƒ St | t	 ƒ r|  j t j d d t | ƒ ƒ ƒ |  j | ƒ d t | ƒ St | t
 ƒ rR|  j t j d d t | ƒ ƒ ƒ d St | t ƒ r|  j t j d	 d
 | ƒ ƒ d St | t ƒ r#t j ƒ  ‚ t j d ƒ y* t j d d | ƒ } |  j | ƒ d SWnC t j t f k
 rt j d	 d
 t | ƒ ƒ } |  j | ƒ d SXWd QXnÜt | t j ƒ rÎ| j ƒ  } | j ƒ  j d k  r¥| j ƒ  j } t | t j d ƒ | ƒ } |  j t j d d | | ƒ ƒ n% |  j t j d d d t | ƒ ƒ ƒ d St | t ƒ r|  j t j d d t j | j ƒ  ƒ ƒ ƒ d St | t ƒ rE|  j t j d d ƒ ƒ d t |  | ƒ St | t ƒ rÄg  } x | D] } t | | ƒ qaWd j | ƒ } |  j t j d d t | ƒ ƒ ƒ |  j | ƒ d t | ƒ S| d k rí|  j t j d d ƒ ƒ d St! j" |  | ƒ ‚ d S(   sÜ   Encode 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

    s   utf-8s   >cIt   Si   t   xs   >cBt   ti   s   >cqt   li	   t   errors   >cit   INi    i
   s   >cBit   Di   s   >cQt   Ts   >ct   Fi   t    t   At   V(#   R   t
   isinstanceR   R   t   encodeR
   R   R   R   R   t   boolt   intR   t   warningst   catch_warningst   filterwarningsR'   t   DeprecationWarningt   decimalt   Decimalt	   normalizet   as_tuplet   exponentR    t   calendart   timegmt   utctimetuplet   dictR"   t   listR   t   joinR   R   t   UnsupportedAMQPFieldException(   R   R   t   packedt   decimalst   rawt   list_piecest   valt   piece(    (    s.   /srv/kernel/kteam-tools/dashboard/pika/data.pyR   c   sx    
"""""%%"c         C   s„   i  } t  j d |  | ƒ d } | d 7} | | } xD | | k  ry t |  | ƒ \ } } t |  | ƒ \ } } | | | <q6 W| | f S(   s  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

    s   >Ii    i   (   R   R   R   t   decode_value(   R   R   t   resultR    t   limitR!   R   (    (    s.   /srv/kernel/kteam-tools/dashboard/pika/data.pyt   decode_table²   s    	

c   	      C   sÒ  |  | | d !} | d 7} | d k rY t  j d |  | ƒ d } t | ƒ } | d 7} no| d k r‹ t  j d |  | ƒ d } | d 7} n=| d k r½ t  j d |  | ƒ d } | d 7} n| d k rï t  j d	 |  | ƒ d } | d
 7} nÙ| d k r!t  j d |  | ƒ d } | d
 7} n§| d k rSt  j d |  | ƒ d } | d 7} nu| d k r…t  j d |  | ƒ d } | d 7} nC| d k r½t t  j d |  | ƒ d ƒ } | d 7} n| d k rõt t  j d |  | ƒ d ƒ } | d 7} nÓ| d k r-t t  j d |  | ƒ d ƒ } | d 7} n›| d k ret t  j d |  | ƒ d ƒ } | d 7} nc| d k rÛt  j d |  | ƒ d } | d 7} t  j d |  | ƒ d } | d 7} t j | ƒ t j d ƒ | } ní| d k rt  j d	 |  | ƒ d } | d
 7} n»| d k rt  j d |  | ƒ d } | d 7} |  | | | !} y | j d ƒ } Wn t k
 rsn X| | 7} nG| d  k rÎt  j d |  | ƒ d } | d 7} |  | | | !} | | 7} nú | d! k rEt  j d |  | ƒ d } | d 7} | | } g  } x¸ | | k  rAt |  | ƒ \ } } | j	 | ƒ qWnƒ | d" k r€t
 j t  j d |  | ƒ d ƒ } | d 7} nH | d# k r¤t |  | ƒ \ } } n$ | d$ k r¹d% } n t j | ƒ ‚ | | f S(&   s5  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

    i   R%   s   >Bi    t   bR   s   >bt   Us   >hi   t   us   >HR(   s   >ii   t   is   >It   Ls   >qi   R&   s   >Qt   fs   >ft   ds   >dR)   i
   t   sR#   R   R$   R-   R*   R+   R.   N(   R   R   R1   R   R7   R8   R   R   RI   R
   R    t   utcfromtimestampRL   R   R   t   InvalidFieldTypeException(	   R   R   t   kindR   RD   RE   R   t
   offset_endRG   (    (    s.   /srv/kernel/kteam-tools/dashboard/pika/data.pyRI   Æ   sš    


$



	(   t   __doc__R   R7   R<   R3   R    t   pikaR   t   pika.compatR   R   R   R   R   R   R   R"   R   RL   RI   (    (    (    s.   /srv/kernel/kteam-tools/dashboard/pika/data.pyt   <module>   s    				O	