o
    h5                     @  s   d dl m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
 d dlmZ d dlmZ e
r:d dlmZmZ d	Zd	ZG d
d deZG dd dZG dd deZG dd deZG dd deZdS )    )annotationsN)defaultdict)Path)TYPE_CHECKING)
BaseThread)SkipRepeatsQueue)FileSystemEventFileSystemEventHandlerg      ?c                   @  s   e Zd ZdZdS )
EventQueuea.  Thread-safe event queue based on a special queue that skips adding
    the same event (:class:`FileSystemEvent`) multiple times consecutively.
    Thus avoiding dispatching multiple event handling
    calls when multiple identical events are produced quicker than an observer
    can consume them.
    N)__name__
__module____qualname____doc__ r   r   J/var/www/vscode/kcb/lib/python3.10/site-packages/watchdog/observers/api.pyr
      s    r
   c                   @  s   e Zd ZdZddd#d
dZed$ddZed%ddZed&ddZed'ddZ	d(ddZ
d(ddZd)dd Zd$d!d"ZdS )*ObservedWatchzAn scheduled watch.

    :param path:
        Path string.
    :param recursive:
        ``True`` if watch is recursive; ``False`` otherwise.
    :param event_filter:
        Optional collection of :class:`watchdog.events.FileSystemEvent` to watch
    N)event_filterpath
str | Path	recursiveboolr   "list[type[FileSystemEvent]] | Nonec                C  s>   t |tr	t|n|| _|| _|d urt|| _d S d | _d S N)
isinstancer   str_path_is_recursive	frozenset_event_filter)selfr   r   r   r   r   r   __init__(   s    zObservedWatch.__init__returnr   c                 C     | j S )z"The path that this watch monitors.)r   r   r   r   r   r   -      zObservedWatch.pathc                 C  r"   )z;Determines whether subdirectories are watched for the path.)r   r#   r   r   r   is_recursive2   r$   zObservedWatch.is_recursive'frozenset[type[FileSystemEvent]] | Nonec                 C  r"   )z.Collection of event types watched for the path)r   r#   r   r   r   r   7   r$   zObservedWatch.event_filter9tuple[str, bool, frozenset[type[FileSystemEvent]] | None]c                 C  s   | j | j| jfS r   )r   r%   r   r#   r   r   r   key<   s   zObservedWatch.keywatchobjectc                 C  s   t |tstS | j|jkS r   r   r   NotImplementedr(   r   r)   r   r   r   __eq__@      
zObservedWatch.__eq__c                 C  s   t |tstS | j|jkS r   r+   r-   r   r   r   __ne__E   r/   zObservedWatch.__ne__intc                 C  s
   t | jS r   )hashr(   r#   r   r   r   __hash__J   s   
zObservedWatch.__hash__c                 C  sZ   | j d urdtdd | j D }d| }nd}dt| j d| jd| j | d	S )
N|c                 s  s    | ]}|j V  qd S r   )r   ).0_clsr   r   r   	<genexpr>O   s    z)ObservedWatch.__repr__.<locals>.<genexpr>z, event_filter= <z: path=z, is_recursive=>)r   joinsortedtyper   r   r%   )r   event_filter_strr   r   r   __repr__M   s
   
&zObservedWatch.__repr__)r   r   r   r   r   r   )r!   r   )r!   r   )r!   r&   )r!   r'   )r)   r*   r!   r   )r!   r1   )r   r   r   r   r    propertyr   r%   r   r(   r.   r0   r3   r?   r   r   r   r   r      s    



r   c                      sd   e Zd ZdZeddd fddZedddZedddZdddZ	d ddZ
d!ddZ  ZS )"EventEmittera  Producer thread base class subclassed by event emitters
    that generate events and populate a queue with them.

    :param event_queue:
        The event queue to populate with generated events.
    :type event_queue:
        :class:`watchdog.events.EventQueue`
    :param watch:
        The watch to observe and produce events for.
    :type watch:
        :class:`ObservedWatch`
    :param timeout:
        Timeout (in seconds) between successive attempts at reading events.
    :type timeout:
        ``float``
    :param event_filter:
        Collection of event types to emit, or None for no filtering (default).
    :type event_filter:
        Iterable[:class:`watchdog.events.FileSystemEvent`] | None
    Ntimeoutr   event_queuer
   r)   r   rC   floatr   r   r!   Nonec                  s<   t    || _|| _|| _|d urt|| _d S d | _d S r   )superr    _event_queue_watch_timeoutr   r   )r   rD   r)   rC   r   	__class__r   r   r    m   s
   
 zEventEmitter.__init__c                 C  r"   )z$Blocking timeout for reading events.rJ   r#   r   r   r   rC   {   r$   zEventEmitter.timeoutc                 C  r"   )z'The watch associated with this emitter.)rI   r#   r   r   r   r)      r$   zEventEmitter.watcheventr   c                   s<   | j du st fdd| j D r| j | jf dS dS )zQueues a single event.

        :param event:
            Event to be queued.
        :type event:
            An instance of :class:`watchdog.events.FileSystemEvent`
            or a subclass.
        Nc                 3  s    | ]}t  |V  qd S r   )r   )r5   clsrN   r   r   r7      s    z+EventEmitter.queue_event.<locals>.<genexpr>)r   anyrH   putr)   )r   rN   r   rP   r   queue_event   s   "	zEventEmitter.queue_eventc                 C     dS )a  Override this method to populate the event queue with events
        per interval period.

        :param timeout:
            Timeout (in seconds) between successive attempts at
            reading events.
        :type timeout:
            ``float``
        Nr   r   rC   r   r   r   queue_events       zEventEmitter.queue_eventsc                 C  s$   |   r| | j |   sd S d S r   )should_keep_runningrV   rC   r#   r   r   r   run   s   zEventEmitter.run)
rD   r
   r)   r   rC   rE   r   r   r!   rF   r!   rE   )r!   r   )rN   r   r!   rF   rC   rE   r!   rF   r!   rF   )r   r   r   r   DEFAULT_EMITTER_TIMEOUTr    r@   rC   r)   rS   rV   rY   __classcell__r   r   rK   r   rA   W   s    

rA   c                      sj   e Zd ZdZe Z	 edd fddZedd	d
Z	dddZ
edddZdddZdddZ  ZS )EventDispatchera7  Consumer thread base class subclassed by event observer threads
    that dispatch events from an event queue to appropriate event handlers.

    :param timeout:
        Timeout value (in seconds) passed to emitters
        constructions in the child class BaseObserver.
    :type timeout:
        ``float``
    rC   rC   rE   r!   rF   c                  s   t    t | _|| _d S r   )rG   r    r
   rH   rJ   rU   rK   r   r   r       s   

zEventDispatcher.__init__c                 C  r"   )z)Timeout value to construct emitters with.rM   r#   r   r   r   rC      r$   zEventDispatcher.timeoutc                 C  sJ   t |  ttj | jtj	 W d    d S 1 sw   Y  d S r   )
r   stop
contextlibsuppressqueueFullrD   
put_nowaitr_   
stop_eventr#   r   r   r   ra      s   
"zEventDispatcher.stopr
   c                 C  r"   )zThe event queue which is populated with file system events
        by emitters and from which events are dispatched by a dispatcher
        thread.
        )rH   r#   r   r   r   rD      s   zEventDispatcher.event_queuerD   c                 C  rT   )ag  Override this method to consume events from an event queue, blocking
        on the queue for the specified timeout before raising :class:`queue.Empty`.

        :param event_queue:
            Event queue to populate with one set of events.
        :type event_queue:
            :class:`EventQueue`
        :raises:
            :class:`queue.Empty`
        Nr   )r   rD   r   r   r   dispatch_events   rW   zEventDispatcher.dispatch_eventsc                 C  s>   |   rz| | j W n
 tjy   Y q w |   sd S d S r   )rX   rh   rD   rd   Emptyr#   r   r   r   rY      s   zEventDispatcher.runr[   rZ   r\   )r!   r
   rD   r
   r!   rF   )r   r   r   r   r*   rg   DEFAULT_OBSERVER_TIMEOUTr    r@   rC   ra   rD   rh   rY   r^   r   r   rK   r   r_      s    


r_   c                      s   e Zd ZdZedd9 fd	d
Zd:ddZd:ddZd;ddZd<ddZ	d=ddZ
ed>ddZd; fddZd d!d"d?d)d*Zd<d+d,Zd<d-d.Zd=d/d0Zd;d1d2Zd;d3d4Zd@d7d8Z  ZS )ABaseObserverzBase observer.r`   emitter_classtype[EventEmitter]rC   rE   r!   rF   c                  sB   t  j|d || _t | _t | _tt| _	t | _
i | _d S )Nr`   )rG   r    _emitter_class	threadingRLock_lockset_watchesr   	_handlers	_emitters_emitter_for_watch)r   rm   rC   rK   r   r   r       s   


zBaseObserver.__init__emitterrA   c                 C  s   || j |j< | j| d S r   )rw   r)   rv   addr   rx   r   r   r   _add_emitter   s   zBaseObserver._add_emitterc                 C  sV   | j |j= | j| |  tt |  W d    d S 1 s$w   Y  d S r   )	rw   r)   rv   removera   rb   rc   RuntimeErrorr;   rz   r   r   r   _remove_emitter   s   

"zBaseObserver._remove_emitterc              	   C  sj   | j D ]}|  q| j D ]}tt |  W d    n1 s#w   Y  q| j   | j  d S r   )rv   ra   rb   rc   r}   r;   clearrw   rz   r   r   r   _clear_emitters   s   




zBaseObserver._clear_emittersevent_handlerr	   r)   r   c                 C  s   | j | | d S r   )ru   ry   r   r   r)   r   r   r   _add_handler_for_watch   s   z#BaseObserver._add_handler_for_watchc                 C  s   | j |= d S r   )ru   r-   r   r   r   _remove_handlers_for_watch      z'BaseObserver._remove_handlers_for_watchset[EventEmitter]c                 C  r"   )z/Returns event emitter created by this observer.)rv   r#   r   r   r   emitters   r$   zBaseObserver.emittersc              	     sD   | j  D ]}z|  W q ty   | |  w t   d S r   )rv   copystart	Exceptionr~   rG   rz   rK   r   r   r     s   
zBaseObserver.startFNr   r   r   r   r   r   r   r   c                C  s   | j 9 t|||d}| || || jvr.| j| j|| j|d}|  r)|  | 	| | j
| W d   |S 1 s?w   Y  |S )a6  Schedules watching a path and calls appropriate methods specified
        in the given event handler in response to file system events.

        :param event_handler:
            An event handler instance that has appropriate event handling
            methods which will be called by the observer in response to
            file system events.
        :type event_handler:
            :class:`watchdog.events.FileSystemEventHandler` or a subclass
        :param path:
            Directory path that will be monitored.
        :type path:
            ``str``
        :param recursive:
            ``True`` if events will be emitted for sub-directories
            traversed recursively; ``False`` otherwise.
        :type recursive:
            ``bool``
        :param event_filter:
            Collection of event types to emit, or None for no filtering (default).
        :type event_filter:
            Iterable[:class:`watchdog.events.FileSystemEvent`] | None
        :return:
            An :class:`ObservedWatch` object instance representing
            a watch.
        r   rB   N)rr   r   r   rw   ro   rD   rC   is_aliver   r{   rt   ry   )r   r   r   r   r   r)   rx   r   r   r   schedule  s   "


zBaseObserver.schedulec                 C  s8   | j  | || W d   dS 1 sw   Y  dS )a!  Adds a handler for the given watch.

        :param event_handler:
            An event handler instance that has appropriate event handling
            methods which will be called by the observer in response to
            file system events.
        :type event_handler:
            :class:`watchdog.events.FileSystemEventHandler` or a subclass
        :param watch:
            The watch to add a handler for.
        :type watch:
            An instance of :class:`ObservedWatch` or a subclass of
            :class:`ObservedWatch`
        N)rr   r   r   r   r   r   add_handler_for_watch=  s   "z"BaseObserver.add_handler_for_watchc                 C  s<   | j  | j| | W d   dS 1 sw   Y  dS )a'  Removes a handler for the given watch.

        :param event_handler:
            An event handler instance that has appropriate event handling
            methods which will be called by the observer in response to
            file system events.
        :type event_handler:
            :class:`watchdog.events.FileSystemEventHandler` or a subclass
        :param watch:
            The watch to remove a handler for.
        :type watch:
            An instance of :class:`ObservedWatch` or a subclass of
            :class:`ObservedWatch`
        N)rr   ru   r|   r   r   r   r   remove_handler_for_watchO  s   "z%BaseObserver.remove_handler_for_watchc                 C  sT   | j  | j| }| j|= | | | j| W d   dS 1 s#w   Y  dS )zUnschedules a watch.

        :param watch:
            The watch to unschedule.
        :type watch:
            An instance of :class:`ObservedWatch` or a subclass of
            :class:`ObservedWatch`
        N)rr   rw   ru   r~   rt   r|   )r   r)   rx   r   r   r   
unschedulea  s   	

"zBaseObserver.unschedulec                 C  sH   | j  | j  |   | j  W d   dS 1 sw   Y  dS )zCUnschedules all watches and detaches all associated event handlers.N)rr   ru   r   r   rt   r#   r   r   r   unschedule_allp  s
   
"zBaseObserver.unschedule_allc                 C  s   |    d S r   )r   r#   r   r   r   on_thread_stopw  r   zBaseObserver.on_thread_stoprD   r
   c                 C  s   |j dd}|tju rd S |\}}| j | j|  D ]}|| j| v r*|| qW d    n1 s5w   Y  |  d S )NT)block)getr_   rg   rr   ru   r   dispatch	task_done)r   rD   entryrN   r)   handlerr   r   r   rh   z  s   

zBaseObserver.dispatch_events)rm   rn   rC   rE   r!   rF   )rx   rA   r!   rF   r\   )r   r	   r)   r   r!   rF   )r)   r   r!   rF   )r!   r   )
r   r	   r   r   r   r   r   r   r!   r   rj   )r   r   r   r   rk   r    r{   r~   r   r   r   r@   r   r   r   r   r   r   r   r   rh   r^   r   r   rK   r   rl      s(    
	


	

/



rl   )
__future__r   rb   rd   rp   collectionsr   pathlibr   typingr   watchdog.utilsr   watchdog.utils.bricksr   watchdog.eventsr   r	   r]   rk   r
   r   rA   r_   rl   r   r   r   r   <module>   s$    	:J9