
    $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dS )    )annotationsN)defaultdict)Path)TYPE_CHECKING)
BaseThread)SkipRepeatsQueue)FileSystemEventFileSystemEventHandlerg      ?c                      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__     V/var/www/html/netbox-4.1.3/venv/lib/python3.11/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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                   t          |t                    rt          |          n|| _        || _        |t          |          nd | _        d S N)
isinstancer   str_path_is_recursive	frozenset_event_filter)selfr   r   r   s       r   __init__zObservedWatch.__init__(   sK    ",T4"8"8BSYYYd
&8D8PY|444VZr   returnr   c                    | j         S )z"The path that this watch monitors.)r    r$   s    r   r   zObservedWatch.path-   s     zr   c                    | j         S )z;Determines whether subdirectories are watched for the path.)r!   r(   s    r   is_recursivezObservedWatch.is_recursive2        !!r   'frozenset[type[FileSystemEvent]] | Nonec                    | j         S )z.Collection of event types watched for the path)r#   r(   s    r   r   zObservedWatch.event_filter7   r+   r   9tuple[str, bool, frozenset[type[FileSystemEvent]] | None]c                *    | j         | j        | j        fS r   )r   r*   r   r(   s    r   keyzObservedWatch.key<   s    y$+T->>>r   watchobjectc                Z    t          |t                    st          S | j        |j        k    S r   r   r   NotImplementedr0   r$   r1   s     r   __eq__zObservedWatch.__eq__@   )    %// 	"!!x59$$r   c                Z    t          |t                    st          S | j        |j        k    S r   r4   r6   s     r   __ne__zObservedWatch.__ne__E   r8   r   intc                *    t          | j                  S r   )hashr0   r(   s    r   __hash__zObservedWatch.__hash__J   s    DH~~r   c                    | j         7d                    t          d | j         D                                 }d| }nd}dt          |           j         d| j        d| j         | dS )	N|c              3  $   K   | ]}|j         V  d S r   )r   ).0_clss     r   	<genexpr>z)ObservedWatch.__repr__.<locals>.<genexpr>O   s$      .[.[t}.[.[.[.[.[.[r   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.[.[IZ.[.[.[([([\\C1ACC!q4::&qqtyqq4K\q^nqqqqr   )r   r   r   r   r   r   )r&   r   )r&   r   )r&   r,   )r&   r.   )r1   r2   r&   r   )r&   r;   )r   r   r   r   r%   propertyr   r*   r   r0   r7   r:   r>   rL   r   r   r   r   r      s         qu [ [ [ [ [ [
    X " " " X" " " " X" ? ? ? X?% % % %
% % % %
   r r r r r rr   r   c                  t     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   event_queuer   r1   r   rQ   floatr   r   r&   Nonec                   t                                                       || _        || _        || _        |t          |          nd | _        d S r   )superr%   _event_queue_watch_timeoutr"   r#   )r$   rR   r1   rQ   r   	__class__s        r   r%   zEventEmitter.__init__m   sR     	'8D8PY|444VZr   c                    | j         S )z$Blocking timeout for reading events.rY   r(   s    r   rQ   zEventEmitter.timeout{        }r   c                    | j         S )z'The watch associated with this emitter.)rX   r(   s    r   r1   zEventEmitter.watch   s     {r   eventr	   c                    | j          t          f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  8   K   | ]}t          |          V  d S r   )r   )rB   clsr_   s     r   rD   z+EventEmitter.queue_event.<locals>.<genexpr>   s-      ,b,bZs-C-C,b,b,b,b,b,br   )r#   anyrW   putr1   )r$   r_   s    `r   queue_eventzEventEmitter.queue_event   s_     %,b,b,b,btOa,b,b,b)b)b%!!5$*"566666 &%r   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$   rQ   s     r   queue_eventszEventEmitter.queue_events         r   c                    |                                  r0|                     | j                   |                                  .d S d S r   )should_keep_runningrg   rQ   r(   s    r   runzEventEmitter.run   sV    &&(( 	,dl+++ &&(( 	, 	, 	, 	, 	,r   )
rR   r   r1   r   rQ   rS   r   r   r&   rT   r&   rS   )r&   r   )r_   r	   r&   rT   rQ   rS   r&   rT   r&   rT   )r   r   r   r   DEFAULT_EMITTER_TIMEOUTr%   rM   rQ   r1   re   rg   rk   __classcell__rZ   s   @r   rO   rO   W   s         4 1;?[ [ [ [ [ [ [ [    X    X
7 
7 
7 
7	 	 	 	, , , , , , , ,r   rO   c                       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``
    rQ   rQ   rS   r&   rT   c               |    t                                                       t                      | _        || _        d S r   )rV   r%   r   rW   rY   )r$   rQ   rZ   s     r   r%   zEventDispatcher.__init__   s0    &LLr   c                    | j         S )z)Timeout value to construct emitters with.r\   r(   s    r   rQ   zEventDispatcher.timeout   r]   r   c                    t          j        |            t          j        t          j                  5  | j                            t          j	                   d d d            d S # 1 swxY w Y   d S r   )
r   stop
contextlibsuppressqueueFullrR   
put_nowaitrs   
stop_eventr(   s    r   rx   zEventDispatcher.stop   s     ,, 	D 	D''(BCCC	D 	D 	D 	D 	D 	D 	D 	D 	D 	D 	D 	D 	D 	D 	D 	D 	D 	Ds   %A%%A),A)r   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.
        )rW   r(   s    r   rR   zEventDispatcher.event_queue   s       r   rR   c                    dS )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$   rR   s     r   dispatch_eventszEventDispatcher.dispatch_events   rh   r   c                    |                                  rG	 |                     | j                   n# t          j        $ r Y Aw xY w|                                  Ed S d S r   )rj   r   rR   r{   Emptyr(   s    r   rk   zEventDispatcher.run   s    &&(( 	$$T%56666;    &&(( 	 	 	 	 	s   1 AArm   rl   rn   )r&   r   rR   r   r&   rT   )r   r   r   r   r2   r~   DEFAULT_OBSERVER_TIMEOUTr%   rM   rQ   rx   rR   r   rk   rp   rq   s   @r   rs   rs      s          JC+C                
    XD D D D
 ! ! ! X!
 
 
 
       r   rs   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d0d!Zd-d"Zd-d#Zd.d$Zd,d%Zd,d&Zd1d)Z xZS )2BaseObserverzBase observer.rt   emitter_classtype[EventEmitter]rQ   rS   r&   rT   c                  t                                          |           || _        t          j                    | _        t                      | _        t          t                    | _	        t                      | _
        i | _        d S )Nrt   )rV   r%   _emitter_class	threadingRLock_lockset_watchesr   	_handlers	_emitters_emitter_for_watch)r$   r   rQ   rZ   s      r   r%   zBaseObserver.__init__   si    )))+_&&
,/EER]^aRbRb,/EEEGr   emitterrO   c                X    || j         |j        <   | j                            |           d S r   )r   r1   r   addr$   r   s     r   _add_emitterzBaseObserver._add_emitter   s-    18.7#####r   c                   | j         |j        = | j                            |           |                                 t          j        t                    5  |                                 d d d            d S # 1 swxY w Y   d S r   )	r   r1   r   removerx   ry   rz   RuntimeErrorrH   r   s     r   _remove_emitterzBaseObserver._remove_emitter   s    #GM2g&&& .. 	 	LLNNN	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   A77A;>A;c                D   | j         D ]}|                                 | j         D ]G}t          j        t                    5  |                                 d d d            n# 1 swxY w Y   H| j                                          | j                                         d S r   )r   rx   ry   rz   r   rH   clearr   r   s     r   _clear_emitterszBaseObserver._clear_emitters   s    ~ 	 	GLLNNNN~ 	 	G$\22                %%'''''s   A""A&	)A&	event_handlerr
   r1   r   c                F    | j         |                             |           d S r   )r   r   r$   r   r1   s      r   _add_handler_for_watchz#BaseObserver._add_handler_for_watch   s#    u!!-00000r   c                    | j         |= d S r   )r   r6   s     r   _remove_handlers_for_watchz'BaseObserver._remove_handlers_for_watch   s    N5!!!r   set[EventEmitter]c                    | j         S )z/Returns event emitter created by this observer.)r   r(   s    r   emitterszBaseObserver.emitters   s     ~r   c                    | j                                         D ];}	 |                                 # t          $ r |                     |            w xY wt                                                       d S r   )r   copystart	Exceptionr   rV   )r$   r   rZ   s     r   r   zBaseObserver.start  s}    ~**,, 	 	G   $$W--- 	s	   3!AFNr   r   r   r   r   r   r   r   c                  | j         5  t          |||          }|                     ||           || j        vr`|                     | j        || j        |          }|                                 r|                                 | 	                    |           | j
                            |           ddd           n# 1 swxY 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   rP   N)r   r   r   r   r   rR   rQ   is_aliver   r   r   r   )r$   r   r   r   r   r1   r   s          r   schedulezBaseObserver.schedule  s   D Z 
	% 
	%!$),WWWE''u=== D333--d.>t|jv-ww==?? $MMOOO!!'***Me$$$
	% 
	% 
	% 
	% 
	% 
	% 
	% 
	% 
	% 
	% 
	% 
	% 
	% 
	% 
	% s   B,C  CCc                r    | j         5  |                     ||           ddd           dS # 1 swxY 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)r   r   r   s      r   add_handler_for_watchz"BaseObserver.add_handler_for_watch=  s     Z 	> 	>''u===	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	>s   ,00c                    | j         5  | j        |                             |           ddd           dS # 1 swxY 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)r   r   r   r   s      r   remove_handler_for_watchz%BaseObserver.remove_handler_for_watchO  s     Z 	8 	8N5!((777	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8s   !6::c                    | j         5  | j        |         }| j        |= |                     |           | j                            |           ddd           dS # 1 swxY 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)r   r   r   r   r   r   )r$   r1   r   s      r   
unschedulezBaseObserver.unschedulea  s     Z 	( 	(-e4Gu%  )))M  '''		( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	(s   AAA!Ac                    | j         5  | j                                         |                                  | j                                         ddd           dS # 1 swxY w Y   dS )zCUnschedules all watches and detaches all associated event handlers.N)r   r   r   r   r   r(   s    r   unschedule_allzBaseObserver.unschedule_allp  s    Z 	" 	"N  """  """M!!!	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	"s   AAA #A c                .    |                                   d S r   )r   r(   s    r   on_thread_stopzBaseObserver.on_thread_stopw  s    r   rR   r   c                P   |                     d          }|t          j        u rd S |\  }}| j        5  | j        |                                         D ]&}|| j        |         v r|                    |           '	 d d d            n# 1 swxY w Y   |                                 d S )NT)block)getrs   r~   r   r   r   dispatch	task_done)r$   rR   entryr_   r1   handlers         r   r   zBaseObserver.dispatch_eventsz  s    d++O...FuZ 	, 	,  >%05577 , ,dnU333$$U+++,		, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	s   ABBB)r   r   rQ   rS   r&   rT   )r   rO   r&   rT   rn   )r   r
   r1   r   r&   rT   )r1   r   r&   rT   )r&   r   )
r   r
   r   r   r   r   r   r   r&   r   r   )r   r   r   r   r   r%   r   r   r   r   r   rM   r   r   r   r   r   r   r   r   r   rp   rq   s   @r   r   r      s       Nf H H H H H H H H$ $ $ $   ( ( ( (1 1 1 1" " " "    X       ;?- - - - - -^> > > >$8 8 8 8$( ( ( (" " " "                  r   r   )
__future__r   ry   r{   r   collectionsr   pathlibr   typingr   watchdog.utilsr   watchdog.utils.bricksr   watchdog.eventsr	   r
   ro   r   r   r   rO   rs   r   r   r   r   <module>r      s   " " " " " "          # # # # # #                   % % % % % % 2 2 2 2 2 2 HGGGGGGGG      !   6r 6r 6r 6r 6r 6r 6r 6rtG, G, G, G, G,: G, G, G,T6 6 6 6 6j 6 6 6rn  n  n  n  n ? n  n  n  n  n r   