l
Ì	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(   u   AMQP Table Encoding/Decodingi    N(   u   datetime(   u
   exceptions(   u   PY2u
   basestring(   u   unicode_typeu   longu   as_bytesc             C   sd   t  | ƒ } t | ƒ } | d k r6 t j | ƒ ‚ n  |  j t j d | ƒ ƒ |  j | ƒ d | S(   uâ   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ÿ   u   Bi   (   u   as_bytesu   lenu
   exceptionsu   ShortStringTooLongu   appendu   structu   pack(   u   piecesu   valueu   encoded_valueu   length(    (    u.   /srv/kernel/kteam-tools/dashboard/pika/data.pyu   encode_short_string   s    	c             C   sp   t  j d |  | ƒ d } | d 7} |  | | | … } y t | ƒ } Wn t k
 r[ Yn X| | 7} | | f S(   uI   Decode a short string value from ``encoded`` data at ``offset``.
        u   Bi    i   (   u   structu   unpack_fromu   bytesu   UnicodeEncodeError(   u   encodedu   offsetu   lengthu   value(    (    u.   /srv/kernel/kteam-tools/dashboard/pika/data.pyu   decode_short_string/   s    

c             C   ss   t  j d |  | ƒ d } | d 7} |  | | | … } y | j d ƒ } Wn t k
 r^ Yn X| | 7} | | f S(   uI   Decode a short string value from ``encoded`` data at ``offset``.
        u   Bi    i   u   utf8(   u   structu   unpack_fromu   decodeu   UnicodeDecodeError(   u   encodedu   offsetu   lengthu   value(    (    u.   /srv/kernel/kteam-tools/dashboard/pika/data.pyu   decode_short_string@   s    

c             C   sŒ   | p	 i  } t  |  ƒ } |  j d ƒ d } x@ | j ƒ  D]2 \ } } | t |  | ƒ 7} | t |  | ƒ 7} q8 Wt j d | ƒ |  | <| d S(   uÑ   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    u   >Ii   N(   u   lenu   appendu   Noneu   itemsu   encode_short_stringu   encode_valueu   structu   pack(   u   piecesu   tableu   length_indexu	   tablesizeu   keyu   value(    (    u.   /srv/kernel/kteam-tools/dashboard/pika/data.pyu   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 SWnG t j t f k
 rt j d	 d
 t | ƒ ƒ } |  j | ƒ d SYn XWd 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 ƒ rI|  j t j d d ƒ ƒ d t |  | ƒ St | t ƒ rÈg  } x | D] } t | | ƒ qe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(   uÜ   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

    u   utf-8u   >cIs   Si   s   xu   >cBs   ti   u   >cqs   li	   u   erroru   >cis   INi    i
   u   >cBis   Di   u   >cQs   Tu   >cs   Fi   s    s   As   V(#   u   PY2u
   isinstanceu
   basestringu   unicode_typeu   encodeu   appendu   structu   packu   lenu   bytesu   boolu   intu   longu   warningsu   catch_warningsu   filterwarningsu   erroru   DeprecationWarningu   decimalu   Decimalu	   normalizeu   as_tupleu   exponentu   datetimeu   calendaru   timegmu   utctimetupleu   dictu   encode_tableu   listu   encode_valueu   joinu   Noneu
   exceptionsu   UnsupportedAMQPFieldException(   u   piecesu   valueu   packedu   decimalsu   rawu   list_piecesu   valu   piece(    (    u.   /srv/kernel/kteam-tools/dashboard/pika/data.pyu   encode_valuec   sx    
"""""%%"c             C   s„   i  } t  j d |  | ƒ d } | d 7} | | } xD | | k  ry t |  | ƒ \ } } t |  | ƒ \ } } | | | <q6 W| | f S(   u  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

    u   >Ii    i   (   u   structu   unpack_fromu   decode_short_stringu   decode_value(   u   encodedu   offsetu   resultu	   tablesizeu   limitu   keyu   value(    (    u.   /srv/kernel/kteam-tools/dashboard/pika/data.pyu   decode_table²   s    	

c       	      C   sÜ  |  | | d … } | d 7} | d k r\ t  j d |  | ƒ d } t | ƒ } | d 7} nv| d k rŽ t  j d |  | ƒ d } | d 7} nD| 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 rVt  j d |  | ƒ d } | d 7} n|| d k rˆt  j d |  | ƒ d } | d 7} nJ| d k rÀt t  j d |  | ƒ d ƒ } | d 7} n| d k røt t  j d |  | ƒ d ƒ } | d 7} nÚ| d k r0t t  j d |  | ƒ d ƒ } | d 7} n¢| d k rht t  j d |  | ƒ d ƒ } | d 7} nj| 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
 rzYn X| | 7} nJ| d! k rØt  j d |  | ƒ d } | d 7} |  | | | … } | | 7} nú | d" k rOt  j d |  | ƒ d } | d 7} | | } g  } x¸ | | k  rK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('   u5  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   s   tu   >Bi    s   bs   Bu   >bs   Uu   >hi   s   uu   >Hs   Iu   >ii   s   iu   >Is   Lu   >qi   s   lu   >Qs   fu   >fs   du   >ds   Du   Bi
   s   ss   Su   utf8s   xs   As   Ts   Fs   VN(   u   structu   unpack_fromu   boolu   longu   decimalu   Decimalu   decodeu   UnicodeDecodeErroru   decode_valueu   appendu   datetimeu   utcfromtimestampu   decode_tableu   Noneu
   exceptionsu   InvalidFieldTypeException(	   u   encodedu   offsetu   kindu   valueu   decimalsu   rawu   lengthu
   offset_endu   val(    (    u.   /srv/kernel/kteam-tools/dashboard/pika/data.pyu   decode_valueÆ   sš    


$



	(   u   __doc__u   structu   decimalu   calendaru   warningsu   datetimeu   pikau
   exceptionsu   pika.compatu   PY2u
   basestringu   unicode_typeu   longu   as_bytesu   encode_short_stringu   decode_short_stringu   encode_tableu   encode_valueu   decode_tableu   decode_value(    (    (    u.   /srv/kernel/kteam-tools/dashboard/pika/data.pyu   <module>   s    				O	