o
    ê}”e™9  ã                   @  sŒ  U d dl m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Z	d dl
mZ d dlmZ ddlmZ eejejhƒZh e£ej’ej’ZeedƒrRe ej¡ eeƒZ[h d£Zd6dd„Zd7dd„Zd8dd„Zd8dd„Zd9dd„Zd:dd„Z G dd„ dƒZ!G d d!„ d!e!ƒZ"G d"d#„ d#e!ƒZ#e"e#d$œZ$d%e%d&< ze&d'ƒ W n e'y®   e$d( e$d)< Y nw e$d* e$d)< d;d+d,„Z(				)d<d=d4d5„Z)dS )>é    )ÚannotationsN)Úchain)ÚPurePathé   )Ú_logÚreal_prefix>   z.toxz.pytest_cachez.noxz.hgz.gitz.mypy_cacheÚ__pycache__Úreturnút.Iterator[str]c                  c  sr   � t tj ¡ ƒD ].} t| ddƒ}|du s| t¡rqtj 	|¡s3|}tj 
|¡}||kr-n	tj 	|¡r |V  qdS )z;Find the filesystem paths associated with imported modules.Ú__file__N)ÚlistÚsysÚmodulesÚvaluesÚgetattrÚ
startswithÚ_ignore_alwaysÚosÚpathÚisfileÚdirname)ÚmoduleÚnameÚold© r   ú[/Users/abhinavbhinder/Documents/map/venv/lib/python3.10/site-packages/werkzeug/_reloader.pyÚ_iter_module_paths(   s   €ú€òr   Úpathsúset[str]Úexclude_patternsÚNonec                 C  s    |D ]}|   t | |¡¡ qd S ©N)Údifference_updateÚfnmatchÚfilter)r   r   Úpatternr   r   r   Ú_remove_by_pattern<   s   ÿr&   Úextra_filesút.Iterable[str]c           
   	   C  sú   t ƒ }tttjƒ| ƒD ]d}tj |¡}tj |¡r| |¡ qtj 	|¡di}t 
|¡D ]B\}}}| t¡s>tj |¡tv rC| ¡  q,d}|D ]}	|	 d¡rZd}| tj ||	¡¡ qG|sj|tj 	|¡ sj| ¡  q,|||< q,q| tƒ ¡ t||ƒ |S )ax  Find paths for the stat reloader to watch. Returns imported
    module files, Python files under non-system paths. Extra files and
    Python files under extra directories can also be scanned.

    System paths have to be excluded for efficiency. Non-system paths,
    such as a project root or ``sys.path.insert``, should be the paths
    of interest to the user anyway.
    TF)z.pyz.pyc)Úsetr   r   r   r   r   Úabspathr   Úaddr   Úwalkr   Ú_stat_ignore_scanÚbasenameÚ_ignore_common_dirsÚclearÚendswithÚjoinÚupdater   r&   )
r'   r   r   r   Zparent_has_pyÚrootÚdirsÚfilesZhas_pyr   r   r   r   Ú_find_stat_pathsA   s8   
ÿþ
€
è
r7   c                 C  sz   t ƒ }tttjƒ| ƒD ]}tj |¡}tj |¡rtj |¡}| 	|¡ qt
ƒ D ]}| 	tj |¡¡ q(t||ƒ t|ƒS )z­Find paths for the stat reloader to watch. Looks at the same
    sources as the stat reloader, but watches everything under
    directories instead of individual files.
    )r)   r   r   r   r   r   r*   r   r   r+   r   r&   Ú_find_common_roots)r'   r   r5   r   r   r   r   Ú_find_watchdog_pathsw   s   

r9   c                   sh   i }t dd„ | D ƒtdd�D ]}|}|D ]}| |i ¡}q| ¡  qtƒ ‰d‡ ‡fdd„‰ ˆ |dƒ ˆS )Nc                 s  s   � | ]}t |ƒjV  qd S r!   )r   Úparts©Ú.0Úxr   r   r   Ú	<genexpr>’   s   € z%_find_common_roots.<locals>.<genexpr>T)ÚkeyÚreverseÚnodeút.Mapping[str, dict]r   útuple[str, ...]r	   r    c                   s@   |   ¡ D ]\}}ˆ |||f ƒ q| sˆ tjj|Ž ¡ d S d S r!   )Úitemsr+   r   r   r2   )rA   r   ÚprefixÚchild©Ú_walkÚrvr   r   rH   œ   s
   ÿz!_find_common_roots.<locals>._walkr   )rA   rB   r   rC   r	   r    )ÚsortedÚlenÚ
setdefaultr0   r)   )r   r4   ÚchunksrA   Úchunkr   rG   r   r8   �   s   

r8   ú	list[str]c                  C  sx  t jdkrt jgt jdd… ¢S t jg} t jd }t jdd… }t jd }t|ddƒdu sFtjdkr„|j	dkr„tj
 |¡s„tj
 |› d	�¡r„tj
 |¡}tjdkr~tj
 |¡sdtj
 |› d	�¡rd|d	7 }tj
 t j¡d d	kr~tj
 |¡d d	kr~|  d¡ |  |¡ n1tj
 |¡r©t t|j	¡}tj
 tj
 |¡¡d }|dkr¨|d
|› �7 }n|}|  d| d
¡f¡ |  |¡ | S )zpDetermine how the script was executed, and return the args needed
    to execute it again in a new process.
    )é   é
   r   Nr   Ú__main__Ú__package__ÚntÚ z.exeÚ.z-m)r   Úversion_infoÚ
executableÚ	orig_argvÚargvr   r   r   r   rS   r   Úexistsr*   ÚsplitextÚpopÚappendr   ÚtÚcastÚstrr.   ÚextendÚlstrip)rI   Z	py_scriptÚargsrR   Z	py_moduler   r   r   r   Ú_get_args_for_reloading§   sF   


ÿþ
ýü
ÿþ
€
re   c                   @  sf   e Zd ZdZ			dddd„Zd dd„Zdd„ Zd!dd„Zd!dd„Zd"dd„Z	d#dd„Z
d#dd„ZdS )$ÚReloaderLooprU   Nr   r'   út.Iterable[str] | Noner   Úintervalúint | floatr	   r    c                 C  s,   dd„ |pdD ƒ| _ t|pdƒ| _|| _d S )Nc                 S  s   h | ]}t j |¡’qS r   )r   r   r*   r;   r   r   r   Ú	<setcomp>ë   ó    z(ReloaderLoop.__init__.<locals>.<setcomp>r   )r'   r)   r   rh   )Úselfr'   r   rh   r   r   r   Ú__init__å   s   
zReloaderLoop.__init__c                 C  s   |   ¡  | S )zgDo any setup, then run one step of the watch to populate the
        initial filesystem state.
        )Úrun_step©rl   r   r   r   Ú	__enter__ï   s   zReloaderLoop.__enter__c                 C  ó   dS )z4Clean up any resources associated with the reloader.Nr   ©rl   Úexc_typeÚexc_valÚexc_tbr   r   r   Ú__exit__ö   s   zReloaderLoop.__exit__c                 C  s   	 |   ¡  t | j¡ q)zfContinually run the watch step, sleeping for the configured
        interval after each step.
        )rn   ÚtimeÚsleeprh   ro   r   r   r   Úrunú   s   þzReloaderLoop.runc                 C  rq   )z}Run one step for watching the filesystem. Called once to set
        up initial state, then repeatedly to update it.
        Nr   ro   r   r   r   rn     s   zReloaderLoop.run_stepÚintc                 C  sJ   	 t dd| j› �ƒ tƒ }tj ¡ }d|d< tj||dd�}|dkr$|S q)	z|Spawn a new Python interpreter with the same arguments as the
        current one, but running the reloader thread.
        TÚinfoz * Restarting with ÚtrueÚWERKZEUG_RUN_MAINF)ÚenvÚ	close_fdsrP   )r   r   re   r   ÚenvironÚcopyÚ
subprocessÚcall)rl   rd   Znew_environZ	exit_coder   r   r   Úrestart_with_reloader  s   
øz"ReloaderLoop.restart_with_reloaderÚfilenamera   c                 C  s   |   |¡ t d¡ d S ©NrP   )Ú
log_reloadr   Úexit©rl   r…   r   r   r   Útrigger_reload  ó   
zReloaderLoop.trigger_reloadc                 C  s"   t j |¡}tdd|›d�ƒ d S )Nr{   z * Detected change in z, reloading)r   r   r*   r   r‰   r   r   r   r‡     s   zReloaderLoop.log_reload)NNr   )r'   rg   r   rg   rh   ri   r	   r    ©r	   rf   ©r	   r    )r	   rz   ©r…   ra   r	   r    )Ú__name__Ú
__module__Ú__qualname__r   rm   rp   rv   ry   rn   r„   rŠ   r‡   r   r   r   r   rf   â   s    ü





rf   c                      s,   e Zd ZdZd	‡ fdd„Zd
dd„Z‡  ZS )ÚStatReloaderLoopÚstatr	   rf   c                   s   i | _ tƒ  ¡ S r!   )ÚmtimesÚsuperrp   ro   ©Ú	__class__r   r   rp   "  s   
zStatReloaderLoop.__enter__r    c              	   C  sn   t | j| jƒD ]-}zt |¡j}W n	 ty   Y qw | j |¡}|d u r+|| j|< q||kr4|  	|¡ qd S r!   )
r7   r'   r   r   r“   Úst_mtimeÚOSErrorr”   ÚgetrŠ   )rl   r   ÚmtimeZold_timer   r   r   rn   &  s   ÿ

€ózStatReloaderLoop.run_steprŒ   r�   )r�   r�   r‘   r   rp   rn   Ú__classcell__r   r   r–   r   r’     s    r’   c                      sR   e Zd Zd‡ fdd„Zdd
d„Zd‡ fdd„Zdd„ Zddd„Zddd„Z‡  Z	S )ÚWatchdogReloaderLooprd   út.AnyÚkwargsr	   r    c           	        sÚ   ddl m} ddlm} ddlm‰  ddlm} tƒ j|i |¤Ž | j‰G ‡ ‡fdd„d|ƒ}|j	 
¡ }| d¡r?|d d	… }d
|› d�| _|ƒ | _dd„ | jD ƒ}|dddg|¢g dd„ tD ƒ¢| j¢d�| _d| _d S )Nr   )ÚObserver)ÚPatternMatchingEventHandler)ÚEVENT_TYPE_OPENED)ÚFileModifiedEventc                      s   e Zd Zd‡ ‡fdd„ZdS )z3WatchdogReloaderLoop.__init__.<locals>.EventHandlerÚeventr£   c                   s   |j ˆ krd S ˆ|jƒ d S r!   )Z
event_typeZsrc_path)rl   r¤   ©r¢   rŠ   r   r   Úon_any_eventB  s   
z@WatchdogReloaderLoop.__init__.<locals>.EventHandler.on_any_eventN)r¤   r£   )r�   r�   r‘   r¦   r   r¥   r   r   ÚEventHandlerA  s    r§   Úobserveriøÿÿÿz
watchdog (ú)c                 S  s   g | ]
}t j |¡s|‘qS r   )r   r   Úisdir)r<   Úpr   r   r   Ú
<listcomp>T  s    z1WatchdogReloaderLoop.__init__.<locals>.<listcomp>z*.pyz*.pycz*.zipc                 S  s   g | ]}d |› d�‘qS )z*/z/*r   )r<   Údr   r   r   r¬   X  rk   )ÚpatternsÚignore_patternsF)Zwatchdog.observersr    Zwatchdog.eventsr¡   r¢   r£   r•   rm   rŠ   r�   Úlowerr1   r   r¨   r'   r/   r   Úevent_handlerÚshould_reload)	rl   rd   rŸ   r    r¡   r£   r§   Zreloader_nameZextra_patternsr–   r¥   r   rm   8  s,   

ÿþþ
zWatchdogReloaderLoop.__init__r…   ra   c                 C  s   d| _ |  |¡ d S )NT)r²   r‡   r‰   r   r   r   rŠ   ^  s   z#WatchdogReloaderLoop.trigger_reloadrf   c                   s   i | _ | j ¡  tƒ  ¡ S r!   )Úwatchesr¨   Ústartr•   rp   ro   r–   r   r   rp   e  s   

zWatchdogReloaderLoop.__enter__c                 C  s   | j  ¡  | j  ¡  d S r!   )r¨   Ústopr2   rr   r   r   r   rv   j  r‹   zWatchdogReloaderLoop.__exit__c                 C  s.   | j s|  ¡  t | j¡ | j rt d¡ d S r†   )r²   rn   rw   rx   rh   r   rˆ   ro   r   r   r   ry   n  s
   þzWatchdogReloaderLoop.runc              	   C  sž   t | jƒ}t| j| jƒD ]*}|| jvr1z| jj| j|dd�| j|< W n ty0   d | j|< Y nw | 	|¡ q|D ]}| j 
|d ¡}|d urL| j |¡ q9d S )NT)Ú	recursive)r)   r³   r9   r'   r   r¨   Zscheduler±   r™   Údiscardr]   Z
unschedule)rl   Z	to_deleter   Zwatchr   r   r   rn   u  s"   

ÿü€üzWatchdogReloaderLoop.run_step)rd   rž   rŸ   rž   r	   r    rŽ   rŒ   r�   )
r�   r�   r‘   rm   rŠ   rp   rv   ry   rn   rœ   r   r   r–   r   r�   7  s    
&
r�   )r“   Úwatchdogzdict[str, type[ReloaderLoop]]Úreloader_loopszwatchdog.observersr“   Úautor¸   c                  C  s€   t jdu s
t j ¡ sdS zddl} W n
 ty   Y dS w |  t j¡}|d | j@ s>|d  | jO  < |  t j| j|¡ dS dS )zuEnsure that echo mode is enabled. Some tools such as PDB disable
    it which causes usability issues after a reload.Nr   rP   )	r   ÚstdinÚisattyÚtermiosÚImportErrorÚ	tcgetattrÚECHOÚ	tcsetattrÚTCSANOW)r½   Ú
attributesr   r   r   Úensure_echo_onš  s   ÿþrÄ   Ú	main_funcút.Callable[[], None]rg   rh   ri   Úreloader_typera   c                 C  s¾   ddl }|  |jdd„ ¡ t| |||d�}z>tj d¡dkrKtƒ  tj| dd	�}d
|_	|� | 
¡  | ¡  W d  ƒ W dS 1 sCw   Y  W dS t | ¡ ¡ W dS  ty^   Y dS w )z<Run the given function in an independent Python interpreter.r   Nc                  W  s
   t  d¡S )Nr   )r   rˆ   )rd   r   r   r   Ú<lambda>·  s   
 z#run_with_reloader.<locals>.<lambda>)r'   r   rh   r}   r|   r   )Útargetrd   T)ÚsignalÚSIGTERMr¹   r   r€   rš   rÄ   Ú	threadingÚThreadÚdaemonr´   ry   r   rˆ   r„   ÚKeyboardInterrupt)rÅ   r'   r   rh   rÇ   rÊ   Zreloaderr_   r   r   r   Úrun_with_reloader­  s$   ÿ
&þÿrÐ   )r	   r
   )r   r   r   r   r	   r    )r'   r   r   r   r	   r(   )r   r(   r	   r(   )r	   rO   r�   )NNr   rº   )rÅ   rÆ   r'   rg   r   rg   rh   ri   rÇ   ra   r	   r    )*Ú
__future__r   r#   r   r‚   r   rÌ   rw   Útypingr_   Ú	itertoolsr   Úpathlibr   Ú	_internalr   ÚtupleÚbase_prefixÚbase_exec_prefixr   rE   Úexec_prefixÚhasattrr+   r   r-   r/   r   r&   r7   r9   r8   re   rf   r’   r�   r¹   Ú__annotations__Ú
__import__r¾   rÄ   rÐ   r   r   r   r   Ú<module>   sT    




6

;=Wþÿ
û