
    g5                        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e      Z G d d      Z G d de      Z G d de      Z G d de      Zy)    )annotationsN)defaultdict)Path)TYPE_CHECKING)
BaseThread)SkipRepeatsQueue)FileSystemEventFileSystemEventHandlerg      ?c                      e Zd ZdZy)
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__     K/var/www/openai/venv/lib/python3.12/site-packages/watchdog/observers/api.pyr   r      s    r   r   c                      e Zd ZdZddddZedd       Zedd       Zedd       Zedd       Z	dd	Z
dd
ZddZddZy)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_filterc                   t        |t              rt        |      n|| _        || _        |t        |      | _        y d | _        y N)
isinstancer   str_path_is_recursive	frozenset_event_filter)selfpath	recursiver   s       r   __init__zObservedWatch.__init__(   s:    ",T4"8SYd
&8D8PY|4VZr   c                    | j                   S )z"The path that this watch monitors.)r   r   s    r   r    zObservedWatch.path-   s     zzr   c                    | j                   S )z;Determines whether subdirectories are watched for the path.)r   r$   s    r   is_recursivezObservedWatch.is_recursive2        !!!r   c                    | j                   S )z.Collection of event types watched for the path)r   r$   s    r   r   zObservedWatch.event_filter7   r'   r   c                H    | j                   | j                  | j                  fS r   )r    r&   r   r$   s    r   keyzObservedWatch.key<   s    yy$++T->->>>r   c                `    t        |t              st        S | j                  |j                  k(  S r   r   r   NotImplementedr*   r   watchs     r   __eq__zObservedWatch.__eq__@   %    %/!!xx599$$r   c                `    t        |t              st        S | j                  |j                  k7  S r   r,   r.   s     r   __ne__zObservedWatch.__ne__E   r1   r   c                ,    t        | j                        S r   )hashr*   r$   s    r   __hash__zObservedWatch.__hash__J   s    DHH~r   c                    | j                   1dj                  t        d | j                   D                    }d| }nd}dt        |       j                   d| j
                  d| j                   | dS )	N|c              3  4   K   | ]  }|j                     y wr   )r   ).0_clss     r   	<genexpr>z)ObservedWatch.__repr__.<locals>.<genexpr>O   s     .[IZt}}IZs   z, event_filter= <z: path=z, is_recursive=>)r   joinsortedtyper   r    r&   )r   event_filter_strs     r   __repr__zObservedWatch.__repr__M   s{    ("xx.[IZIZ.[([\!01A0BC!4:&&'wtyym?4K\K\J]^n]oopqqr   )r    z
str | Pathr!   boolr   "list[type[FileSystemEvent]] | None)returnr   )rG   rE   )rG   z'frozenset[type[FileSystemEvent]] | None)rG   z9tuple[str, bool, frozenset[type[FileSystemEvent]] | None])r/   objectrG   rE   )rG   int)r   r   r   r   r"   propertyr    r&   r   r*   r0   r3   r6   rD   r   r   r   r   r      sy     qu [
   " " " " ? ?%
%
rr   r   c                  z     e Zd ZdZedd	 	 	 	 	 	 	 	 	 d
 fdZedd       Zedd       ZddZ	ddZ
dd	Z x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   c                   t         |           || _        || _        || _        |t        |      | _        y d | _        y r   )superr"   _event_queue_watch_timeoutr   r   )r   event_queuer/   rN   r   	__class__s        r   r"   zEventEmitter.__init__m   s@     	'8D8PY|4VZr   c                    | j                   S )z$Blocking timeout for reading events.rS   r$   s    r   rN   zEventEmitter.timeout{        }}r   c                    | j                   S )z'The watch associated with this emitter.)rR   r$   s    r   r/   zEventEmitter.watch   s     {{r   c                    | j                   t        fd| j                   D              r(| j                  j                  | j                  f       yy)zQueues a single event.

        :param event:
            Event to be queued.
        :type event:
            An instance of :class:`watchdog.events.FileSystemEvent`
            or a subclass.
        Nc              3  6   K   | ]  }t        |        y wr   )r   )r:   clsevents     r   r<   z+EventEmitter.queue_event.<locals>.<genexpr>   s     ,bOaZs-COas   )r   anyrQ   putr/   )r   r]   s    `r   queue_eventzEventEmitter.queue_event   sG     %,btOaOa,b)b!!5$**"56 *cr   c                     y)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   rN   s     r   queue_eventszEventEmitter.queue_events       r   c                ~    | j                         r-| j                  | j                         | j                         r,y y r   )should_keep_runningrb   rN   r$   s    r   runzEventEmitter.run   s.    &&(dll+ &&(r   )
rT   r   r/   r   rN   floatr   rF   rG   NonerG   rg   )rG   r   )r]   r	   rG   rh   rN   rg   rG   rh   rG   rh   )r   r   r   r   DEFAULT_EMITTER_TIMEOUTr"   rJ   rN   r/   r`   rb   rf   __classcell__rU   s   @r   rL   rL   W   s    4 1;?[[ [
 [ 9[ 
[    
7	,r   rL   c                  v     e Zd ZdZ e       Z	 edd	 fdZed
d       Z	ddZ
edd       ZddZddZ x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``
    rN   c               N    t         |           t               | _        || _        y r   )rP   r"   r   rQ   rS   )r   rN   rU   s     r   r"   zEventDispatcher.__init__   s    &Lr   c                    | j                   S )z)Timeout value to construct emitters with.rW   r$   s    r   rN   zEventDispatcher.timeout   rX   r   c                    t        j                  |        t        j                  t        j
                        5  | j                  j                  t        j                         d d d        y # 1 sw Y   y xY wr   )
r   stop
contextlibsuppressqueueFullrT   
put_nowaitrp   
stop_eventr$   s    r   ru   zEventDispatcher.stop   sF      ,''(B(BC -,,s   *A,,A5c                    | j                   S )zThe event queue which is populated with file system events
        by emitters and from which events are dispatched by a dispatcher
        thread.
        )rQ   r$   s    r   rT   zEventDispatcher.event_queue   s        r   c                     y)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   rT   s     r   dispatch_eventszEventDispatcher.dispatch_events   rc   r   c                    | j                         r.	 | j                  | j                         | j                         r-y y # t        j                  $ r Y Tw xY wr   )re   r~   rT   rx   Emptyr$   s    r   rf   zEventDispatcher.run   sM    &&($$T%5%56 &&( ;; s   A   AArj   ri   rk   )rG   r   rT   r   rG   rh   )r   r   r   r   rH   r{   DEFAULT_OBSERVER_TIMEOUTr"   rJ   rN   ru   rT   r~   rf   rm   rn   s   @r   rp   rp      sT     JC+C  
  D
 ! !
r   rp   c                       e Zd ZdZedd fdZddZddZddZddZ	ddZ
edd	       Zd fd
Zddd	 	 	 	 	 	 	 	 	 ddZddZddZddZddZddZddZ xZS )BaseObserverzBase observer.rq   c                   t         |   |       || _        t        j                         | _        t               | _        t        t              | _	        t               | _
        i | _        y )Nrq   )rP   r"   _emitter_class	threadingRLock_lockset_watchesr   	_handlers	_emitters_emitter_for_watch)r   emitter_classrN   rU   s      r   r"   zBaseObserver.__init__   sP    )+__&
,/ER]^aRb,/EEGr   c                l    || j                   |j                  <   | j                  j                  |       y r   )r   r/   r   addr   emitters     r   _add_emitterzBaseObserver._add_emitter   s(    18.7#r   c                   | j                   |j                  = | j                  j                  |       |j	                          t        j                  t              5  |j                          d d d        y # 1 sw Y   y xY wr   )	r   r/   r   removeru   rv   rw   RuntimeErrorr@   r   s     r   _remove_emitterzBaseObserver._remove_emitter   sR    ##GMM2g&  .LLN /..s   A66A?c                L   | j                   D ]  }|j                           | j                   D ]4  }t        j                  t              5  |j                          d d d        6 | j                   j                          | j                  j                          y # 1 sw Y   uxY wr   )r   ru   rv   rw   r   r@   clearr   r   s     r   _clear_emitterszBaseObserver._clear_emitters   ss    ~~GLLN &~~G$$\2 32 & 	%%' 32s   
BB#	c                @    | j                   |   j                  |       y r   )r   r   r   event_handlerr/   s      r   _add_handler_for_watchz#BaseObserver._add_handler_for_watch   s    u!!-0r   c                    | j                   |= y r   )r   r.   s     r   _remove_handlers_for_watchz'BaseObserver._remove_handlers_for_watch   s    NN5!r   c                    | j                   S )z/Returns event emitter created by this observer.)r   r$   s    r   emitterszBaseObserver.emitters   s     ~~r   c                    | j                   j                         D ]  }	 |j                           t
        | 	          y # t        $ r | j	                  |        w xY wr   )r   copystart	Exceptionr   rP   )r   r   rU   s     r   r   zBaseObserver.start  sU    ~~**,G - 	  $$W-s   AAFNr!   r   c                  | j                   5  t        |||      }| j                  ||       || j                  vrZ| j	                  | j
                  || j                  |      }| j                         r|j                          | j                  |       | j                  j                  |       ddd       |S # 1 sw Y   S xY w)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   rM   N)r   r   r   r   r   rT   rN   is_aliver   r   r   r   )r   r   r    r!   r   r/   r   s          r   schedulezBaseObserver.schedule  s    D ZZ!$),WE''u= D333--d.>.>t||jv-w==?MMO!!'*MMe$    s   B$B;;Cc                j    | j                   5  | j                  ||       ddd       y# 1 sw Y   yxY w)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)r   r   r   s      r   add_handler_for_watchz"BaseObserver.add_handler_for_watch=  s%     ZZ''u= ZZs   )2c                    | j                   5  | j                  |   j                  |       ddd       y# 1 sw Y   yxY w)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)r   r   r   r   s      r   remove_handler_for_watchz%BaseObserver.remove_handler_for_watchO  s,     ZZNN5!((7 ZZs   5>c                    | j                   5  | j                  |   }| j                  |= | j                  |       | j                  j                  |       ddd       y# 1 sw Y   yxY w)zUnschedules a watch.

        :param watch:
            The watch to unschedule.
        :type watch:
            An instance of :class:`ObservedWatch` or a subclass of
            :class:`ObservedWatch`
        N)r   r   r   r   r   r   )r   r/   r   s      r   
unschedulezBaseObserver.unschedulea  sQ     ZZ--e4Gu%  )MM  '	 ZZs   A	AA(c                    | j                   5  | j                  j                          | j                          | j                  j                          ddd       y# 1 sw Y   yxY w)zCUnschedules all watches and detaches all associated event handlers.N)r   r   r   r   r   r$   s    r   unschedule_allzBaseObserver.unschedule_allp  s?    ZZNN  "  "MM! ZZs   AAA$c                $    | j                          y r   )r   r$   s    r   on_thread_stopzBaseObserver.on_thread_stopw  s    r   c                F   |j                  d      }|t        j                  u ry |\  }}| j                  5  | j                  |   j                         D ]%  }|| j                  |   v s|j                  |       ' 	 d d d        |j                          y # 1 sw Y   xY w)NT)block)getrp   r{   r   r   r   dispatch	task_done)r   rT   entryr]   r/   handlers         r   r~   zBaseObserver.dispatch_eventsz  s    d+O...uZZ  >>%0557dnnU33$$U+ 8	  	 Zs   2B*BB )r   ztype[EventEmitter]rN   rg   rG   rh   )r   rL   rG   rh   rk   )r   r
   r/   r   rG   rh   )r/   r   rG   rh   )rG   zset[EventEmitter])
r   r
   r    r   r!   rE   r   rF   rG   r   r   )r   r   r   r   r   r"   r   r   r   r   r   rJ   r   r   r   r   r   r   r   r   r~   rm   rn   s   @r   r   r      s    Nf H$(1"    ;?--- -
 - 9- 
-^>$8$(" r   r   )
__future__r   rv   rx   r   collectionsr   pathlibr   typingr   watchdog.utilsr   watchdog.utils.bricksr   watchdog.eventsr	   r
   rl   r   r   r   rL   rp   r   r   r   r   <module>r      sv    "    #    % 2G  ! 6r 6rtG,: G,T6j 6rn ? n r   