a
    @¸Dfp$  ã                   @  s”   d Z ddlmZ ddlZe e¡ZddlZddlZddl	m
Z
mZmZmZ ddlmZ ddlmZ ddlmZ e
r~d	d
lmZ dZG dd„ dƒZdS )zR Encapulate the management of Document models with a DocumentModelManager
class.

é    )ÚannotationsN)ÚTYPE_CHECKINGÚCallableÚ	GeneratorÚIteratoré   )ÚID)ÚModel)ÚMultiValuedDicté   )ÚDocument)ÚDocumentModelManagerc                   @  sv  e Zd ZU dZded< ded< ded< ded	< d
ed< eƒ Zded< ddœdd„Zddœdd„Zdddœdd„Z	ddddœdd„Z
dddœdd „Zd!dœd"d#„Zddœd$d%„Zejd&dœd'd(„ƒZd)d*d+œd,d-„Zdd.dœd/d0„Zd)d.d+œd1d2„Zeddœd3d4„ƒZdHd6dd7œd8d9„Zddœd:d;„Zddœd<d=„Zdddœd>d?„Zdd@d@ddAœdBdC„ZddœdDdE„ZddœdFdG„Zd5S )Ir   zÐ Manage and provide access to all of the models that belong to a Bokeh
    Document.

    The set of "all models" means specifically all the models reachable from
    references form a Document's roots.

    zweakref.ReferenceType[Document]Ú	_documentÚintÚ_freeze_countzdict[ID, Model]Ú_modelsz
set[Model]Ú_new_modelszMultiValuedDict[str, Model]Ú_models_by_namezset[ID]Ú_seen_model_idsr   )Údocumentc                 C  s4   t  |¡| _d| _i | _tƒ | _tƒ | _tƒ | _	dS )zœ

        Args:
            document (Document): A Document to manage models for
                A weak reference to the Document will be retained

        r   N)
ÚweakrefÚrefr   r   r   r
   r   Úsetr   r   )Úselfr   © r   úb/nfs/NAS7/SABIOD/METHODE/ermites/ermites_venv/lib/python3.9/site-packages/bokeh/document/models.pyÚ__init__F   s    zDocumentModelManager.__init__)Úreturnc                 C  s
   t | jƒS ©N)Úlenr   ©r   r   r   r   Ú__len__U   s    zDocumentModelManager.__len__r   r	   )Úidr   c                 C  s
   | j | S r   ©r   ©r   r"   r   r   r   Ú__getitem__X   s    z DocumentModelManager.__getitem__ÚNone)r"   Úmodelr   c                 C  s   || j |< d S r   r#   )r   r"   r'   r   r   r   Ú__setitem__[   s    z DocumentModelManager.__setitem__Úboolc                 C  s
   || j v S r   r#   r$   r   r   r   Ú__contains__^   s    z!DocumentModelManager.__contains__zIterator[Model]c                 C  s   t | j ¡ ƒS r   )Úiterr   Úvaluesr    r   r   r   Ú__iter__a   s    zDocumentModelManager.__iter__c                 C  s$   | j  ¡ D ]}| ¡  q
| ` | `dS )z6 Clean up references to the Documents models

        N)r   r,   Údestroyr   )r   Úmr   r   r   r.   d   s    
zDocumentModelManager.destroyzGenerator[None, None, None]c                 c  s   |   ¡  dV  |  ¡  dS )aˆ   Defer expensive model recompuation until intermediate updates are
        complete.

        Making updates to the model graph might trigger events that cause more
        updates. This context manager can be used to prevent expensive model
        recompuation from happening until all events have finished and the
        Document state is quiescent.

        Example:

        .. code-block:: python

            with models.freeze():
                # updates that might change the model graph, that might trigger
                # updates that change the model graph, etc. Recompuation will
                # happen once at the end.

        N)Ú_push_freezeÚ_pop_freezer    r   r   r   Úfreezeq   s    zDocumentModelManager.freezeÚstrzlist[Model])Únamer   c                 C  s   | j  |¡S )z» Find all the models for this Document with a given name.

        Args:
            name (str) : the name of a model to search for

        Returns
            A list of models

        )r   Úget_all©r   r4   r   r   r   Úget_all_by_name‰   s    
z$DocumentModelManager.get_all_by_namezModel | Nonec                 C  s   | j  |d¡S )zè Find the model for this Document with a given ID.

        Args:
            id (ID) : model ID to search for
                If no model with the given ID exists, returns None

        Return:
            a Model or None

        N)r   Úgetr$   r   r   r   Ú	get_by_id•   s    zDocumentModelManager.get_by_idc                 C  s   | j  |d|› d¡S )a   Find a single model for this Document with a given name.

        If multiple models are found with the name, an error is raised.

        Args:
            name (str) : the name of a model to search for

        Returns
            A model with the given name, or None

        z!Found more than one model named 'ú')r   Zget_oner6   r   r   r   Úget_one_by_name¢   s    z$DocumentModelManager.get_one_by_namec                   s   t ‡ fdd„ˆ j ¡ D ƒƒS )Nc                 3  s   | ]}|ˆ j vr|V  qd S r   )r   )Ú.0r'   r    r   r   Ú	<genexpr>²   ó    z9DocumentModelManager.synced_references.<locals>.<genexpr>)r   r   r,   r    r   r    r   Úsynced_references°   s    z&DocumentModelManager.synced_referencesNzCallable[[Model], bool] | None)Úis_still_newr   c                   s2   ˆ du r| j  ¡  nt‡ fdd„| j D ƒƒ| _ dS )z4 Clean up transient state of the document's models. Nc                 3  s   | ]}ˆ |ƒr|V  qd S r   r   )r<   Z	new_model©r@   r   r   r=   ¹   r>   z4DocumentModelManager.flush_synced.<locals>.<genexpr>)r   Úclearr   )r   r@   r   rA   r   Úflush_synced´   s    z!DocumentModelManager.flush_syncedc                 C  s   | j dkr|  ¡  dS )zf Recompute the set of all models, if not currently frozen

        Returns:
            None

        r   N©r   Ú	recomputer    r   r   r   Ú
invalidate»   s    
zDocumentModelManager.invalidatec                 C  sÖ   |   ¡ }|du rdS tƒ }|jD ]}|| ¡ O }q t| j ¡ ƒ}|| }|| }i }tƒ }|D ]&}	|	||	j< |	jdur^| 	|	j|	¡ q^|D ]}
| j
 |
j¡ |
 ¡  qŠ|D ]}| |¡ | j |¡ qª|| _|| _dS )a   Recompute the set of all models based on references reachable from
        the Document's current roots.

        This computation can be expensive. Use ``freeze`` to wrap operations
        that update the model object graph to avoid over-recompuation

        .. note::
            Any models that remove during recomputation will be noted as
            "previously seen"

        N)r   r   ÚrootsZ
referencesr   r,   r
   r"   r4   Ú	add_valuer   ÚaddZ_detach_documentZ_attach_documentr   r   )r   r   Z
new_modelsÚmrZ
old_modelsZ	to_detachZ	to_attachZ
recomputedZrecomputed_by_nameÚmnZmdÚmar   r   r   rE   Å   s.    




zDocumentModelManager.recomputec                 C  s
   || j v S )zÄ Report whether a model id has ever previously belonged to this
        Document.

        Args:
            id (ID) : the model id of a model to check

        Returns:
            bool

        )r   r$   r   r   r   Úseenô   s    zDocumentModelManager.seenz
str | None)r'   Úold_nameÚnew_namer   c                 C  s0   |dur| j  ||¡ |dur,| j  ||¡ dS )aD   Update the name for a model.

        .. note::
            This function and the internal name mapping exist to support
            optimizing the common case of name lookup for models. Keeping a
            dedicated name index is faster than using generic ``bokeh.query``
            functions with a name selector

        Args:
            model (Model) : a model to update the name for

            old_name(str, None) : a previous name for the model, or None

            new_name(str, None) : a new name for the model, or None

        Returns:
            None

        N)r   Zremove_valuerH   )r   r'   rN   rO   r   r   r   Úupdate_name  s    z DocumentModelManager.update_namec                 C  s   |  j d7  _ d S )Nr   )r   r    r   r   r   r0     s    z!DocumentModelManager._push_freezec                 C  s$   |  j d8  _ | j dkr |  ¡  d S )Nr   r   rD   r    r   r   r   r1     s    
z DocumentModelManager._pop_freeze)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú__annotations__r   r   r   r!   r%   r(   r*   r-   r.   Ú
contextlibÚcontextmanagerr2   r7   r9   r;   Úpropertyr?   rC   rF   rE   rM   rP   r0   r1   r   r   r   r   r   6   s8   

/r   )rT   Ú
__future__r   ÚloggingÚ	getLoggerrQ   ÚlogrV   r   Útypingr   r   r   r   Z
core.typesr   r'   r	   Zutil.datatypesr
   r   r   Ú__all__r   r   r   r   r   Ú<module>   s   
