a
    ‚½Df*   ã                   @   s$  d dl Z d dlZd dlZd dlZd dlZd dlZd dlZd dlmZ d dl	m
Z
 zd dlmZ W n  ey€   ddœdd„ZY n0 dd	lmZ d
dlmZ e d¡Zeƒ Zeƒ Zeƒ Zg d¢Zg d¢Zdd„ Zdd„ Zdd„ Zd#dd„Zd$dd„Zdd„ Zdd„ Zed%dd„ƒZ dd „ Z!d!d"„ Z"dS )&é    N)Úcontextmanager)ÚCodeHandler)Úawatch©Ú
stop_eventc                 G  sd   | p
t  ¡ } i }|  ¡ s`tƒ }|D ] }t||ƒ}|r"| ||f¡ q"|rN|V  t  d¡I d H  qd S )Ng      à?)ÚasyncioÚEventÚis_setÚsetÚ_check_fileÚaddÚsleep)r   ÚfilesÚmodify_timesÚchangesÚpathZchange© r   ú\/nfs/NAS7/SABIOD/METHODE/ermites/ermites_venv/lib/python3.9/site-packages/panel/io/reload.pyr      s    
r   é   )Úfullpathé   )Ústatezpanel.io.reload)z**/.*z**/anacondaz**/anaconda2z**/anaconda3z**/dist-packagesz**/minicondaz**/miniconda2z**/miniconda3z**/node_modulesz**/pyenvz**/site-packagesz**/venvz**/virtualenv)Z	bokeh_appzgeoviews.models.zpanel.ztorch.c                    s   t ‡ fdd„tD ƒƒS )Nc                 3   s   | ]}t ˆ |ƒV  qd S ©N)Úfile_is_in_folder_glob)Ú.0Zdenylisted_folder©Úfilepathr   r   Ú	<genexpr>?   s   ÿzin_denylist.<locals>.<genexpr>)ÚanyÚDEFAULT_FOLDER_DENYLISTr   r   r   r   Úin_denylist>   s    þr    c                 C   sB   |  d¡s&|  d¡r|d7 }n|d7 }tj | ¡d }t ||¡S )zÜ
    Test whether a file is in some folder with globbing support.

    Parameters
    ----------
    filepath : str
        A file path.
    folderpath_glob: str
        A path to a folder that may include globbing.
    Ú*ú/z/*)ÚendswithÚosr   ÚdirnameÚfnmatch)r   Zfolderpath_globZfile_dirr   r   r   r   D   s    


r   c                  C   s”   t tƒ} i }ttB D ]v}|tjvr$qtj| }t|tjƒs<qt	|dd ƒ}|sNq| 
d¡rd|d d… }tj tj |¡¡}|||< |  |¡ q|| fS )NÚ__file__)z.pycz.pyoéÿÿÿÿ)ÚlistÚ_watched_filesÚ_modulesÚ_local_modulesÚsysÚmodulesÚ
isinstanceÚtypesÚ
ModuleTypeÚgetattrr#   r$   r   ÚabspathÚrealpathÚappend)r   Úmodule_pathsÚmodule_nameÚmoduler   r   r   r   Úwatched_modulesZ   s"    


r9   c                 Ã   sV   t ƒ \}}t|d| iŽ2 z*3 d H W }t||ƒ t d¡I d H   qFq6 |  ¡ r qRq d S )Nr   r   )r9   r   Ú_reloadr   r   r	   )r   r6   r   r   r   r   r   Úasync_file_watcherq   s    

r;   c                 Ã   sN   zddl }W n" ty.   tjdtdd Y n0 t d¡ t| dI dH  dS )ze
    Installs a periodic callback which checks for changes in watched
    files and sys.modules.
    r   Nzß--autoreload functionality now depends on the watchfiles library. In future versions autoreload will not work without watchfiles being installed. Since it provides a much better user experience consider installing it today.)Ú
stacklevelz%Setting up global autoreload watcher.r   )Ú
watchfilesÚ	ExceptionÚwarningsÚwarnÚFutureWarningÚ_reload_loggerÚdebugr;   )r   r=   r   r   r   Úsetup_autoreload_watcher{   s    û
rD   c                 C   s   t  | ¡ dS )z'
    Add a file to the watch list.
    N)r*   r   )Úfilenamer   r   r   Úwatch   s    rF   c                 C   s0   zt j || g¡|kW S  ty*   Y dS 0 d S )NF)r$   r   Ú
commonpathr>   )Úsubpathr   r   r   r   Ú
is_subpath“   s    rI   c              	   #   s‚  t ƒ }t|dƒr&| tj |jj¡¡ | p,dD ]H}|js:q.|jD ]}t|t	ƒr@ qVq@q.t|dƒr.| tj |jj¡¡ q.t t
jƒ}dV  t t
jƒ |¡D ]ä‰ t‡ fdd„tD ƒƒr´q˜t
jˆ  }z¤t|ddƒ}|du rît|ddƒ}|du rôW q˜n|j}t|ƒ}|du st|ƒrW q˜tj |¡s&W q˜tj |¡‰t‡fdd„|D ƒƒrVt ˆ ¡ n
t ˆ ¡ W q˜ tyz   Y q˜Y q˜0 q˜dS )	z7
    Records modules which are currently imported.
    Ú_runnerr   Nc                 3   s   | ]}ˆ   |¡V  qd S r   ©Ú
startswith)r   Zimodule)r7   r   r   r   ®   ó    z!record_modules.<locals>.<genexpr>Ú__spec__r'   c                 3   s    | ]}ˆ |kpt |ˆ ƒV  qd S r   )rI   )r   Zapp_path)Úparent_pathr   r   r   Ã   rM   )r
   Úhasattrr   r$   r   r%   rJ   Ú	_handlersr/   r   r-   r.   Ú
differencer   ÚIGNORED_MODULESr2   Úoriginr   r    Úisfiler,   r+   r>   )ZapplicationsÚhandlerZ	app_pathsZappr.   r8   Úspecr   r   )r7   rO   r   Úrecord_modules™   sH    





rX   c           
         sæ   t  d¡ d\}}|D ]B\}}|| vr(q| | ‰ ˆ tv rTt‡ fdd„tD ƒƒsTd}qd}qtƒ }|rl|tO }|st|r||tO }|D ]‰ ˆ tjv r€tjˆ = q€tj	 
¡ D ]>\}}|js´q¢q¢tj |¡rÈd|_q¢|fdd„}	| d|	¡ q¢d	S )
aU  
    Reloads modules depending on the module files that were changed.
    Specifically we make a distinction between local modules relative
    to the current application paths and global modules. This allows
    us to reload the application itself, any local modules imported
    by the application or all global modules independently.
    zQChanges detected by autoreload watcher, unloading modules and reloading sessions.)FFc                 3   s   | ]}|  ˆ › d ¡V  qdS )Ú.NrK   )r   Zm_©r8   r   r   r   Ù   rM   z_reload.<locals>.<genexpr>Tc                 S   s
   d|_ d S )NT)Úreload)ÚeventÚlocr   r   r   Úreload_sessionî   s    z_reload.<locals>.reload_sessionZdocument_readyN)rB   rC   r,   r   r+   r
   r-   r.   r   Z
_locationsÚitemsZsession_contextÚ_loadedÚgetr[   Zon_event)
r6   r   Zlocal_Zglobal_Ú_r   Zmodules_to_deleteÚdocr]   r^   r   rZ   r   r:   Ê   s0    


r:   c                 C   sx   |  | ¡}zt | ¡j}W n0 ty8   |r2Y dS Y dS  tyJ   Y dS 0 |du r`||| < dS ||krt||| < dS dS )aÔ  
    Checks if a file was modified or deleted and then returns a code,
    modeled after watchfiles, indicating the type of change:

    - 0: No change
    - 2: File modified
    - 3: File deleted

    Arguments
    ---------
    path: str | os.PathLike
      Path of the file to check for modification
    modify_times: dict[str, int]
      Dictionary of modification times for different paths.

    Returns
    -------
    Status code indicating type of change.
    é   r   Nr   )ra   r$   ÚstatÚst_mtimeÚFileNotFoundErrorr>   )r   r   Zlast_modifiedÚmodifiedr   r   r   r   ò   s    
r   )N)N)NN)#r   r&   Úloggingr$   r-   r0   r?   Ú
contextlibr   Zbokeh.application.handlersr   r=   r   r>   Úutilr   r   Ú	getLoggerrB   r
   r*   r+   r,   r   rS   r    r   r9   r;   rD   rF   rI   rX   r:   r   r   r   r   r   Ú<module>   s>   



0(