o
    *Î®c‘  ã                   @   sJ   d dl Z d dlmZ d dlmZ d dlmZ e  e¡Z	G dd„ deƒZ
dS )é    N)Ú
BaseThread)ÚDelayedQueue)ÚInotifyc                       sN   e Zd ZdZdZd‡ fdd„	Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
‡  ZS )ÚInotifyBufferzŠA wrapper for `Inotify` that holds events for `delay` seconds. During
    this time, IN_MOVED_FROM and IN_MOVED_TO events are paired.
    g      à?Fc                    s.   t ƒ  ¡  t| jƒ| _t||ƒ| _|  ¡  d S ©N)ÚsuperÚ__init__r   ÚdelayÚ_queuer   Ú_inotifyÚstart)ÚselfÚpathÚ	recursive©Ú	__class__© úH/tmp/pip-target-vg8gfxp4/lib/python/watchdog/observers/inotify_buffer.pyr       s   
zInotifyBuffer.__init__c                 C   s
   | j  ¡ S )z¥Returns a single event or a tuple of from/to events in case of a
        paired move event. If this buffer has been closed, immediately return
        None.
        )r
   Úget©r   r   r   r   Ú
read_event&   s   
zInotifyBuffer.read_eventc                 C   s   | j  ¡  | j ¡  d S r   )r   Úcloser
   r   r   r   r   Úon_thread_stop-   s   
zInotifyBuffer.on_thread_stopc                 C   s   |   ¡  |  ¡  d S r   )ÚstopÚjoinr   r   r   r   r   1   s   zInotifyBuffer.closec                    sž   g }|D ]H‰ t  dˆ ¡ ‡ fdd„}ˆ jrGt|ƒD ]\}}||ƒr)|ˆ f||<  nq| j |¡}|dur<| |ˆ f¡ qt  d¡ | ˆ ¡ q| ˆ ¡ q|S )zGroup any matching move eventszin-event %sc                    s   t | tƒ o| jo| jˆ jkS r   )Ú
isinstanceÚtupleÚis_moved_fromÚcookie)Úevent©Úinotify_eventr   r   Úmatching_from_event;   s   
ÿz8InotifyBuffer._group_events.<locals>.matching_from_eventNz'could not find matching move_from event)ÚloggerÚdebugÚis_moved_toÚ	enumerater
   ÚremoveÚappend)r   Ú
event_listÚgroupedr"   Úindexr   Ú
from_eventr   r    r   Ú_group_events5   s$   þ

€zInotifyBuffer._group_eventsc                 C   s°   d}|   ¡ rR|sT| j ¡ }|  |¡}|D ]5}t|tƒs(|jr(|j| jjkr'd}qt|tƒ o0|j	}| j
 ||¡ t|tƒsI|jrI|j| jjkrId}q|   ¡ rV|rdS dS dS dS )zÍRead event from `inotify` and add them to `queue`. When reading a
        IN_MOVE_TO event, remove the previous added matching IN_MOVE_FROM event
        and add them back to the queue as a tuple.
        FTN)Úshould_keep_runningr   Úread_eventsr-   r   r   Ú
is_ignoredÚsrc_pathr   r   r
   ÚputÚis_delete_self)r   Údeleted_selfÚinotify_eventsÚgrouped_eventsr!   r	   r   r   r   ÚrunQ   s"   

ÿ€îzInotifyBuffer.run)F)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r	   r   r   r   r   r-   r7   Ú__classcell__r   r   r   r   r      s    r   )ÚloggingÚwatchdog.utilsr   Úwatchdog.utils.delayed_queuer   Úwatchdog.observers.inotify_cr   Ú	getLoggerr8   r#   r   r   r   r   r   Ú<module>   s   
