ó
±ZOc           @   så   d  Z  d d l Z d d l m Z m Z m Z d d l m Z d d l m	 Z	 m
 Z
 d d l m Z d d l m Z d e f d	 „  ƒ  YZ d
 e j f d „  ƒ  YZ d e e j e j f d „  ƒ  YZ d e e j f d „  ƒ  YZ d S(   sC  
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.
iÿÿÿÿN(   t   baset	   posixbaset   selectreactor(   t   IReactorFDSet(   t   logt   runtime(   t   set(   t
   implementst   GlibSignalMixinc           B   s#   e  Z e j d  k r! d „  Z n  RS(   t   posixc            sx   d d l  m ‰ t j j ˆ  ƒ t j t j ‡  ‡ f d †  ƒ t t d d  ƒ d  k	 rm t j	 t j t
 ƒ n  ˆ ƒ  d  S(   Niÿÿÿÿ(   t   reapAllProcessesc             s   ˆ  j  ˆ ƒ S(   N(   t   callFromThread(   t   a(   t   selft   _reapAllProcesses(    s>   /usr/lib/python2.7/dist-packages/twisted/internet/_glibbase.pyt   <lambda>'   s    t   siginterrupt(   t   twisted.internet.processR
   R    t   _SignalReactorMixint   _handleSignalst   signalt   SIGCHLDt   getattrt   NoneR   t   False(   R   (    (   R   R   s>   /usr/lib/python2.7/dist-packages/twisted/internet/_glibbase.pyR      s    (   t   __name__t
   __module__R   t   platformTypeR   (    (    (    s>   /usr/lib/python2.7/dist-packages/twisted/internet/_glibbase.pyR      s   t	   GlibWakerc           B   s   e  Z d  Z d „  Z RS(   s/   
    Run scheduled events after waking up.
    c         C   s!   t  j j |  ƒ |  j j ƒ  d  S(   N(   R   t
   _UnixWakert   doReadt   reactort	   _simulate(   R   (    (    s>   /usr/lib/python2.7/dist-packages/twisted/internet/_glibbase.pyR   5   s    (   R   R   t   __doc__R   (    (    (    s>   /usr/lib/python2.7/dist-packages/twisted/internet/_glibbase.pyR   0   s   t   GlibReactorBasec           B   sÒ   e  Z d  Z e e ƒ e Z e d „ Z d „  Z	 d „  Z
 d „  Z d „  Z d „  Z d „  Z d „  Z d	 „  Z d
 „  Z d „  Z d „  Z d d „ Z d „  Z d „  Z e d „ Z d „  Z d „  Z d „  Z RS(   s;  
    Base class for GObject event loop reactors.

    Notification for I/O events (reads and writes on file descriptors) is done
    by the 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.
    c            s  d  ˆ  _ t ƒ  ˆ  _ t ƒ  ˆ  _ i  ˆ  _ | ˆ  _ | ˆ  _ t j	 j
 ˆ  ƒ ˆ  j j ˆ  _ ˆ  j j ˆ  _ ‡  f d †  } | r¸ ˆ  j j ˆ  _ ˆ  j j ˆ  _ | ˆ  _ ˆ  j j ˆ  _ nc ˆ  j j ƒ  ˆ  _ ˆ  j j ˆ  _ ˆ  j j ˆ  _ ˆ  j j ƒ  ˆ  _ ‡  f d †  ˆ  _ ˆ  j j ˆ  _ d  S(   Nc              s#   ˆ  j  j ƒ  r ˆ  j  j ƒ  n  d  S(   N(   t   _gtkt
   main_levelt	   main_quit(    (   R   (    s>   /usr/lib/python2.7/dist-packages/twisted/internet/_glibbase.pyt	   _mainquitm   s    c              s   ˆ  j  j ˆ  j j ƒ S(   N(   t   _glibt   idle_addt   loopt   quit(    (   R   (    s>   /usr/lib/python2.7/dist-packages/twisted/internet/_glibbase.pyR   {   s    (   R   t   _simtagR   t   _readst   _writest   _sourcesR'   R#   R   t   PosixReactorBaset   __init__t   source_removet   _source_removet   timeout_addt   _timeout_addt   events_pendingt   _pendingt   main_iteration_dot
   _iterationt   _crasht   maint   _runt   main_context_defaultt   contextt   pendingt	   iterationt   MainLoopR)   t   run(   R   t   glib_modulet
   gtk_modulet   useGtkR&   (    (   R   s>   /usr/lib/python2.7/dist-packages/twisted/internet/_glibbase.pyR0   a   s*    					c         C   sT   t  | d ƒ r: | | d „ } |  j j | j ƒ  | | ƒ S|  j j | | | ƒ Sd  S(   Nt   filenoc         S   s   | | | ƒ S(   N(    (   t   sourcet	   conditiont   real_st   real_cb(    (    s>   /usr/lib/python2.7/dist-packages/twisted/internet/_glibbase.pyt   wrapper‹   s    (   t   hasattrR'   t   io_add_watchRE   (   R   RF   RG   t   callbackRJ   (    (    s>   /usr/lib/python2.7/dist-packages/twisted/internet/_glibbase.pyt	   input_addˆ   s    c         C   s    t  j | |  j | | | ƒ t S(   s@   
        Called by event loop when an I/O event occurs.
        (   R   t   callWithLoggert   _doReadOrWritet   True(   R   RF   RG   (    (    s>   /usr/lib/python2.7/dist-packages/twisted/internet/_glibbase.pyt   _ioEventCallback’   s    c         C   ss   | | k r d S| } | | k rC |  j  |  j | ƒ | | O} n  |  j | | |  j ƒ |  j | <| j | ƒ d S(   s  
        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(   R2   R.   RN   RR   t   add(   R   RF   t   primaryt   othert   primaryFlagt	   otherFlagt   flags(    (    s>   /usr/lib/python2.7/dist-packages/twisted/internet/_glibbase.pyt   _add›   s    c         C   s)   |  j  | |  j |  j |  j |  j ƒ d S(   sS   
        Add a L{FileDescriptor} for monitoring of data available to read.
        N(   RY   R,   R-   t   INFLAGSt   OUTFLAGS(   R   t   reader(    (    s>   /usr/lib/python2.7/dist-packages/twisted/internet/_glibbase.pyt	   addReader­   s    c         C   s)   |  j  | |  j |  j |  j |  j ƒ d S(   sO   
        Add a L{FileDescriptor} for monitoring ability to write data.
        N(   RY   R-   R,   R[   RZ   (   R   t   writer(    (    s>   /usr/lib/python2.7/dist-packages/twisted/internet/_glibbase.pyt	   addWriterµ   s    c         C   s   t  |  j ƒ S(   sW   
        Retrieve the list of current L{FileDescriptor} monitored for reading.
        (   t   listR,   (   R   (    (    s>   /usr/lib/python2.7/dist-packages/twisted/internet/_glibbase.pyt
   getReaders½   s    c         C   s   t  |  j ƒ S(   sW   
        Retrieve the list of current L{FileDescriptor} monitored for writing.
        (   R`   R-   (   R   (    (    s>   /usr/lib/python2.7/dist-packages/twisted/internet/_glibbase.pyt
   getWritersÄ   s    c         C   s   |  j  |  j |  j ƒ S(   sJ   
        Remove monitoring for all registered L{FileDescriptor}s.
        (   t
   _removeAllR,   R-   (   R   (    (    s>   /usr/lib/python2.7/dist-packages/twisted/internet/_glibbase.pyt	   removeAllË   s    c         C   ss   | | k r d S|  j  |  j | ƒ | j | ƒ | | k r_ |  j | | |  j ƒ |  j | <n |  j j | ƒ d S(   sÝ   
        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(   R2   R.   t   removeRN   RR   t   pop(   R   RF   RT   RU   RX   (    (    s>   /usr/lib/python2.7/dist-packages/twisted/internet/_glibbase.pyt   _removeÒ   s    c         C   s#   |  j  | |  j |  j |  j ƒ d S(   sJ   
        Stop monitoring the given L{FileDescriptor} for reading.
        N(   Rg   R,   R-   R[   (   R   R\   (    (    s>   /usr/lib/python2.7/dist-packages/twisted/internet/_glibbase.pyt   removeReaderã   s    c         C   s#   |  j  | |  j |  j |  j ƒ d S(   sJ   
        Stop monitoring the given L{FileDescriptor} for writing.
        N(   Rg   R-   R,   RZ   (   R   R^   (    (    s>   /usr/lib/python2.7/dist-packages/twisted/internet/_glibbase.pyt   removeWriterê   s    i    c         C   s.   |  j  ƒ  x |  j ƒ  r) |  j d ƒ q Wd S(   sv   
        One iteration of the event loop, for trial's use.

        This is not used for actual reactor runs.
        i    N(   t   runUntilCurrentR6   R8   (   R   t   delay(    (    s>   /usr/lib/python2.7/dist-packages/twisted/internet/_glibbase.pyt   iterateñ   s    
c         C   s   t  j j |  ƒ |  j ƒ  d S(   s$   
        Crash the reactor.
        N(   R   R/   t   crashR9   (   R   (    (    s>   /usr/lib/python2.7/dist-packages/twisted/internet/_glibbase.pyRm   ü   s    c         C   s   t  j j |  ƒ |  j ƒ  d S(   s#   
        Stop the reactor.
        N(   R   R/   t   stopt   wakeUp(   R   (    (    s>   /usr/lib/python2.7/dist-packages/twisted/internet/_glibbase.pyRn     s    c         C   s:   |  j  |  j ƒ |  j d | ƒ |  j r6 |  j ƒ  n  d S(   s"   
        Run the reactor.
        t   installSignalHandlersN(   t   callWhenRunningt   _reschedulet   startRunningt   _startedR;   (   R   Rp   (    (    s>   /usr/lib/python2.7/dist-packages/twisted/internet/_glibbase.pyRA     s    	c         O   s&   t  j j |  | | Ž } |  j ƒ  | S(   s,   
        Schedule a C{DelayedCall}.
        (   R   R/   t	   callLaterRr   (   R   t   argst   kwargst   result(    (    s>   /usr/lib/python2.7/dist-packages/twisted/internet/_glibbase.pyRu     s    
c         C   sl   |  j  d k	 r+ |  j |  j  ƒ d |  _  n  |  j ƒ  } | d k	 rh |  j t | d ƒ |  j ƒ |  _  n  d S(   s;   
        Schedule a glib timeout for C{_simulate}.
        iè  N(   R+   R   R2   t   timeoutR4   t   intR    (   R   Ry   (    (    s>   /usr/lib/python2.7/dist-packages/twisted/internet/_glibbase.pyRr   (  s    c         C   s   |  j  ƒ  |  j ƒ  d S(   sX   
        Run timers, and then reschedule glib timeout for next scheduled event.
        N(   Rj   Rr   (   R   (    (    s>   /usr/lib/python2.7/dist-packages/twisted/internet/_glibbase.pyR    5  s    
(   R   R   R!   R   R   R   t   _wakerFactoryR   R0   RN   RR   RY   R]   R_   Ra   Rb   Rd   Rg   Rh   Ri   Rl   Rm   Rn   RQ   RA   Ru   Rr   R    (    (    (    s>   /usr/lib/python2.7/dist-packages/twisted/internet/_glibbase.pyR"   ;   s,   
'	
													
		t   PortableGlibReactorBasec           B   s8   e  Z d  Z e d „ Z d „  Z e d „ Z d „  Z RS(   sŠ   
    Base class for GObject event loop reactors that works on Windows.

    Sockets aren't supported by GObject's input_add on Win32.
    c            s°   d  ˆ  _ | ˆ  _ | ˆ  _ t j j ˆ  ƒ ˆ  j j ˆ  _ ˆ  j j	 ˆ  _
 ‡  f d †  } | ry | ˆ  _ ˆ  j j ˆ  _ n3 ˆ  j j ƒ  ˆ  _ ‡  f d †  ˆ  _ ˆ  j j ˆ  _ d  S(   Nc              s#   ˆ  j  j ƒ  r ˆ  j  j ƒ  n  d  S(   N(   R#   R$   R%   (    (   R   (    s>   /usr/lib/python2.7/dist-packages/twisted/internet/_glibbase.pyR&   M  s    c              s   ˆ  j  j ˆ  j j ƒ S(   N(   R'   R(   R)   R*   (    (   R   (    s>   /usr/lib/python2.7/dist-packages/twisted/internet/_glibbase.pyR   V  s    (   R   R+   R'   R#   R   t   SelectReactorR0   R1   R2   R3   R4   R9   R:   R;   R@   R)   RA   (   R   RB   RC   RD   R&   (    (   R   s>   /usr/lib/python2.7/dist-packages/twisted/internet/_glibbase.pyR0   D  s    				c         C   s   t  j j |  ƒ |  j ƒ  d  S(   N(   R   R}   Rm   R9   (   R   (    (    s>   /usr/lib/python2.7/dist-packages/twisted/internet/_glibbase.pyRm   Z  s    c         C   s=   |  j  d | ƒ |  j d |  j ƒ |  j r9 |  j ƒ  n  d  S(   NRp   i    (   Rs   R4   t   simulateRt   R;   (   R   Rp   (    (    s>   /usr/lib/python2.7/dist-packages/twisted/internet/_glibbase.pyRA   _  s    	c         C   s|   |  j  d k	 r" |  j |  j  ƒ n  |  j ƒ  t |  j ƒ  d ƒ } | d k rV d } n  |  j t | d ƒ |  j ƒ |  _  d S(   s@   
        Run simulation loops and reschedule callbacks.
        g{®Gáz„?iè  N(	   R+   R   R2   Rl   t   minRy   R4   Rz   R~   (   R   Ry   (    (    s>   /usr/lib/python2.7/dist-packages/twisted/internet/_glibbase.pyR~   f  s    
	(	   R   R   R!   R   R0   Rm   RQ   RA   R~   (    (    (    s>   /usr/lib/python2.7/dist-packages/twisted/internet/_glibbase.pyR|   >  s
   	(   R!   R   t   twisted.internetR    R   R   t   twisted.internet.interfacesR   t   twisted.pythonR   R   t   twisted.python.compatR   t   zope.interfaceR   t   objectR   R   R   R/   t   _PollLikeMixinR"   R}   R|   (    (    (    s>   /usr/lib/python2.7/dist-packages/twisted/internet/_glibbase.pyt   <module>   s   ÿ 