ó
L]c           @   sp   d  Z  d d l Z d d l m Z d d l m Z m Z m Z d e f d     YZ	 d d  Z d d	 g Z d S(
   s*   0MQ polling related functions and classes.i˙˙˙˙N(   t   zmq_polli   (   t   POLLINt   POLLOUTt   POLLERRt   Pollerc           B   sa   e  Z d  Z d Z i  Z d   Z d   Z e e	 Bd  Z
 e e	 Bd  Z d   Z d d  Z RS(   s>   A stateful poll interface that mirrors Python's built-in poll.c         C   s   g  |  _  i  |  _ d  S(   N(   t   socketst   _map(   t   self(    (    s-   lib/python2.7/site-packages/zmq/sugar/poll.pyt   __init__   s    	c         C   s   | |  j  k S(   N(   R   (   R   t   socket(    (    s-   lib/python2.7/site-packages/zmq/sugar/poll.pyt   __contains__   s    c         C   s   | rm | |  j  k r8 |  j  | } | | f |  j | <q t |  j  } |  j j | | f  | |  j  | <n | |  j  k r |  j |  n  d S(   s  p.register(socket, flags=POLLIN|POLLOUT)

        Register a 0MQ socket or native fd for I/O monitoring.
        
        register(s,0) is equivalent to unregister(s).

        Parameters
        ----------
        socket : zmq.Socket or native socket
            A zmq.Socket or any Python object having a ``fileno()`` 
            method that returns a valid file descriptor.
        flags : int
            The events to watch for.  Can be POLLIN, POLLOUT or POLLIN|POLLOUT.
            If `flags=0`, socket will be unregistered.
        N(   R   R   t   lent   appendt
   unregister(   R   R	   t   flagst   idx(    (    s-   lib/python2.7/site-packages/zmq/sugar/poll.pyt   register   s    c         C   s   |  j  | |  d S(   sC   Modify the flags for an already registered 0MQ socket or native fd.N(   R   (   R   R	   R   (    (    s-   lib/python2.7/site-packages/zmq/sugar/poll.pyt   modify;   s    c         C   sW   |  j  j |  } |  j j |  x. |  j | D] \ } } |  j  | c d 8<q0 Wd S(   s­   Remove a 0MQ socket or native fd for I/O monitoring.

        Parameters
        ----------
        socket : Socket
            The socket instance to stop polling.
        i   N(   R   t   popR   (   R   R	   R   R   (    (    s-   lib/python2.7/site-packages/zmq/sugar/poll.pyR   ?   s    c         C   sR   | d k s | d k  r! d } n t | t  r? t |  } n  t |  j d | S(   së  Poll the registered 0MQ or native fds for I/O.

        Parameters
        ----------
        timeout : float, int
            The timeout in milliseconds. If None, no `timeout` (infinite). This
            is in milliseconds to be compatible with ``select.poll()``.

        Returns
        -------
        events : list of tuples
            The list of events that are ready to be processed.
            This is a list of tuples of the form ``(socket, event)``, where the 0MQ Socket
            or integer fd is the first element, and the poll event mask (POLLIN, POLLOUT) is the second.
            It is common to call ``events = dict(poller.poll())``,
            which turns the list of tuples into a mapping of ``socket : event``.
        i    i˙˙˙˙t   timeoutN(   t   Nonet
   isinstancet   floatt   intR    R   (   R   R   (    (    s-   lib/python2.7/site-packages/zmq/sugar/poll.pyt   pollM   s
    	N(   t   __name__t
   __module__t   __doc__R   R   R   R   R
   R   R   R   R   R   R   (    (    (    s-   lib/python2.7/site-packages/zmq/sugar/poll.pyR      s   			c         C   sX  | d k r d } n  t | d  } | d k  r: d } n  g  } x t |  | |  D]j } d } | |  k rz | t O} n  | | k r | t O} n  | | k rŹ | t O} n  | j | | f  qU Wt | |  } g  g  g  }  } } xb | D]Z \ } } | t @r|  j |  n  | t @r-| j |  n  | t @rí | j |  qí qí W|  | | f S(   s\  select(rlist, wlist, xlist, timeout=None) -> (rlist, wlist, xlist)

    Return the result of poll as a lists of sockets ready for r/w/exception.

    This has the same interface as Python's built-in ``select.select()`` function.

    Parameters
    ----------
    timeout : float, int, optional
        The timeout in seconds. If None, no timeout (infinite). This is in seconds to be
        compatible with ``select.select()``.
    rlist : list of sockets/FDs
        sockets/FDs to be polled for read events
    wlist : list of sockets/FDs
        sockets/FDs to be polled for write events
    xlist : list of sockets/FDs
        sockets/FDs to be polled for error events
    
    Returns
    -------
    (rlist, wlist, xlist) : tuple of lists of sockets (length 3)
        Lists correspond to sockets available for read/write/error events respectively.
    i˙˙˙˙g     @@i    N(   R   R   t   setR   R   R   R   R    (   t   rlistt   wlistt   xlistR   R   t   sR   t   return_sockets(    (    s-   lib/python2.7/site-packages/zmq/sugar/poll.pyt   selectf   s2    		


R"   (   R   t   zmqt   zmq.backendR    t	   constantsR   R   R   t   objectR   R   R"   t   __all__(    (    (    s-   lib/python2.7/site-packages/zmq/sugar/poll.pyt   <module>   s   V8