U
    /e6                     @  s   d dl mZ d dlZeeZd dlmZ d dlm	Z	m
Z
mZmZmZ ddlmZ ddlmZ ddlmZ dd	lmZ d
dlmZ e	rddlmZ ddlmZ ddlmZmZ ddlm Z  d
dlm!Z! dZ"G dd dee Z#dddddddZ$dS )    )annotationsN)loads)TYPE_CHECKINGAnyListSetTuple   )serialize_jsoninvoke_with_curdoc)	PatchJson)references_json   )Message)Setter)Document)DocumentPatchedDocumentPatchedEvent)Model)	BufferRef)	patch_docprocess_document_eventsc                   @  sB   e Zd ZdZdZeddddd ddd	ZdddddddZd
S )r   a?   Define the ``PATCH-DOC`` message for sending Document patch events
    between remote documents.

    The ``content`` fragment of for this message is has the form:

    .. code-block:: python

        {
            'events'     : <protocol document events>
            'references' : <model references>
        }

    z	PATCH-DOCTList[DocumentPatchedEvent]boolr   )eventsuse_buffersmetadatareturnc                 K  sv   |   }|stddd |D }t|dkr6tdt||\}}t|}| |||}	|D ]\}
}|	|
| q\|	S )z Create a ``PATCH-DOC`` message

        Args:
            events (list) :
                A list of patch events to apply to a document

        Any additional keyword arguments will be put into the message
        ``metadata`` fragment as-is.

        z-PATCH-DOC message requires at least one eventc                 S  s   h | ]
}|j qS  )document).0eventr   r   E/tmp/pip-unpacked-wheel-f5fndrjf/bokeh/protocol/messages/patch_doc.py	<setcomp>_   s     z#patch_doc.create.<locals>.<setcomp>   zCPATCH-DOC message configured with events for more than one document)Zcreate_header
ValueErrorlenr   r   Z
add_buffer)clsr   r   r   headerZdocsZ
patch_jsonbufferscontentmsgZbuffer_headerpayloadr   r   r#   createN   s    zpatch_doc.createNr   zSetter | NoneNone)docsetterr   c                   s   t   fdd dS )z


        c                     s     jS )N)Zapply_json_patchr+   r   r0   selfr1   r   r#   <lambda>s       z-patch_doc.apply_to_document.<locals>.<lambda>Nr   )r3   r0   r1   r   r2   r#   apply_to_documento   s    zpatch_doc.apply_to_document)T)N)__name__
__module____qualname____doc__Zmsgtypeclassmethodr.   r6   r   r   r   r#   r   =   s
    r   Tr   r   zTuple[str, List[BufferRef]])r   r   r   c                 C  sR   g }t  }|rg nd}| D ]}|||| qt|t|d}t||pNg fS )a:   Create a JSON string describing a patch to be applied as well as
    any optional buffers.

    Args:
      events : list of events to be translated into patches

    Returns:
      str, list :
        JSON string which can be applied to make the given updates to obj
        as well as any optional buffers

    N)r   
references)setappendgenerater   r   r
   )r   r   Zjson_eventsr<   r*   r"   jsonr   r   r#   r   u   s    r   )T)%
__future__r   logging	getLoggerr7   logr@   r   typingr   r   r   r   r   Zcore.json_encoderr
   Zdocument.callbacksr   Zdocument.jsonr   Zdocument.utilr   messager   Zcore.has_propsr   Zdocument.documentr   Zdocument.eventsr   r   modelr   r   __all__r   r   r   r   r   r#   <module>   s$   
	8