U
    ꥡc"                     @   s   d 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	 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 G dd deZG d	d
 d
eZG dd deZdS )a~  
: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>
: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

    N   )InotifyBuffer)EventEmitterBaseObserverDEFAULT_EMITTER_TIMEOUTDEFAULT_OBSERVER_TIMEOUT)DirDeletedEventDirModifiedEventDirMovedEventDirCreatedEventFileDeletedEventFileModifiedEventFileMovedEventFileCreatedEventFileClosedEventgenerate_sub_moved_eventsgenerate_sub_created_eventsc                       sF   e Zd ZdZef fdd	Zdd Zdd Zdd	d
Zdd Z	  Z
S )InotifyEmitterar  
    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``
    c                    s$   t  ||| t | _d | _d S N)super__init__	threadingLock_lock_inotifyselfZevent_queuewatchtimeout	__class__ >/tmp/pip-unpacked-wheel-thtqfo2i/watchdog/observers/inotify.pyr   o   s    
zInotifyEmitter.__init__c                 C   s"   t | jj}t|| jj| _d S r   )osfsencoder   pathr   is_recursiver   r   r%   r!   r!   r"   on_thread_startt   s    zInotifyEmitter.on_thread_startc                 C   s   | j r| j   d | _ d S r   )r   close)r   r!   r!   r"   on_thread_stopx   s    
zInotifyEmitter.on_thread_stopFc           
   	   C   s  | j  | j }|d kr*W 5 Q R  d S t|tr|\}}| |j}| |j}|jr^tnt	}| 
||| | 
ttj| | 
ttj| |jr| jjrt||D ]}	| 
|	 qW 5 Q R  d S | |j}|jrr|r|jrtnt	}| 
|d | n|jrtnt}| 
|| | 
ttj| |jr| jjrt|D ]}	| 
|	 q\n|jr|jrtnt}| 
|| np|jr|jrtnt}| 
|| nF|js|jr|s|jrtnt}| 
|| | 
ttj| n|jrX|rX|jr,tnt	}| 
||d  | 
ttj| n|jr|jrltnt}| 
|| | 
ttj| nv|jr|jst}| 
|| | 
ttj| n<|j r|| jjkr|jrtnt}| 
|| | !  W 5 Q R X d S r   )"r   r   Z
read_event
isinstancetuple_decode_pathsrc_pathZis_directoryr
   r   Zqueue_eventr	   r#   r%   dirnamer   r&   r   Zis_moved_tor   r   r   Z	is_attribr   Z	is_modifyZ	is_deleteZis_moved_fromr   r   Z	is_createZis_close_writer   Zis_delete_selfstop)
r   r   full_eventseventZ	move_fromZmove_tor.   	dest_pathclsZ	sub_eventr!   r!   r"   queue_events}   sj    


zInotifyEmitter.queue_eventsc                 C   s   t | jjtr|S t|S )z?Decode path only if unicode string was passed to this emitter. )r+   r   r%   bytesr#   fsdecoder'   r!   r!   r"   r-      s    zInotifyEmitter._decode_path)F)__name__
__module____qualname____doc__r   r   r(   r*   r5   r-   __classcell__r!   r!   r   r"   r   _   s   
>r   c                       s.   e Zd ZdZef fdd	ZdddZ  ZS )InotifyFullEmittera  
    inotify(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.

    :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``
    c                    s   t  ||| d S r   )r   r   r   r   r!   r"   r      s    zInotifyFullEmitter.__init__Tc                 C   s   t j| ||d d S )N)r1   )r   r5   )r   r   eventsr!   r!   r"   r5      s    zInotifyFullEmitter.queue_events)T)r8   r9   r:   r;   r   r   r5   r<   r!   r!   r   r"   r=      s   r=   c                       s&   e Zd ZdZedf fdd	Z  ZS )InotifyObserverzi
    Observer thread that schedules watching directories and dispatches
    calls to event handlers.
    Fc                    s    |rt nt}t j||d d S )N)Zemitter_classr   )r=   r   r   r   )r   r   Zgenerate_full_eventsr4   r   r!   r"   r      s    zInotifyObserver.__init__)r8   r9   r:   r;   r   r   r<   r!   r!   r   r"   r?      s   r?   )r;   r#   r   Zinotify_bufferr   Zwatchdog.observers.apir   r   r   r   Zwatchdog.eventsr   r	   r
   r   r   r   r   r   r   r   r   r   r=   r?   r!   r!   r!   r"   <module>   s   34c