
L]c           @  s\  d  Z  d d l m Z m Z m Z d d l Z d d l Z d d l Z d d l m	 Z	 m
 Z
 m Z m Z m Z m Z y d d l Z e j Z Wn e e f k
 r d Z n Xd d l m Z m Z d d l m Z d e f d	     YZ d
 e f d     YZ d e f d     YZ d e k o,d k  n rIe d  Z e e _ n  e Z  d   Z! d S(   sX  tornado IOLoop API with zmq compatibility

If you have tornado ≥ 3.0, this is a subclass of tornado's IOLoop,
otherwise we ship a minimal subset of tornado in zmq.eventloop.minitornado.

The minimal shipped version of tornado's IOLoop does not include
support for concurrent futures - this will only be available if you
have tornado ≥ 3.0.
i    (   t   absolute_importt   divisiont   with_statementN(   t   Pollert   POLLINt   POLLOUTt   POLLERRt   ZMQErrort   ETERMi   (   t
   PollIOLoopt   PeriodicCallback(   t   gen_logt   DelayedCallbackc           B  s,   e  Z d  Z d d  Z d   Z d   Z RS(   s   Schedules the given callback to be called once.

    The callback is called once, after callback_time milliseconds.

    `start` must be called after the DelayedCallback is created.

    The timeout is calculated from when `start` is called.
    c         C  s?   t  j d t  t | d  } t t |   j | | |  d  S(   NsD   DelayedCallback is deprecated.
        Use loop.add_timeout instead.gMbP?(   t   warningst   warnt   DeprecationWarningt   maxt   superR   t   __init__(   t   selft   callbackt   callback_timet   io_loop(    (    s8   lib/python2.7/site-packages/zmq/eventloop/_deprecated.pyR   .   s    
c         C  sI   t  |  _ t  |  _ t j   |  j d |  _ |  j j |  j |  j  d S(   s   Starts the timer.g     @@N(	   t   Truet   _runningt	   _firstrunt   timeR   t   _next_timeoutR   t   add_timeoutt   _run(   R   (    (    s8   lib/python2.7/site-packages/zmq/eventloop/_deprecated.pyt   start5   s    		c         C  sO   |  j  s d  St |  _  y |  j   Wn$ t k
 rJ t j d d t n Xd  S(   Ns   Error in delayed callbackt   exc_info(   R   t   FalseR   t	   ExceptionR   t   errorR   (   R   (    (    s8   lib/python2.7/site-packages/zmq/eventloop/_deprecated.pyR   <   s    	 	N(   t   __name__t
   __module__t   __doc__t   NoneR   R   R   (    (    (    s8   lib/python2.7/site-packages/zmq/eventloop/_deprecated.pyR   %   s   	t	   ZMQPollerc           B  sb   e  Z d  Z d   Z e d    Z e d    Z d   Z d   Z d   Z	 d   Z
 d   Z RS(	   s   A poller that can be used in the tornado IOLoop.
    
    This simply wraps a regular zmq.Poller, scaling the timeout
    by 1000, so that it is in seconds rather than milliseconds.
    c         C  s   t    |  _ d  S(   N(   R   t   _poller(   R   (    (    s8   lib/python2.7/site-packages/zmq/eventloop/_deprecated.pyR   L   s    c         C  sX   d } |  t  j @r  | t O} n  |  t  j @r: | t O} n  |  t  j @rT | t O} n  | S(   sE   translate IOLoop.READ/WRITE/ERROR event masks into zmq.POLLIN/OUT/ERRi    (   t   IOLoopt   READR   t   WRITER   t   ERRORR   (   t   eventst   z_events(    (    s8   lib/python2.7/site-packages/zmq/eventloop/_deprecated.pyt   _map_eventsO   s    c         C  sX   d } |  t  @r  | t j O} n  |  t @r: | t j O} n  |  t @rT | t j O} n  | S(   sE   translate zmq.POLLIN/OUT/ERR event masks into IOLoop.READ/WRITE/ERRORi    (   R   R)   R*   R   R+   R   R,   (   R.   R-   (    (    s8   lib/python2.7/site-packages/zmq/eventloop/_deprecated.pyt   _remap_events[   s    


c         C  s   |  j  j | |  j |   S(   N(   R(   t   registerR/   (   R   t   fdR-   (    (    s8   lib/python2.7/site-packages/zmq/eventloop/_deprecated.pyR1   g   s    c         C  s   |  j  j | |  j |   S(   N(   R(   t   modifyR/   (   R   R2   R-   (    (    s8   lib/python2.7/site-packages/zmq/eventloop/_deprecated.pyR3   j   s    c         C  s   |  j  j |  S(   N(   R(   t
   unregister(   R   R2   (    (    s8   lib/python2.7/site-packages/zmq/eventloop/_deprecated.pyR4   m   s    c         C  sB   |  j  j d |  } g  | D]! \ } } | |  j |  f ^ q S(   so   poll in seconds rather than milliseconds.
        
        Event masks will be IOLoop.READ/WRITE/ERROR
        i  (   R(   t   pollR0   (   R   t   timeoutR.   R2   t   evt(    (    s8   lib/python2.7/site-packages/zmq/eventloop/_deprecated.pyR5   p   s    c         C  s   d  S(   N(    (   R   (    (    s8   lib/python2.7/site-packages/zmq/eventloop/_deprecated.pyt   closex   s    (   R#   R$   R%   R   t   staticmethodR/   R0   R1   R3   R4   R5   R8   (    (    (    s8   lib/python2.7/site-packages/zmq/eventloop/_deprecated.pyR'   E   s   					t	   ZMQIOLoopc           B  sG   e  Z d  Z e Z d d  Z e d    Z e d    Z	 d   Z
 RS(   sj   ZMQ subclass of tornado's IOLoop
    
    Minor modifications, so that .current/.instance return self
    c         K  s>   | d  k r |  j   n | } t t |   j d | |  d  S(   Nt   impl(   R&   t	   _zmq_implR   R:   t
   initialize(   R   R;   t   kwargs(    (    s8   lib/python2.7/site-packages/zmq/eventloop/_deprecated.pyR=      s    c         O  sd   t  d k r t j |   n  t j | |   } t | |   s` t j d |  | f t d d n  | S(   s  Returns a global `IOLoop` instance.
        
        Most applications have a single, global `IOLoop` running on the
        main thread.  Use this method to get this instance from
        another thread.  To get the current thread's `IOLoop`, use `current()`.
        i   s.   IOLoop.current expected instance of %r, got %rt
   stackleveli   (   i   (   t   tornado_versionR	   t	   configuret   instancet
   isinstanceR   R   t   RuntimeWarning(   t   clst   argsR>   t   loop(    (    s8   lib/python2.7/site-packages/zmq/eventloop/_deprecated.pyRB      s    
c         O  sd   t  d k r t j |   n  t j | |   } t | |   s` t j d |  | f t d d n  | S(   s/   Returns the current thread’s IOLoop.
        i   s.   IOLoop.current expected instance of %r, got %rR?   i   (   i   (   R@   R	   RA   t   currentRC   R   R   RD   (   RE   RF   R>   RG   (    (    s8   lib/python2.7/site-packages/zmq/eventloop/_deprecated.pyRH      s    c         C  sF   y t  t |   j   Wn( t k
 rA } | j t k r; qB   n Xd  S(   N(   R   R:   R   R   t   errnoR   (   R   t   e(    (    s8   lib/python2.7/site-packages/zmq/eventloop/_deprecated.pyR      s    N(   R#   R$   R%   R'   R<   R&   R=   t   classmethodRB   RH   R   (    (    (    s8   lib/python2.7/site-packages/zmq/eventloop/_deprecated.pyR:   |   s   i   c         C  s&   d d l  m } | j j |   |  S(   sB   backport IOLoop.close to 3.0 from 3.1 (supports fd.close() method)i    (   R	   (   t    zmq.eventloop.minitornado.ioloopR	   R8   t   __get__(   R   t   all_fdst	   mini_loop(    (    s8   lib/python2.7/site-packages/zmq/eventloop/_deprecated.pyt   backport_close   s    c          C  s|   d d l  m }  |  j j   sG |  j j   t j   k sG t d   t d k rf |  j j t  n t j   |  j _	 d S(   s4  set the tornado IOLoop instance with the pyzmq IOLoop.
    
    After calling this function, tornado's IOLoop.instance() and pyzmq's
    IOLoop.instance() will return the same object.
    
    An assertion error will be raised if tornado's IOLoop has been initialized
    prior to calling this function.
    i    (   t   ioloops"   tornado IOLoop already initializedi   N(   i   (
   t   tornadoRQ   R)   t   initializedRB   t   AssertionErrorR@   RA   R:   t	   _instance(   RQ   (    (    s8   lib/python2.7/site-packages/zmq/eventloop/_deprecated.pyt   install   s    	'(    (   i   i    (   i   i   ("   R%   t
   __future__R    R   R   t   osR   R   t   zmqR   R   R   R   R   R   RR   t   version_infoR@   t   ImportErrort   AttributeErrort   minitornado.ioloopR	   R
   t   minitornado.logR   R   t   objectR'   R:   R    RP   R8   R)   RV   (    (    (    s8   lib/python2.7/site-packages/zmq/eventloop/_deprecated.pyt   <module>
   s(   .
 79