U
    õÕ/e©  ã                   @  st   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
 ddlmZmZ er^ddlmZ dZG d	d
„ d
ƒZdS )z‰ Encapulate the management of any modules that are created in the process
of building a Bokeh Document in a DocumentModelManager class.

é    )ÚannotationsN)Ú
ModuleType)ÚTYPE_CHECKINGÚListé   )ÚDocument)ÚDocumentModuleManagerc                   @  s\   e Zd ZU dZded< ded< ddœdd	„Zd
dœdd„Zdddœdd„Zddœdd„ZdS )r   z[ Keep track of and clean up after modules created while building Bokeh
    Documents.

    zweakref.ReferenceType[Document]Ú	_documentzList[ModuleType]Ú_modulesr   )Údocumentc                 C  s   t  |¡| _g | _dS )z

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

        N)ÚweakrefÚrefr	   r
   )Úselfr   © r   ú:/tmp/pip-unpacked-wheel-f5fndrjf/bokeh/document/modules.pyÚ__init__6   s    zDocumentModuleManager.__init__Úint)Úreturnc                 C  s
   t | jƒS )N)Úlenr
   ©r   r   r   r   Ú__len__A   s    zDocumentModuleManager.__len__r   ÚNone)Úmoduler   c                 C  sB   |j tjkr&td|j ›d|  ¡ ›ƒ‚|tj|j < | j |¡ dS )a	   Add a module associated with a Document.

        .. note::
            This method will install the module in ``sys.modules``

        Args:
            module (Module) : a module to install for the configured Document

        Returns:
            None

        z Add called already-added module z for N)Ú__name__ÚsysÚmodulesÚRuntimeErrorr	   r
   Úappend)r   r   r   r   r   ÚaddD   s    zDocumentModuleManager.addc                   sÌ   ddl m} ddlm‰  t dtˆjƒ› dˆ ¡ ›¡ ˆjD ]‚}||ƒ}dd„ |D ƒ}‡fdd„|D ƒ}‡ fd	d„|D ƒ}t|ƒdkržt 	d
|›d|›¡ |j
tjkr´tj|j
= |j ¡  ~q>g ˆ_dS )zŽ Clean up any added modules, and check that there are no unexpected
        referrers afterwards.

        Returns:
            None

        r   )Úget_referrers©Ú	FrameTypez	Deleting z modules for document c                 S  s   g | ]}|t jk	r|‘qS r   )r   r   ©Ú.0Úxr   r   r   Ú
<listcomp>q   s     
 z1DocumentModuleManager.destroy.<locals>.<listcomp>c                   s   g | ]}|ˆ j k	r|‘qS r   )r
   r"   r   r   r   r%   r   s     
 c                   s   g | ]}t |ˆ ƒs|‘qS r   )Ú
isinstancer"   r    r   r   r%   s   s     
 zModule z] has extra unexpected referrers! This could indicate a serious memory leak. Extra referrers: N)Úgcr   Útypesr!   ÚlogÚdebugr   r
   r	   Úerrorr   r   r   Ú__dict__Úclear)r   r   r   Z	referrersr   )r!   r   r   ÚdestroyV   s     


zDocumentModuleManager.destroyN)	r   Ú
__module__Ú__qualname__Ú__doc__Ú__annotations__r   r   r   r.   r   r   r   r   r   -   s   
r   )r1   Ú
__future__r   ÚloggingÚ	getLoggerr   r)   r   r   r(   r   Útypingr   r   r   r   Ú__all__r   r   r   r   r   Ú<module>   s   
