U
    n5{iV  ã                   @   s’   d Z ddlZddlmZmZmZmZmZmZ	m
Z
 ddlmZ ddlmZ ddlmZ ddlmZ eeƒG dd	„ d	ejejƒƒZd
d„ Zd	dgZdS )zñ
A poll() based implementation of the twisted main loop.

To install the event loop (and you should do this before any connections,
listeners or connectors are added)::

    from twisted.internet import pollreactor
    pollreactor.install()
é    N)ÚPOLLERRÚPOLLHUPÚPOLLINÚPOLLNVALÚPOLLOUTÚerrorÚpoll)Úimplementer)Ú	posixbase)ÚIReactorFDSet)Úlogc                   @   s€   e Zd ZdZeeB eB ZeZ	e
Zdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Zdd„ Zdd„ Zdd„ ZeZdd„ Zdd„ ZdS )ÚPollReactora^  
    A reactor that uses poll(2).

    @ivar _poller: A L{select.poll} which will be used to check for I/O
        readiness.

    @ivar _selectables: A dictionary mapping integer file descriptors to
        instances of L{FileDescriptor} which have been registered with the
        reactor.  All L{FileDescriptor}s which are currently receiving read or
        write readiness notifications will be present as values in this
        dictionary.

    @ivar _reads: A dictionary mapping integer file descriptors to arbitrary
        values (this is essentially a set).  Keys in this dictionary will be
        registered with C{_poller} for read readiness notifications which will
        be dispatched to the corresponding L{FileDescriptor} instances in
        C{_selectables}.

    @ivar _writes: A dictionary mapping integer file descriptors to arbitrary
        values (this is essentially a set).  Keys in this dictionary will be
        registered with C{_poller} for write readiness notifications which will
        be dispatched to the corresponding L{FileDescriptor} instances in
        C{_selectables}.
    c                 C   s*   t ƒ | _i | _i | _i | _tj | ¡ dS )zo
        Initialize polling object, file descriptor tracking dictionaries, and
        the base class.
        N)r   Ú_pollerÚ_selectablesÚ_readsÚ_writesr
   ÚPosixReactorBaseÚ__init__©Úself© r   ú@/tmp/pip-unpacked-wheel-mnhg7r0q/twisted/internet/pollreactor.pyr   C   s
    zPollReactor.__init__c                 C   s|   z| j  |¡ W n tk
r$   Y nX d}|| jkr<|tB }|| jkrN|tB }|dkrf| j  ||¡ n|| jkrx| j|= dS )z*Register/unregister an fd with the poller.r   N)	r   Ú
unregisterÚKeyErrorr   r   r   r   Úregisterr   )r   ÚfdÚmaskr   r   r   Ú_updateRegistrationN   s    


zPollReactor._updateRegistrationc                 C   sl   z|  ¡ }||  W n: tk
rN   | j ¡ D ]\}}||kr. qJq.Y d S Y nX ||krh||= |  |¡ d S )N)ÚfilenoÚBaseExceptionr   Úitemsr   )r   Ú
selectableZmdictr   Zfdesr   r   r   Ú_dictRemove`   s    zPollReactor._dictRemovec                 C   s4   |  ¡ }|| jkr0|| j|< d| j|< |  |¡ dS )z@Add a FileDescriptor for notification of data available to read.é   N)r   r   r   r   )r   Úreaderr   r   r   r   Ú	addReaderu   s
    


zPollReactor.addReaderc                 C   s4   |  ¡ }|| jkr0|| j|< d| j|< |  |¡ dS )zAAdd a FileDescriptor for notification of data available to write.r#   N)r   r   r   r   )r   Úwriterr   r   r   r   Ú	addWriter}   s
    


zPollReactor.addWriterc                 C   s   |   || j¡S )z?Remove a Selectable for notification of data available to read.)r"   r   )r   r$   r   r   r   ÚremoveReader…   s    zPollReactor.removeReaderc                 C   s   |   || j¡S )z@Remove a Selectable for notification of data available to write.)r"   r   )r   r&   r   r   r   ÚremoveWriter‰   s    zPollReactor.removeWriterc                    s,   ˆ   ‡ fdd„ˆ jD ƒ‡ fdd„ˆ jD ƒ¡S )zD
        Remove all selectables, and return a list of them.
        c                    s   g | ]}ˆ j | ‘qS r   ©r   ©Ú.0r   r   r   r   Ú
<listcomp>’   s     z)PollReactor.removeAll.<locals>.<listcomp>c                    s   g | ]}ˆ j | ‘qS r   r*   r+   r   r   r   r-   “   s     )Z
_removeAllr   r   r   r   r   r   Ú	removeAll�   s    þzPollReactor.removeAllc              
   C   s¶   |dk	rt |d ƒ}z| j |¡}W n> tk
rb } z |jd tjkrPW Y ¢
dS ‚ W 5 d}~X Y nX | j}|D ]B\}}z| j| }W n t	k
rœ   Y qnY nX t
 |||||¡ qndS )zPoll the poller for new events.Niè  r   )Úintr   r   ÚSelectErrorÚargsÚerrnoZEINTRZ_doReadOrWriter   r   r   ZcallWithLogger)r   ÚtimeoutÚlÚeZ_drdwr   Úeventr!   r   r   r   ÚdoPoll–   s    

zPollReactor.doPollc                    s   ‡ fdd„ˆ j D ƒS )Nc                    s   g | ]}ˆ j | ‘qS r   r*   r+   r   r   r   r-   ¯   s     z*PollReactor.getReaders.<locals>.<listcomp>)r   r   r   r   r   Ú
getReaders®   s    zPollReactor.getReadersc                    s   ‡ fdd„ˆ j D ƒS )Nc                    s   g | ]}ˆ j | ‘qS r   r*   r+   r   r   r   r-   ²   s     z*PollReactor.getWriters.<locals>.<listcomp>)r   r   r   r   r   Ú
getWriters±   s    zPollReactor.getWritersN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   Z_POLL_DISCONNECTEDr   Z_POLL_INr   Z	_POLL_OUTr   r   r"   r%   r'   r(   r)   r.   r7   ZdoIterationr8   r9   r   r   r   r   r   $   s    	r   c                  C   s   t ƒ } ddlm} || ƒ dS )zInstall the poll() reactor.r   )ÚinstallReactorN)r   Ztwisted.internet.mainr>   )Úpr>   r   r   r   Úinstallµ   s    r@   )r=   r2   Úselectr   r   r   r   r   r   r0   r   Zzope.interfacer	   Ztwisted.internetr
   Ztwisted.internet.interfacesr   Ztwisted.pythonr   r   Z_PollLikeMixinr   r@   Ú__all__r   r   r   r   Ú<module>   s   $
 