ó
ßüÚ\c           @` sî   d  d l  m Z m Z m Z d  d l Z d  d l Z d d l m Z m Z d Z	 d „  d d d	 d
 d g Dƒ Z
 y- e j e j d ƒ d e e
 e e
 d <Wn e k
 r¹ d e
 d <n Xe	 d „ Z d „  Z d „  Z d „  Z d „  Z d S(   i    (   t   print_functiont   divisiont   absolute_importNi   (   t   ensure_bytest   nbytesi   c         C` s   i  |  ] } d d | “ q S(   i   i   i   s
   max_%s_lenI   €    iÿÿÿ(    (   t   .0t   x(    (    s9   lib/python2.7/site-packages/distributed/protocol/utils.pys
   <dictcomp>   s   	 t   strt   bint   arrayt   mapt   extt    t   raws   utf-8t   encodingc         C` sø   |  s
 |  St  t t |  ƒ ƒ | k r) |  Sg  } xÂ |  D]º } t | ƒ | k rã t | t t f ƒ rr t | ƒ } n  y | j } Wn t k
 r˜ d } n XxT t	 d t | ƒ | | | ƒ D]" } | j
 | | | | | !ƒ qº Wq6 | j
 | ƒ q6 W| S(   s  
    Split a list of frames into a list of frames of maximum size

    This helps us to avoid passing around very large bytestrings.

    Examples
    --------
    >>> frame_split_size([b'12345', b'678'], n=3)  # doctest: +SKIP
    [b'123', b'45', b'678']
    i   i    (   t   maxR
   R   t
   isinstancet   bytest	   bytearrayt
   memoryviewt   itemsizet   AttributeErrort   ranget   append(   t   framest   nt   outt   frameR   t   i(    (    s9   lib/python2.7/site-packages/distributed/protocol/utils.pyt   frame_split_size   s"    
'#c         C` sQ  t  |  d ƒ } | s | St | ƒ t t t | ƒ ƒ k sA t ‚ t d „  t | | ƒ Dƒ ƒ rd | S| d d d … } | d d d … } g  } xº | rL| j ƒ  } g  } x| | r)| j ƒ  } t | ƒ | k rò | j | ƒ | t | ƒ 8} q® t	 | ƒ } | j | |  ƒ | j | | ƒ d } q® W| j d j
 t t | ƒ ƒ ƒ q“ W| S(   sØ    Merge frames into original lengths

    Examples
    --------
    >>> merge_frames({'lengths': [3, 3]}, [b'123456'])
    [b'123', b'456']
    >>> merge_frames({'lengths': [6]}, [b'123', b'456'])
    [b'123456']
    t   lengthsc         s` s'   |  ] \ } } t  | ƒ | k Vq d  S(   N(   t   len(   R   t   ft   l(    (    s9   lib/python2.7/site-packages/distributed/protocol/utils.pys	   <genexpr>I   s    Niÿÿÿÿi    R   (   t   listt   sumR
   R   t   AssertionErrort   allt   zipt   popR   R   t   joinR   (   t   headerR   R   R   R!   t   LR   t   mv(    (    s9   lib/python2.7/site-packages/distributed/protocol/utils.pyt   merge_frames8   s.    
'		
#c         C` sp   g  |  D] } t  | ƒ ^ q } t j d t  |  ƒ ƒ g g  |  D] } t j d t | ƒ ƒ ^ q> } d j | ƒ S(   Nt   QR   (   R   t   structt   packR   R(   (   R   R    R   R   (    (    s9   lib/python2.7/site-packages/distributed/protocol/utils.pyt   pack_frames_preludea   s    ,c         C` s>   t  |  ƒ g } t |  t ƒ s- t |  ƒ }  n  d j | |  ƒ S(   s¢    Pack frames into a byte-like object

    This prepends length information to the front of the bytes-like object

    See Also
    --------
    unpack_frames
    R   (   R0   R   R"   R(   (   R   t   prelude(    (    s9   lib/python2.7/site-packages/distributed/protocol/utils.pyt   pack_framesi   s    	c         C` sœ   t  j d |  d  ƒ \ } g  } d | d } xh t | ƒ D]Z } t  j d |  | d d | d d !ƒ \ } |  | | | !} | j | ƒ | | 7} q: W| S(   sÄ    Unpack bytes into a sequence of frames

    This assumes that length information is at the front of the bytestring,
    as performed by pack_frames

    See Also
    --------
    pack_frames
    R-   i   i   i   (   R.   t   unpackR   R   (   t   bt   n_framesR   t   startR   t   lengthR   (    (    s9   lib/python2.7/site-packages/distributed/protocol/utils.pyt   unpack_framesz   s    
,i   (   t
   __future__R    R   R   R.   t   msgpackt   utilsR   R   t   BIG_BYTES_SHARD_SIZEt   msgpack_optst   loadst   dumpst   Falset	   TypeErrorR   R,   R0   R2   R8   (    (    (    s9   lib/python2.7/site-packages/distributed/protocol/utils.pyt   <module>   s   !	)		