U
    ÌZjû7  ã                   @   s>  U 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eejejh™Zeedƒr”e ej¡ eeƒZ[ddd	d
dddhZeje dœdd„Zeje eje ddœdd„Zeje eje eje dœdd„Zeje eje eje dœdd„Z eje eje dœdd„Z!ej"e 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'ej(eej)e$ f e*d%< ze+d&ƒ W n" e,k
�rØ   e'd' e'd(< Y nX e'd) e'd(< ddœd*d+„Z-d/ej.g df ej/eje  ej/eje  ej0e1e2f edd,œd-d.„Z3dS )0é    N)Úchain)ÚPurePathé   )Ú_logÚreal_prefixÚ__pycache__z.gitz.hgz.toxz.noxz.pytest_cachez.mypy_cache©Úreturnc                  c   sf   t tj ¡ ƒD ]R} t| ddƒ}|dks| t¡r2qtj 	|¡sZ|}tj 
|¡}||kr2qq2|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   úU/var/www/html/TRUCKING_PROJECT/venv/lib/python3.8/site-packages/werkzeug/_reloader.pyÚ_iter_module_paths&   s    r   )ÚpathsÚexclude_patternsr	   c                 C   s    |D ]}|   t | |¡¡ qd S ©N)Údifference_updateÚfnmatchÚfilter)r   r   Úpatternr   r   r   Ú_remove_by_pattern:   s    r#   )Úextra_filesr   r	   c           
   	   C   sú   t ƒ }tttjƒ| ƒD ]È}tj |¡}tj |¡r>| |¡ qtj 	|¡di}t 
|¡D ]„\}}}| t¡s|tj |¡tkr†| ¡  qXd}|D ]&}	|	 d¡rŽd}| tj ||	¡¡ qŽ|sÔ|tj 	|¡ sÔ| ¡  qX|||< qXq| 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_paths?   s4    
ÿþ

r3   c                 C   sz   t ƒ }tttjƒ| ƒD ]2}tj |¡}tj |¡r>tj |¡}| 	|¡ qt
ƒ D ]}| 	tj |¡¡ qPt||ƒ 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   r1   r   r   r   r   Ú_find_watchdog_pathsu   s    

r5   )r   r	   c                    s„   i }t dd„ | D ƒtdd�D ]&}|}|D ]}| |i ¡}q(| ¡  qtƒ ‰tjttf tj	tdf d 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.)Únoder   r	   c                    s<   |   ¡ D ]\}}ˆ |||f ƒ q| s8ˆ tjj|Ž ¡ d S r   )Úitemsr'   r   r   r.   )r=   r   ÚprefixÚchild©Ú_walkÚrvr   r   rB   š   s    z!_find_common_roots.<locals>._walkr   )
ÚsortedÚlenÚ
setdefaultr,   r%   ÚtÚMappingÚstrÚdictÚTuple)r   r0   Úchunksr=   Úchunkr   rA   r   r4   �   s    
,
r4   c                  C   s\  t jg} t jd }t jdd… }t jd }t|ddƒdksltjdkrè|jdkrètj 	|¡sètj 	|› d�¡rètj 
|¡}tjdkrÜtj 	|¡s¨tj 	|› d�¡r¨|d7 }tj t j¡d dkrÜtj |¡d dkrÜ|  d¡ |  |¡ nftj |¡�r6t 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   r   NÚ__main__Ú__package__ÚntÚ z.exeÚ.z-m)r   Ú
executableÚargvr   r   r   r   rO   r   Úexistsr&   ÚsplitextÚpopÚappendr   rG   ÚcastrI   r*   ÚextendÚlstrip)rC   Z	py_scriptÚargsrN   Z	py_moduler   r   r   r   Ú_get_args_for_reloading¥   s@    

ÿþ
ýü
ÿþ


r]   c                   @   s¨   e Zd ZdZdejeje  ejeje  eje	e
f ddœdd„Zd dœdd	„Zd
d„ Zddœdd„Zddœdd„Ze	dœdd„Zeddœdd„Zeddœdd„ZdS )ÚReloaderLooprQ   Nr   )r$   r   Úintervalr	   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&   r7   r   r   r   Ú	<setcomp>ä   s     z(ReloaderLoop.__init__.<locals>.<setcomp>r   )r$   r%   r   r_   )Úselfr$   r   r_   r   r   r   Ú__init__Þ   s    zReloaderLoop.__init__r   c                 C   s   |   ¡  | S )zgDo any setup, then run one step of the watch to populate the
        initial filesystem state.
        )Úrun_step©ra   r   r   r   Ú	__enter__è   s    zReloaderLoop.__enter__c                 C   s   dS )z4Clean up any resources associated with the reloader.Nr   ©ra   Úexc_typeÚexc_valÚexc_tbr   r   r   Ú__exit__ï   s    zReloaderLoop.__exit__c                 C   s   |   ¡  t | j¡ q dS )zfContinually run the watch step, sleeping for the configured
        interval after each step.
        N)rc   ÚtimeÚsleepr_   rd   r   r   r   Úrunó   s    zReloaderLoop.runc                 C   s   dS )z}Run one step for watching the filesystem. Called once to set
        up initial state, then repeatedly to update it.
        Nr   rd   r   r   r   rc   û   s    zReloaderLoop.run_stepc                 C   sL   t dd| j› �ƒ tƒ }tj ¡ }d|d< tj||dd�}|dkr |S q dS )	z|Spawn a new Python interpreter with the same arguments as the
        current one, but running the reloader thread.
        Úinfoz * Restarting with ÚtrueÚWERKZEUG_RUN_MAINF)ÚenvÚ	close_fdsé   N)r   r   r]   r   ÚenvironÚcopyÚ
subprocessÚcall)ra   r\   Znew_environÚ	exit_coder   r   r   Úrestart_with_reloader  s    
z"ReloaderLoop.restart_with_reloader©Úfilenamer	   c                 C   s   |   |¡ t d¡ d S ©Nrs   )Ú
log_reloadr   Úexit©ra   r{   r   r   r   Útrigger_reload  s    
zReloaderLoop.trigger_reloadc                 C   s"   t j |¡}tdd|›d�ƒ d S )Nrn   z * Detected change in z, reloading)r   r   r&   r   r   r   r   r   r}     s    zReloaderLoop.log_reload)NNr   )Ú__name__Ú
__module__Ú__qualname__r   rG   ÚOptionalÚIterablerI   ÚUnionÚintÚfloatrb   re   rj   rm   rc   ry   r€   r}   r   r   r   r   r^   Û   s"      üû
r^   c                       s4   e Zd ZdZedœ‡ fdd„Zddœdd„Z‡  ZS )ÚStatReloaderLoopÚstatr   c                    s   i | _ tƒ  ¡ S r   )ÚmtimesÚsuperre   rd   ©Ú	__class__r   r   re     s    zStatReloaderLoop.__enter__Nc              	   C   st   t | j| jƒD ]`}zt |¡j}W n tk
r:   Y qY nX | j |¡}|d kr\|| j|< q||kr|  	|¡ qd S r   )
r3   r$   r   r   rŠ   Úst_mtimeÚOSErrorr‹   Úgetr€   )ra   r   ÚmtimeZold_timer   r   r   rc     s    

zStatReloaderLoop.run_step)r�   r‚   rƒ   r   r^   re   rc   Ú__classcell__r   r   r�   r   r‰     s   r‰   c                       sp   e Zd Zejejddœ‡ fdd„Zeddœdd„Zedœ‡ fd	d
„Z	dd„ Z
ddœdd„Zddœdd„Z‡  ZS )ÚWatchdogReloaderLoopN)r\   Úkwargsr	   c                    s²   ddl m} ddlm} tƒ j||Ž | j‰ G ‡ fdd„d|ƒ}|j ¡ }| 	d¡r`|d d… }d|› d	�| _
|ƒ | _d
d„ | jD ƒ}|d|•dd„ tD ƒ| j•d�| _d| _d S )Nr   )ÚObserver)ÚPatternMatchingEventHandlerc                       s   e Zd Z‡ fdd„ZdS )z3WatchdogReloaderLoop.__init__.<locals>.EventHandlerc                    s   ˆ |j ƒ d S r   )Zsrc_path)ra   Úevent©r€   r   r   Úon_any_event9  s    z@WatchdogReloaderLoop.__init__.<locals>.EventHandler.on_any_eventN)r�   r‚   rƒ   rš   r   r™   r   r   ÚEventHandler8  s   r›   Úobserveriøÿÿÿz
watchdog (ú)c                 S   s   g | ]}t j |¡s|‘qS r   )r   r   Úisdir)r8   Úpr   r   r   Ú
<listcomp>H  s      z1WatchdogReloaderLoop.__init__.<locals>.<listcomp>ú*.pyú*.pycú*.zipc                 S   s   g | ]}d |› d�‘qS )z*/z/*r   )r8   Údr   r   r   r    L  s     )ÚpatternsÚignore_patternsF)r¡   r¢   r£   )Zwatchdog.observersr–   Zwatchdog.eventsr—   rŒ   rb   r€   r�   Úlowerr-   r   rœ   r$   r+   r   Úevent_handlerÚshould_reload)ra   r\   r•   r–   r—   r›   Zreloader_nameZextra_patternsr�   r™   r   rb   1  s$    

þþzWatchdogReloaderLoop.__init__rz   c                 C   s   d| _ |  |¡ d S )NT)r©   r}   r   r   r   r   r€   R  s    z#WatchdogReloaderLoop.trigger_reloadr   c                    s   i | _ | j ¡  tƒ  ¡ S r   )Úwatchesrœ   ÚstartrŒ   re   rd   r�   r   r   re   Y  s    
zWatchdogReloaderLoop.__enter__c                 C   s   | j  ¡  | j  ¡  d S r   )rœ   Ústopr.   rf   r   r   r   rj   ^  s    
zWatchdogReloaderLoop.__exit__c                 C   s*   | j s|  ¡  t | j¡ q t d¡ d S r|   )r©   rc   rk   rl   r_   r   r~   rd   r   r   r   rm   b  s    zWatchdogReloaderLoop.runc              	   C   s    t | jƒ}t| j| jƒD ]V}|| jkrdz| jj| j|dd�| j|< W n tk
rb   d | j|< Y nX | 	|¡ q|D ]&}| j 
|d ¡}|d k	rt| j |¡ qtd S )NT)Ú	recursive)r%   rª   r5   r$   r   rœ   Zscheduler¨   r�   ÚdiscardrW   Z
unschedule)ra   Z	to_deleter   Úwatchr   r   r   rc   i  s     

  ÿzWatchdogReloaderLoop.run_step)r�   r‚   rƒ   rG   ÚAnyrb   rI   r€   r^   re   rj   rm   rc   r“   r   r   r�   r   r”   0  s   !r”   )rŠ   ÚwatchdogÚreloader_loopszwatchdog.observersrŠ   Úautor±   c                  C   s~   t jdkst j ¡ sdS zddl} W n tk
r:   Y dS X |  t j¡}|d | j@ sz|d  | jO  < |  t j| j|¡ dS )zuEnsure that echo mode is enabled. Some tools such as PDB disable
    it which causes usability issues after a reload.Nr   rs   )	r   ÚstdinÚisattyÚtermiosÚImportErrorÚ	tcgetattrÚECHOÚ	tcsetattrÚTCSANOW)r¶   Ú
attributesr   r   r   Úensure_echo_onŽ  s    r½   )Ú	main_funcr$   r   r_   Úreloader_typer	   c              	   C   s¤   ddl }|  |jdd„ ¡ t| |||d�}z^tj d¡dkrztƒ  tj| dd	�}d
|_	|� | 
¡  | ¡  W 5 Q R X nt | ¡ ¡ W n tk
rž   Y nX dS )z<Run the given function in an independent Python interpreter.r   Nc                  W   s
   t  d¡S )Nr   )r   r~   )r\   r   r   r   Ú<lambda>«  ó    z#run_with_reloader.<locals>.<lambda>)r$   r   r_   rp   ro   r   )Útargetr\   T)ÚsignalÚSIGTERMr²   r   rt   r‘   r½   Ú	threadingÚThreadÚdaemonr«   rm   r   r~   ry   ÚKeyboardInterrupt)r¾   r$   r   r_   r¿   rÃ   ZreloaderrG   r   r   r   Úrun_with_reloader¡  s$      ÿrÉ   )NNr   r³   )4r    r   rv   r   rÅ   rk   ÚtypingrG   Ú	itertoolsr   Úpathlibr   Ú	_internalr   ÚtupleÚbase_prefixÚbase_exec_prefixr   r?   Úexec_prefixÚhasattrr'   r   r)   r+   ÚIteratorrI   r   ÚSetr#   r…   r3   r5   r4   ÚListr]   r^   r‰   r”   r²   ÚDictÚTypeÚ__annotations__Ú
__import__r·   r½   ÚCallabler„   r†   r‡   rˆ   rÉ   r   r   r   r   Ú<module>   sx    
ù þ7 þ6=Rþ    ûú