o
    ¯b¢1  ã                   @   s–   d Z ddlZddlmZ ddlmZmZmZ ddlm	Z	 ddl
mZ g fdd„ZG d	d
„ d
ejƒZee	ƒG dd„ dejejƒƒZG dd„ dejƒZdS )aC  
This module provides base support for Twisted to interact with the glib/gtk
mainloops.

The classes in this module should not be used directly, but rather you should
import gireactor or gtk3reactor for GObject Introspection based applications,
or glib2reactor or gtk2reactor for applications using legacy static bindings.
é    N)Úimplementer)ÚbaseÚ	posixbaseÚselectreactor)ÚIReactorFDSet)Úlogc                 C   s:   | D ]}t j |¡durt|ƒ‚q|D ]}dt j|< qdS )a[  
    Check whether the given modules were imported, and if requested, ensure
    they will not be importable in the future.

    @param moduleNames: A list of module names we make sure aren't imported.
    @type moduleNames: C{list} of C{str}

    @param preventImports: A list of module name whose future imports should
        be prevented.
    @type preventImports: C{list} of C{str}

    @param errorMessage: Message to use when raising an C{ImportError}.
    @type errorMessage: C{str}

    @raise ImportError: with given error message if a given module name
        has already been imported.
    N)ÚsysÚmodulesÚgetÚImportError)ÚmoduleNamesÚerrorMessageÚpreventImportsÚname© r   ú</usr/lib/python3/dist-packages/twisted/internet/_glibbase.pyÚensureNotImported   s   ÿÿr   c                   @   s   e Zd ZdZdd„ ZdS )Ú	GlibWakerz/
    Run scheduled events after waking up.
    c                 C   s   t j | ¡ | j ¡  d S ©N)r   Ú
_UnixWakerÚdoReadÚreactorÚ	_simulate©Úselfr   r   r   r   8   s   zGlibWaker.doReadN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r   3   s    r   c                   @   sº   e Zd ZdZeZd.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dd„ Zdd„ Zdd„ Zdd„ Zd/dd„Zd d!„ Zd"d#„ Zd0d%d&„Zd'd(„ Zd)d*„ Zd+d,„ Zd-S )1ÚGlibReactorBasea7  
    Base class for GObject event loop reactors.

    Notification for I/O events (reads and writes on file descriptors) is done
    by the gobject-based event loop. File descriptors are registered with
    gobject with the appropriate flags for read/write/disconnect notification.

    Time-based events, the results of C{callLater} and C{callFromThread}, are
    handled differently. Rather than registering each event with gobject, a
    single gobject timeout is registered for the earliest scheduled event, the
    output of C{reactor.timeout()}. For example, if there are timeouts in 1, 2
    and 3.4 seconds, a single timeout is registered for 1 second in the
    future. When this timeout is hit, C{_simulate} is called, which calls the
    appropriate Twisted-level handlers, and a new timeout is added to gobject
    by the C{_reschedule} method.

    To handle C{callFromThread} events, we use a custom waker that calls
    C{_simulate} whenever it wakes up.

    @ivar _sources: A dictionary mapping L{FileDescriptor} instances to
        GSource handles.

    @ivar _reads: A set of L{FileDescriptor} instances currently monitored for
        reading.

    @ivar _writes: A set of L{FileDescriptor} instances currently monitored for
        writing.

    @ivar _simtag: A GSource handle for the next L{simulate} call.
    Fc                    sÈ   d ˆ _ tƒ ˆ _tƒ ˆ _i ˆ _|ˆ _|ˆ _tj 	ˆ ¡ ˆ jj
ˆ _ˆ jjˆ _‡ fdd„}|r@ˆ jjˆ _ˆ jjˆ _|ˆ _ˆ jjˆ _d S ˆ j ¡ ˆ _ˆ jjˆ _ˆ jjˆ _ˆ j ¡ ˆ _‡ fdd„ˆ _ˆ jjˆ _d S )Nc                      ó   ˆ j  ¡ rˆ j  ¡  d S d S r   ©Ú_gtkÚ
main_levelÚ	main_quitr   r   r   r   Ú	_mainquitn   ó   
ÿz+GlibReactorBase.__init__.<locals>._mainquitc                      ó   ˆ j  ˆ jj¡S r   ©Ú_glibÚidle_addÚloopÚquitr   r   r   r   Ú<lambda>|   ó    z*GlibReactorBase.__init__.<locals>.<lambda>)Ú_simtagÚsetÚ_readsÚ_writesÚ_sourcesr)   r"   r   ÚPosixReactorBaseÚ__init__Úsource_removeÚ_source_removeÚtimeout_addÚ_timeout_addÚevents_pendingÚ_pendingÚmain_iteration_doÚ
_iterationÚ_crashÚmainÚ_runÚmain_context_defaultÚcontextÚpendingÚ	iterationÚMainLoopr+   Úrun©r   Úglib_moduleÚ
gtk_moduleÚuseGtkr%   r   r   r   r5   b   s*   





zGlibReactorBase.__init__c                 C   s"   t j | ¡ |  dtjj| ¡ d S )Nr   )r   Ú_SignalReactorMixinÚ_handleSignalsÚ	callLaterr   r4   r   r   r   r   rL      s   zGlibReactorBase._handleSignalsc                    sB   t ˆdƒr‡ ‡fdd„}ˆ ¡ }nˆ}ˆ }| jj|||| jjdS )NÚfilenoc                    s
   ˆ ˆ|ƒS r   r   )ÚignoredÚ	condition©ÚcallbackÚsourcer   r   Úwrapper”   s   
z*GlibReactorBase.input_add.<locals>.wrapper©Úpriority)ÚhasattrrN   r)   Úio_add_watchÚPRIORITY_DEFAULT_IDLE)r   rS   rP   rR   rT   rN   r   rQ   r   Ú	input_add‘   s   

ÿzGlibReactorBase.input_addc                 C   s   t  || j|||¡ dS )z@
        Called by event loop when an I/O event occurs.
        T)r   ÚcallWithLoggerÚ_doReadOrWrite)r   rS   rP   r   r   r   Ú_ioEventCallbackŸ   s   z GlibReactorBase._ioEventCallbackc                 C   sT   ||v rdS |}||v r|   | j| ¡ ||O }|  ||| j¡| j|< | |¡ dS )a  
        Add the given L{FileDescriptor} for monitoring either for reading or
        writing. If the file is already monitored for the other operation, we
        delete the previous registration and re-register it for both reading
        and writing.
        N)r7   r3   rZ   r]   Úadd)r   rS   ÚprimaryÚotherÚprimaryFlagÚ	otherFlagÚflagsr   r   r   Ú_add¦   s   zGlibReactorBase._addc                 C   ó   |   || j| j| j| j¡ dS )zS
        Add a L{FileDescriptor} for monitoring of data available to read.
        N)rd   r1   r2   ÚINFLAGSÚOUTFLAGS©r   Úreaderr   r   r   Ú	addReader¶   ó   zGlibReactorBase.addReaderc                 C   re   )zO
        Add a L{FileDescriptor} for monitoring ability to write data.
        N)rd   r2   r1   rg   rf   ©r   Úwriterr   r   r   Ú	addWriter¼   rk   zGlibReactorBase.addWriterc                 C   ó
   t | jƒS )zW
        Retrieve the list of current L{FileDescriptor} monitored for reading.
        )Úlistr1   r   r   r   r   Ú
getReadersÂ   ó   
zGlibReactorBase.getReadersc                 C   ro   )zW
        Retrieve the list of current L{FileDescriptor} monitored for writing.
        )rp   r2   r   r   r   r   Ú
getWritersÈ   rr   zGlibReactorBase.getWritersc                 C   s   |   | j| j¡S )zJ
        Remove monitoring for all registered L{FileDescriptor}s.
        )Ú
_removeAllr1   r2   r   r   r   r   Ú	removeAllÎ   s   zGlibReactorBase.removeAllc                 C   sX   ||vrdS |   | j| ¡ | |¡ ||v r$|  ||| j¡| j|< dS | j |¡ dS )zÝ
        Remove monitoring the given L{FileDescriptor} for either reading or
        writing. If it's still monitored for the other operation, we
        re-register the L{FileDescriptor} for only that operation.
        N)r7   r3   ÚremoverZ   r]   Úpop)r   rS   r_   r`   rc   r   r   r   Ú_removeÔ   s   
zGlibReactorBase._removec                 C   ó   |   || j| j| j¡ dS )zJ
        Stop monitoring the given L{FileDescriptor} for reading.
        N)rx   r1   r2   rg   rh   r   r   r   ÚremoveReaderã   ó   zGlibReactorBase.removeReaderc                 C   ry   )zJ
        Stop monitoring the given L{FileDescriptor} for writing.
        N)rx   r2   r1   rf   rl   r   r   r   ÚremoveWriteré   r{   zGlibReactorBase.removeWriterr   c                 C   s*   |   ¡  |  ¡ r|  d¡ |  ¡ sdS dS )zv
        One iteration of the event loop, for trial's use.

        This is not used for actual reactor runs.
        r   N)ÚrunUntilCurrentr;   r=   )r   Údelayr   r   r   Úiterateï   s   
ÿzGlibReactorBase.iteratec                 C   ó   t j | ¡ |  ¡  dS )z$
        Crash the reactor.
        N)r   r4   Úcrashr>   r   r   r   r   r   ù   s   zGlibReactorBase.crashc                 C   r€   )z#
        Stop the reactor.
        N)r   r4   ÚstopÚwakeUpr   r   r   r   r‚      s   zGlibReactorBase.stopTc                 C   s.   |   | j¡ | j|d | jr|  ¡  dS dS )z"
        Run the reactor.
        ©ÚinstallSignalHandlersN)ÚcallWhenRunningÚ_rescheduleÚstartRunningÚ_startedr@   ©r   r…   r   r   r   rF     s
   ÿzGlibReactorBase.runc                 O   s&   t jj| g|¢R i |¤Ž}|  ¡  |S )z,
        Schedule a C{DelayedCall}.
        )r   r4   rM   r‡   )r   ÚargsÚkwargsÚresultr   r   r   rM     s   zGlibReactorBase.callLaterc                 C   sT   | j dur|  | j ¡ d| _ |  ¡ }|dur(| jt|d ƒ| j| jjd| _ dS dS )z;
        Schedule a glib timeout for C{_simulate}.
        Néè  rU   )r/   r7   Útimeoutr9   Úintr   r)   rY   ©r   r   r   r   r   r‡   !  s   

ýÿzGlibReactorBase._reschedulec                 C   s   |   ¡  |  ¡  dS )zX
        Run timers, and then reschedule glib timeout for next scheduled event.
        N)r}   r‡   r   r   r   r   r   0  s   zGlibReactorBase._simulateN©F)r   ©T)r   r   r   r   r   Ú_wakerFactoryr5   rL   rZ   r]   rd   rj   rn   rq   rs   ru   rx   rz   r|   r   r   r‚   rF   rM   r‡   r   r   r   r   r   r   =   s.    !



	
r   c                   @   s4   e Zd ZdZddd„Zdd„ Zddd	„Zd
d„ ZdS )ÚPortableGlibReactorBasezŠ
    Base class for GObject event loop reactors that works on Windows.

    Sockets aren't supported by GObject's input_add on Win32.
    Fc                    s~   d ˆ _ |ˆ _|ˆ _tj ˆ ¡ ˆ jjˆ _ˆ jjˆ _	‡ fdd„}|r+|ˆ _
ˆ jjˆ _d S ˆ j ¡ ˆ _‡ fdd„ˆ _
ˆ jjˆ _d S )Nc                      r    r   r!   r   r   r   r   r%   H  r&   z3PortableGlibReactorBase.__init__.<locals>._mainquitc                      r'   r   r(   r   r   r   r   r-   Q  r.   z2PortableGlibReactorBase.__init__.<locals>.<lambda>)r/   r)   r"   r   ÚSelectReactorr5   r6   r7   r8   r9   r>   r?   r@   rE   r+   rF   rG   r   r   r   r5   ?  s   

z PortableGlibReactorBase.__init__c                 C   s   t j | ¡ |  ¡  d S r   )r   r–   r   r>   r   r   r   r   r   T  s   zPortableGlibReactorBase.crashTc                 C   s0   | j |d |  d| j¡ | jr|  ¡  d S d S )Nr„   r   )rˆ   r9   Úsimulater‰   r@   rŠ   r   r   r   rF   X  s
   ÿzPortableGlibReactorBase.runc                 C   s\   | j dur|  | j ¡ |  ¡  t|  ¡ dƒ}|du rd}| jt|d ƒ| j| jj	d| _ dS )z@
        Run simulation loops and reschedule callbacks.
        Ng{®Gáz„?rŽ   rU   )
r/   r7   r   Úminr   r9   r   r—   r)   rY   r‘   r   r   r   r—   ^  s   

ýz PortableGlibReactorBase.simulateNr’   r“   )r   r   r   r   r5   r   rF   r—   r   r   r   r   r•   8  s    

r•   )r   r   Úzope.interfacer   Útwisted.internetr   r   r   Útwisted.internet.interfacesr   Útwisted.pythonr   r   r   r   r4   Ú_PollLikeMixinr   r–   r•   r   r   r   r   Ú<module>   s   

 {