U
    ˆj²  ã                   @   sº   d Z ddlZddlZddlmZmZ ddlmZ G dd„ deƒZ	e
dœdd	„Ze
ee e
e
d
œdd„Ze
e
ddœdd„Zejddœdd„Zej d¡Zes¢edƒ‚ej e¡Zeeƒ dS )zŒ
Very low-level ctypes-based interface to Linux inotify(7).

ctypes and a version of libc which supports inotify system calls are
required.
é    N)ÚAnyÚcast)ÚFilePathc                   @   s   e Zd ZdZdS )ÚINotifyErrorzR
    Unify all the possible exceptions that can be raised by the INotify API.
    N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__© r
   r
   ú;/tmp/pip-unpacked-wheel-j5h_c8_q/twisted/python/_inotify.pyr      s   r   )Úreturnc                  C   s"   t tt ¡ ƒ} | dk rtdƒ‚| S )zO
    Create an inotify instance and return the associated file descriptor.
    r   zINotify initialization error.)r   ÚintÚlibcÚinotify_initr   )Úfdr
   r
   r   Úinit   s    r   )r   ÚpathÚmaskr   c                 C   s<   t tt | | ¡ j|¡ƒ}|dk r8td|›d|›d�ƒ‚|S )a/  
    Add a watch for the given path to the inotify file descriptor, and return
    the watch descriptor.

    @param fd: The file descriptor returned by C{libc.inotify_init}.
    @param path: The path to watch via inotify.
    @param mask: Bitmask specifying the events that inotify should monitor.
    r   zFailed to add watch on 'z' - (ú))r   r   r   Úinotify_add_watchZasBytesModer   r   )r   r   r   Úwdr
   r
   r   Úadd#   s    	r   )r   r   r   c                 C   s   t  | |¡ dS )zM
    Remove the given watch descriptor from the inotify file descriptor.
    N)r   Úinotify_rm_watch)r   r   r
   r
   r   Úremove2   s    r   )r   r   c                 C   sp   dD ]}t | |dƒdkrtdƒ‚qg | j_tj| j_tjtjg| j_tj| j_tjtjtj	g| j
_tj| j
_dS )zµ
    Initialize the module, checking if the expected APIs exist and setting the
    argtypes and restype for C{inotify_init}, C{inotify_add_watch}, and
    C{inotify_rm_watch}.
    )r   r   r   Nzlibc6 2.4 or higher needed)ÚgetattrÚImportErrorr   ÚargtypesÚctypesÚc_intÚrestyper   Úc_char_pÚc_uint32r   )r   Úfunctionr
   r
   r   ÚinitializeModuleM   s    


r#   ÚczCan't find C library.)r	   r   Zctypes.utilÚtypingr   r   Ztwisted.python.filepathr   Ú	Exceptionr   r   r   r   r   ÚCDLLr#   ÚutilZfind_libraryÚnamer   ÚcdllÚLoadLibraryr   r
   r
   r
   r   Ú<module>   s   
