U
    /e                     @  s   d Z ddlmZ ddlZeeZddlmZm	Z	m
Z
mZmZmZ ddlmZ ddlmZ erddlmZ dd	lmZmZ dd
lmZ dZddddddddZddddddZdddddZdS )zO Provide ancillary utility functions useful for manipulating Bokeh
documents.

    )annotationsN)TYPE_CHECKINGDictIterableListMappingType   )HasProps)	get_class)Setter)IDReferenceJson)Model)initialize_references_jsoninstantiate_references_jsonreferences_jsonzList[ReferenceJson]zMapping[ID, Model]zSetter | NoneNone)r   
referencessetterreturnc                 C  sF   | D ]<}|d }|d }|| }|j s0t| |j|||d qdS )a   Given a JSON representation of the models in a graph, and new model
    objects, set the properties on the models from the JSON

    Args:
        references_json (``JSON``)
            JSON specifying attributes and values to initialize new model
            objects with.

        references (dict[str, Model])
            A dictionary mapping model IDs to newly created (but not yet
            initialized) Bokeh models.

            **This is an "out" parameter**. The values it contains will be
            modified in-place.

        setter (ClientSession or ServerSession or None, optional) :
                This is used to prevent "boomerang" updates to Bokeh apps.
                (default: None)

                In the context of a Bokeh server application, incoming updates
                to properties will be annotated with the session that is
                doing the updating. This value is propagated through any
                subsequent change notifications that the update triggers.
                The session can compare the event setter to itself, and
                suppress any updates that originate from itself.

    id
attributes)modelsr   N)_initializedr
   __init__Zupdate_from_json)r   r   r   objobj_idZ	obj_attrsinstance r   7/tmp/pip-unpacked-wheel-f5fndrjf/bokeh/document/util.pyr   =   s    
r   zDict[ID, Model])r   existing_instancesr   c                 C  s   i }| D ]t}|d }| ddp&|d }||kr>|| ||< qt|}|j||d}|dkrrtd| d| d|||j< q|S )	z Given a JSON representation of all the models in a graph, return a
    dict of new model objects.

    Args:
        references_json (``JSON``)
            JSON specifying new Bokeh models to create

    Returns:
        dict[str, Model]

    r   subtypeNtype)r   z%Error loading model from JSON (type: z, id: ))getr   __new__RuntimeErrorr   )r   r!   r   r   r   Zobj_typeclsr   r   r   r    r   i   s    r   zIterable[Model])r   r   c                 C  s2   g }| D ]$}|j }|jdd|d< || q|S )z Given a list of all models in a graph, return JSON representing
    them and their properties.

    Args:
        references (seq[Model]) :
            A list of models to convert to JSON

    Returns:
        list

    F)Zinclude_defaultsr   )structZ_to_json_likeappend)r   r   rr)   r   r   r    r      s    r   )N)__doc__
__future__r   logging	getLogger__name__logtypingr   r   r   r   r   r   Zcore.has_propsr
   modelr   r   Z
core.typesr   r   r   __all__r   r   r   r   r   r   r    <module>   s   
 
 ,