σ
mάJ]c           @` s   d  Z  d d l m Z m Z m Z m Z d d l Z e j e  Z	 d d l
 Z
 d d l m Z d d l m Z d
 Z d e f d	     YZ d S(   un    Assemble WebSocket wire message fragments into complete Bokeh Server
message objects that can be processed.

i    (   t   absolute_importt   divisiont   print_functiont   unicode_literalsN(   t   geni   (   t   ValidationErroru   Receivert   Receiverc           B` sq   e  Z d  Z d   Z e j d    Z d   Z d   Z d   Z	 d   Z
 d   Z d   Z d	   Z d
   Z RS(   uΚ   Receive wire message fragments and assemble complete Bokeh server
    message objects.

    On ``MessageError`` or ``ValidationError``, the receiver will reset its
    state and attempt to consume a new message.

    The *fragment* received can be either bytes or unicode, depending on
    the transport's semantics (WebSocket allows both).

    .. code-block:: python

        [
            # these are required
            b'{header}',        # serialized header dict
            b'{metadata}',      # serialized metadata dict
            b'{content},        # serialized content dict

            # these are optional, and come in pairs; header contains num_buffers
            b'{buf_header}',    # serialized buffer header dict
            b'array'            # raw buffer payload data
            ...
        ]

    The ``header`` fragment will have the form:

    .. code-block:: python

        header = {
            # these are required
            'msgid'       : <str> # a unique id for the message
            'msgtype'     : <str> # a message type, e.g. 'ACK', 'PATCH-DOC', etc

            # these are optional
            'num_buffers' : <int> # the number of additional buffers, if any
        }

    The ``metadata`` fragment may contain any arbitrary information. It is not
    processed by Bokeh for any purpose, but may be useful for external
    monitoring or instrumentation tools.

    The ``content`` fragment is defined by the specific message type.

    c         C` s+   | |  _  |  j |  _ d |  _ d |  _ d S(   uί    Configure a Receiver with a specific Bokeh protocol version.

        Args:
            protocol (Protocol) :
                A Bokeh protocol object to use to assemble collected message
                fragments.
        N(   t	   _protocolt   _HEADERt   _current_consumert   Nonet   _messaget   _buf_header(   t   selft   protocol(    (    s6   lib/python2.7/site-packages/bokeh/protocol/receiver.pyt   __init__^   s    		c         C` s#   |  j  |  t j |  j   d S(   u    Consume individual protocol message fragments.

        Args:
            fragment (``JSON``) :
                A message fragment to assemble. When a complete message is
                assembled, the receiver state will reset to begin consuming a
                new message.

        N(   R	   R   t   ReturnR   (   R   t   fragment(    (    s6   lib/python2.7/site-packages/bokeh/protocol/receiver.pyt   consumek   s    c         C` s;   |  j  |  d  |  _ d  |  _ | g |  _ |  j |  _ d  S(   N(   t   _assume_textR
   R   t   _partialt
   _fragmentst	   _METADATAR	   (   R   R   (    (    s6   lib/python2.7/site-packages/bokeh/protocol/receiver.pyR   y   s
    		c         C` s-   |  j  |  |  j j |  |  j |  _ d  S(   N(   R   R   t   appendt   _CONTENTR	   (   R   R   (    (    s6   lib/python2.7/site-packages/bokeh/protocol/receiver.pyR      s    c         C` s\   |  j  |  |  j j |  |  j d  \ } } } |  j j | | |  |  _ |  j   d  S(   Ni   (   R   R   R   R   t   assembleR   t   _check_complete(   R   R   t   header_jsont   metadata_jsont   content_json(    (    s6   lib/python2.7/site-packages/bokeh/protocol/receiver.pyR      s
    c         C` s&   |  j  |  | |  _ |  j |  _ d  S(   N(   R   R   t   _BUFFER_PAYLOADR	   (   R   R   (    (    s6   lib/python2.7/site-packages/bokeh/protocol/receiver.pyt   _BUFFER_HEADER   s    	c         C` s1   |  j  |  |  j j |  j |  |  j   d  S(   N(   t   _assume_binaryR   t   assemble_bufferR   R   (   R   R   (    (    s6   lib/python2.7/site-packages/bokeh/protocol/receiver.pyR      s    c         C` s7   |  j  j r' |  j  |  _ |  j |  _ n |  j |  _ d  S(   N(   R   t   completeR   R   R	   R   (   R   (    (    s6   lib/python2.7/site-packages/bokeh/protocol/receiver.pyR      s    c         C` s/   t  | t j  s+ t d |  j j   n  d  S(   Nu:   expected text fragment but received binary fragment for %s(   t
   isinstancet   sixt	   text_typeR   R	   t   __name__(   R   R   (    (    s6   lib/python2.7/site-packages/bokeh/protocol/receiver.pyR   ‘   s    c         C` s/   t  | t j  s+ t d |  j j   n  d  S(   Nu:   expected binary fragment but received text fragment for %s(   R#   R$   t   binary_typeR   R	   R&   (   R   R   (    (    s6   lib/python2.7/site-packages/bokeh/protocol/receiver.pyR    ₯   s    (   R&   t
   __module__t   __doc__R   R   t	   coroutineR   R   R   R   R   R   R   R   R    (    (    (    s6   lib/python2.7/site-packages/bokeh/protocol/receiver.pyR   1   s   +				
				(   u   Receiver(   R)   t
   __future__R    R   R   R   t   loggingt	   getLoggerR&   t   logR$   t   tornadoR   t
   exceptionsR   t   __all__t   objectR   (    (    (    s6   lib/python2.7/site-packages/bokeh/protocol/receiver.pyt   <module>
   s   "	