
    -e}$                        d Z ddlmZ ddlZ ej        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          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                  (   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<   d6dZd7dZd8dZ	d9dZ
d:dZd;dZd<dZej        d=d             Zd>d$Zd?d&Zd@d'ZedAd(            ZdBdCd,Zd<d-Zd<d.Zd:d/ZdDd3Zd<d4Zd<d5Zd)S )Er   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]_models
set[Model]_new_modelszMultiValuedDict[str, Model]_models_by_namezset[ID]_seen_model_idsdocumentr   c                    t          j        |          | _        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   s     5lib/python3.11/site-packages/bokeh/document/models.py__init__zDocumentModelManager.__init__F   sP     !X...0055"uu    returnc                *    t          | j                  S N)lenr   r   s    r   __len__zDocumentModelManager.__len__U   s    4<   r    idr	   r
   c                    | j         |         S r#   r   r   r'   s     r   __getitem__z DocumentModelManager.__getitem__X   s    |Br    modelNonec                    || j         |<   d S r#   r)   )r   r'   r,   s      r   __setitem__z DocumentModelManager.__setitem__[   s     Rr    boolc                    || j         v S r#   r)   r*   s     r   __contains__z!DocumentModelManager.__contains__^   s    T\!!r    Iterator[Model]c                N    t          | j                                                  S r#   )iterr   valuesr%   s    r   __iter__zDocumentModelManager.__iter__a   s    DL''))***r    c                n    | j                                         D ]}|                                 | ` | `dS )z6 Clean up references to the Documents models

        N)r   r6   destroyr   )r   ms     r   r9   zDocumentModelManager.destroyd   sA     $$&& 	 	AIIKKKKL   r    Generator[None, None, None]c              #  b   K   |                                   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%   s    r   freezezDocumentModelManager.freezeq   s;      ( 	r    namestrlist[Model]c                6    | 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   r@   s     r   get_all_by_namez$DocumentModelManager.get_all_by_name   s     #++D111r    Model | Nonec                8    | 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*   s     r   	get_by_idzDocumentModelManager.get_by_id   s     |D)))r    c                @    | 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   get_onerE   s     r   get_one_by_namez$DocumentModelManager.get_one_by_name   s*     #++D2]VZ2]2]2]^^^r    c                h     t           fd j                                        D                       S )Nc              3  .   K   | ]}|j         v|V  d S r#   )r   ).0r,   r   s     r   	<genexpr>z9DocumentModelManager.synced_references.<locals>.<genexpr>   s0      ]]UuDL\?\?\5?\?\?\?\]]r    )r   r   r6   r%   s   `r   synced_referencesz&DocumentModelManager.synced_references   s4    ]]]]dl&9&9&;&;]]]]]]r    Nis_still_newCallable[[Model], bool] | Nonec                    | j                                          dS t          fd| j         D                       | _         dS )z4 Clean up transient state of the document's models. Nc              3  2   K   | ]} |          |V  d S r#    )rQ   	new_modelrT   s     r   rR   z4DocumentModelManager.flush_synced.<locals>.<genexpr>   s5      "j"jR^R^_hRiRi"j9"j"j"j"j"j"jr    )r   clearr   )r   rT   s    `r   flush_syncedz!DocumentModelManager.flush_synced   sR    ""$$$$$""j"j"j"jd>N"j"j"jjjDr    c                H    | j         dk    r|                                  dS dS )zf Recompute the set of all models, if not currently frozen

        Returns:
            None

        r   Nr   	recomputer%   s    r   
invalidatezDocumentModelManager.invalidate   s0     ""NN #"r    c                j   |                                  }|dS t                      }|j        D ]}||                                z  }t          | j                                                  }||z
  }||z
  }i }t                      }|D ].}	|	||	j        <   |	j        |	                    |	j        |	           /|D ]5}
| j
                            |
j                   |
                                 6|D ]1}|                    |           | j                            |           2|| _        || _        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   roots
referencesr   r6   r   r'   r@   	add_valuer   add_detach_document_attach_documentr   r   )r   r   
new_modelsmr
old_models	to_detach	to_attach
recomputedrecomputed_by_namemnmdmas               r   r^   zDocumentModelManager.recompute   sW    >>##F!$
. 	* 	*B"--//)JJ,,..//
+	+	&(
:I:K:K 	: 	:B "Jruw"",,RWb999 	" 	"B $$RU+++!!!! 	% 	%B)))  $$$$!1r    c                    || 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*   s     r   seenzDocumentModelManager.seen   s     T)))r    old_name
str | Nonenew_namec                ~    || j                             ||           || j                             ||           dS 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   remove_valuerc   )r   r,   rs   ru   s       r   update_namez DocumentModelManager.update_name  sP    (  --h>>> **8U;;;;;  r    c                &    | xj         dz  c_         d S )Nr   )r   r%   s    r   r=   z!DocumentModelManager._push_freeze  s    ar    c                h    | xj         dz  c_         | j         dk    r|                                  d S d S )Nr   r   r]   r%   s    r   r>   z DocumentModelManager._pop_freeze  sB    a""NN #"r    )r   r   )r!   r   )r'   r	   r!   r
   )r'   r	   r,   r
   r!   r-   )r'   r	   r!   r0   )r!   r3   )r!   r-   )r!   r;   )r@   rA   r!   rB   )r'   r	   r!   rG   )r@   rA   r!   rG   )r!   r   r#   )rT   rU   r!   r-   )r,   r
   rs   rt   ru   rt   r!   r-   )__name__
__module____qualname____doc____annotations__r   r   r   r&   r+   r/   r2   r7   r9   
contextlibcontextmanagerr?   rF   rJ   rN   propertyrS   r[   r_   r^   rr   rx   r=   r>   rX   r    r   r   r   6   s           0///0000"suuO$$$$% % % %! ! ! !       ! ! ! !" " " "+ + + +! ! ! !    .
2 
2 
2 
2* * * *_ _ _ _ ^ ^ ^ X^k k k k k   *2 *2 *2 *2^* * * *< < < <2            r    r   )r~   
__future__r   logging	getLoggerr{   logr   r   typingr   r   r   r   
core.typesr	   r,   r
   util.datatypesr   r   r   __all__r   rX   r    r   <module>r      s&    # " " " " " g!!                             , , , , , , #""""""j j j j j j j j j jr    