
    g)                    "   d Z 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	m
Z
mZmZmZmZmZmZmZmZmZmZmZmZ ddlmZmZmZmZ ddlmZ ddlmZ erdd	lm Z m!Z!  ejD                  e#      Z$ G d
 de      Z% G d de%      Z& G d de      Z'y)u  :module: watchdog.observers.inotify
:synopsis: ``inotify(7)`` based emitter implementation.
:author: Sebastien Martini <seb@dbzteam.org>
:author: Luke McCarthy <luke@iogopro.co.uk>
:author: yesudeep@google.com (Yesudeep Mangalapilly)
:author: Tim Cuthbertson <tim+github@gfxmonk.net>
:author: contact@tiger-222.fr (Mickaël Schoentgen)
:platforms: Linux 2.6.13+.

.. ADMONITION:: About system requirements

    Recommended minimum kernel version: 2.6.25.

    Quote from the inotify(7) man page:

        "Inotify was merged into the 2.6.13 Linux kernel. The required library
        interfaces were added to glibc in version 2.4. (IN_DONT_FOLLOW,
        IN_MASK_ADD, and IN_ONLYDIR were only added in version 2.5.)"

    Therefore, you must ensure the system is running at least these versions
    appropriate libraries and the kernel.

.. ADMONITION:: About recursiveness, event order, and event coalescing

    Quote from the inotify(7) man page:

        If successive output inotify events produced on the inotify file
        descriptor are identical (same wd, mask, cookie, and name) then they
        are coalesced into a single event if the older event has not yet been
        read (but see BUGS).

        The events returned by reading from an inotify file descriptor form
        an ordered queue. Thus, for example, it is guaranteed that when
        renaming from one directory to another, events will be produced in
        the correct order on the inotify file descriptor.

        ...

        Inotify monitoring of directories is not recursive: to monitor
        subdirectories under a directory, additional watches must be created.

    This emitter implementation therefore automatically adds watches for
    sub-directories if running in recursive mode.

Some extremely useful articles and documentation:

.. _inotify FAQ: http://inotify.aiken.cz/?section=inotify&page=faq&lang=en
.. _intro to inotify: http://www.linuxjournal.com/article/8478

    )annotationsN)TYPE_CHECKING)DirCreatedEventDirDeletedEventDirModifiedEventDirMovedEventFileClosedEventFileClosedNoWriteEventFileCreatedEventFileDeletedEventFileModifiedEventFileMovedEventFileOpenedEventFileSystemEventgenerate_sub_created_eventsgenerate_sub_moved_events)DEFAULT_EMITTER_TIMEOUTDEFAULT_OBSERVER_TIMEOUTBaseObserverEventEmitter)InotifyBuffer)InotifyConstants)
EventQueueObservedWatchc                  l     e Zd ZdZedd	 	 	 	 	 	 	 	 	 d fdZddZddZdddd	Zdd
Z	ddZ
 xZS )InotifyEmittera/  inotify(7)-based event emitter.

    :param event_queue:
        The event queue to fill with events.
    :param watch:
        A watch object representing the directory to monitor.
    :type watch:
        :class:`watchdog.observers.api.ObservedWatch`
    :param timeout:
        Read events blocking timeout (in seconds).
    :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timeoutevent_filterc               l    t         |   ||||       t        j                         | _        d | _        y )Nr   )super__init__	threadingLock_lock_inotify)selfevent_queuewatchr   r   	__class__s        O/var/www/openai/venv/lib/python3.12/site-packages/watchdog/observers/inotify.pyr"   zInotifyEmitter.__init__h   s0     	eW<X^^%
.2    c                    t        j                  | j                  j                        }| j	                         }t        || j                  j                  |      | _        y )N)	recursive
event_mask)osfsencoder)   pathget_event_mask_from_filterr   is_recursiver&   )r'   r2   r/   s      r+   on_thread_startzInotifyEmitter.on_thread_startt   sA    {{4::??+446
%ddjj6M6MZder,   c                `    | j                   r"| j                   j                          d | _         y y )N)r&   close)r'   s    r+   on_thread_stopzInotifyEmitter.on_thread_stopy   s%    ==MM! DM r,   Ffull_eventsc               `   | j                   t        j                  d       y | j                  5  | j                   t        j                  d       	 d d d        y | j                   j	                         }|
	 d d d        y t        |t              r'|\  }}| j                  |j                        }| j                  |j                        }|j                  rt        nt        }| j                   |||             | j                  t        t        j                  j!                  |                   | j                  t        t        j                  j!                  |                   |j                  r8| j"                  j$                  r"t'        ||      D ]  }	| j                  |	        	 d d d        y | j                  |j                        }|j(                  r|r1|j                  rt        nt        }| j                   |d|             n/|j                  rt*        nt,        }| j                   ||             | j                  t        t        j                  j!                  |                   |j                  r| j"                  j$                  rt/        |      D ]  }
| j                  |
        n|j0                  s|j2                  r1|j                  rt        nt4        }| j                   ||             n|j6                  s|j8                  rj|sh|j                  rt:        nt<        }| j                   ||             | j                  t        t        j                  j!                  |                   n|j8                  rk|ri|j                  rt        nt        }| j                   ||d             | j                  t        t        j                  j!                  |                   n|j>                  rh|j                  rt*        nt,        }| j                   ||             | j                  t        t        j                  j!                  |                   n%|j@                  rY|| j"                  j                  k(  r@|j                  rt:        nt<        }| j                   ||             | jC                          n|j                  s|jD                  rtF        }| j                   ||             n|jH                  rUtJ        }| j                   ||             | j                  t        t        j                  j!                  |                   n)|jL                  rtN        }| j                   ||             d d d        y # 1 sw Y   y xY w)Nz@InotifyEmitter.queue_events() called when the thread is inactive )(r&   loggererrorr%   
read_event
isinstancetuple_decode_pathsrc_pathis_directoryr   r   queue_eventr   r0   r2   dirnamer)   r4   r   is_moved_tor   r   r   	is_attrib	is_modifyr   	is_deleteis_moved_fromr   r   	is_createis_delete_selfstopis_openr   is_close_writer	   is_close_nowriter
   )r'   r   r:   event	move_frommove_torC   	dest_pathclssub_moved_eventsub_created_events              r+   queue_eventszInotifyEmitter.queue_events~   s    == LL[\ZZ}}$_` Z MM,,.E} Z %'%*"	7,,Y-?-?@ --g.>.>?	'0'='=m>  Xy!9:  !1"''//(2K!LM  !1"''//)2L!MN))djj.E.E+DXy+Y((9 ,Z) Z, ((8H  +0+=+=->C$$SX%67-2-?-?/EUC$$S]3  !1"''//(2K!LM%%$***A*A-H-R)(():; .SEOO*/*<*<&BS  X/U%8%8).););oAQ  X/  !1"''//(2K!LM$$','9'9m~  Xr!23  !1"''//(2K!LM).););oAQ  X/  !1"''//(2K!LM%%(djjoo*E).););oAQ  X/		''==)C$$S]3)))C$$S]3$$%5bggooh6O%PQ++0C$$S]3 ZZs   #V$V$D/V$9O"V$$V-c                x    t        | j                  j                  t              r|S t	        j
                  |      S )z>Decode path only if unicode string was passed to this emitter.)r@   r)   r2   bytesr0   fsdecode)r'   r2   s     r+   rB   zInotifyEmitter._decode_path   s'    !$**//59tPr{{4?PPr,   c                   | j                   yt        j                  }| j                   D ]_  }|t        t        hv r|t        j
                  z  }&|t        t        hv r%|t        j
                  t        j                  z  z  }Y|t        u rX|t        j
                  t        j                  z  t        j                  z  t        j                  z  t        j                  z  z  }|t        u r%|t        j                  t        j                  z  z  }|t        t        hv r|t        j                   z  }	|t"        u r|t        j                  z  }&|t$        u r|t        j&                  z  }C|t(        u sM|t        j*                  z  }b |S )zCOptimization: Only include events we are filtering in inotify call.N)_event_filterr   IN_DELETE_SELFr   r   IN_MOVEr   r   	IN_CREATEr   	IN_ATTRIB	IN_MODIFYIN_CLOSE_WRITEr   r   r   	IN_DELETEr	   r
   IN_CLOSE_NOWRITEr   IN_OPEN)r'   r/   rV   s      r+   r3   z)InotifyEmitter.get_event_mask_from_filter   sj   % &44
%%C}n55.666
*:;;.669I9S9SSS
(($,,&001&001 '001 '55	6
 )).88;K;U;UUU
*:;;.888
'.===
...???
'.666
- &0 r,   )
r(   r   r)   r   r   floatr   z"list[type[FileSystemEvent]] | NonereturnNone)ri   rj   )r   rh   r:   boolri   rj   )r2   bytes | strri   rl   )ri   z
int | None)__name__
__module____qualname____doc__r   r"   r5   r8   rY   rB   r3   __classcell__r*   s   @r+   r   r   U   si    . 1;?
3
3 
3
 
3 9
3 

3f
!
 CH E4NQ r,   r   c                  *     e Zd ZdZddd fdZ xZS )InotifyFullEmitterzinotify(7)-based event emitter. By default this class produces move events even if they are not matched
    Such move events will have a ``None`` value for the unmatched part.
    T)eventsc               (    t         |   ||       y )Nr9   )r!   rY   )r'   r   ru   r*   s      r+   rY   zInotifyFullEmitter.queue_events   s    W&9r,   )r   rh   ru   rk   ri   rj   )rm   rn   ro   rp   rY   rq   rr   s   @r+   rt   rt      s     >B : :r,   rt   c                  ,     e Zd ZdZeddd fdZ xZS )InotifyObserverzdObserver thread that schedules watching directories and dispatches
    calls to event handlers.
    F)r   generate_full_eventsc               D    |rt         nt        }t        |   ||       y )N)r   )rt   r   r!   r"   )r'   r   ry   rV   r*   s       r+   r"   zInotifyObserver.__init__   s    $8 ng.r,   )r   rh   ry   rk   ri   rj   )rm   rn   ro   rp   r   r"   rq   rr   s   @r+   rx   rx      s     ,Dbg / /r,   rx   )(rp   
__future__r   loggingr0   r#   typingr   watchdog.eventsr   r   r   r   r	   r
   r   r   r   r   r   r   r   r   watchdog.observers.apir   r   r   r   !watchdog.observers.inotify_bufferr   watchdog.observers.inotify_cr   r   r   	getLoggerrm   r=   r   rt   rx    r,   r+   <module>r      s~   1f #  	        q p ; 9@			8	$T\ Tn: :/l /r,   