ó
L]c           @   s   d  Z  d Z d d l m Z d d l Z d d l m Z d e f d „  ƒ  YZ d	 e f d
 „  ƒ  YZ	 d e f d „  ƒ  YZ
 d „  Z d „  Z d S(   si  Decorators for running functions with context/sockets.

.. versionadded:: 15.3

Like using Contexts and Sockets as context managers, but with decorator syntax.
Context and sockets are closed at the end of the function.

For example::

    from zmq.decorators import context, socket
    
    @context()
    @socket(zmq.PUSH)
    def work(ctx, push):
        ...
t   contextt   socketiÿÿÿÿ(   t   wrapsN(   t
   basestringt
   _Decoratorc           B   s5   e  Z d  Z d d „ Z d „  Z d „  Z d „  Z RS(   s   The mini decorator factoryc         C   s   | |  _  d  S(   N(   t   _target(   t   selft   target(    (    s-   lib/python2.7/site-packages/zmq/decorators.pyt   __init__#   s    c            s7   ˆ j  ˆ  ˆ Ž  \ ‰ ‰  ‰ ‡  ‡ ‡ ‡ f d †  } | S(   sÆ  
        The main logic of decorator

        Here is how those arguments works::

            @out_decorator(*dec_args, *dec_kwargs)
            def func(*wrap_args, **wrap_kwargs):
                ...

        And in the ``wrapper``, we simply create ``self.target`` instance via
        ``with``::
            
            target = self.get_target(*args, **kwargs)
            with target(*dec_args, **dec_kwargs) as obj:
                ...

        c            s+   t  ˆ  ƒ ‡ ‡ ‡  ‡ ‡ f d †  ƒ } | S(   Nc             s—   ˆ j  |  | Ž  } | ˆ  ˆ Ž  p } ˆ rC ˆ | k rC | | ˆ <n= ˆ rs ˆ | k rs t d j ˆ j ˆ ƒ ƒ ‚ n |  | f }  ˆ |  | Ž  SWd  QXd  S(   Ns,   {0}() got multiple values for argument '{1}'(   t
   get_targett	   TypeErrort   formatt   __name__(   t   argst   kwargsR   t   obj(   t   dec_argst
   dec_kwargst   funct   kw_nameR   (    s-   lib/python2.7/site-packages/zmq/decorators.pyt   wrapper;   s    (   R   (   R   R   (   R   R   R   R   (   R   s-   lib/python2.7/site-packages/zmq/decorators.pyt	   decorator:   s    '(   t   process_decorator_args(   R   R   R   R   (    (   R   R   R   R   s-   lib/python2.7/site-packages/zmq/decorators.pyt   __call__&   s    c         O   s   |  j  S(   s_   Return the target function
        
        Allows modifying args/kwargs to be passed.
        (   R   (   R   R   R   (    (    s-   lib/python2.7/site-packages/zmq/decorators.pyR	   Q   s    c         O   sy   d } t | j d ƒ t ƒ r0 | j d ƒ } n< t | ƒ d k rl t | d t ƒ rl | d } | d } n  | | | f S(   s­   Process args passed to the decorator.
        
        args not consumed by the decorator will be passed to the target factory
        (Context/Socket constructor).
        t   namei   i    N(   t   Nonet
   isinstancet   getR   t   popt   len(   R   R   R   R   (    (    s-   lib/python2.7/site-packages/zmq/decorators.pyR   X   s    %
N(   R   t
   __module__t   __doc__R   R   R   R	   R   (    (    (    s-   lib/python2.7/site-packages/zmq/decorators.pyR       s
   	+	t   _ContextDecoratorc           B   s   e  Z d  Z d „  Z RS(   s   Decorator subclass for Contextsc         C   s   t  t |  ƒ j t j ƒ d  S(   N(   t   superR    R   t   zmqt   Context(   R   (    (    s-   lib/python2.7/site-packages/zmq/decorators.pyR   k   s    (   R   R   R   R   (    (    (    s-   lib/python2.7/site-packages/zmq/decorators.pyR    i   s   t   _SocketDecoratorc           B   s)   e  Z d  Z d „  Z d „  Z d „  Z RS(   sN   Decorator subclass for sockets
    
    Gets the context from other args.
    c         O   sF   t  t |  ƒ j | | Ž  \ } } } | j d d ƒ |  _ | | | f S(   s$   Also grab context_name out of kwargst   context_nameR    (   R!   R$   R   R   R%   (   R   R   R   R   (    (    s-   lib/python2.7/site-packages/zmq/decorators.pyR   u   s    $c         O   s   |  j  | | Ž  } | j S(   s$   Get context, based on call-time args(   t   _get_contextR   (   R   R   R   R    (    (    s-   lib/python2.7/site-packages/zmq/decorators.pyR	   {   s    c         O   si   |  j  | k r5 | |  j  } t | t j ƒ r5 | Sn  x$ | D] } t | t j ƒ r< | Sq< Wt j j ƒ  S(   s¯  
        Find the ``zmq.Context`` from ``args`` and ``kwargs`` at call time.

        First, if there is an keyword argument named ``context`` and it is a
        ``zmq.Context`` instance , we will take it.

        Second, we check all the ``args``, take the first ``zmq.Context``
        instance.

        Finally, we will provide default Context -- ``zmq.Context.instance``

        :return: a ``zmq.Context`` instance
        (   R%   R   R"   R#   t   instance(   R   R   R   t   ctxt   arg(    (    s-   lib/python2.7/site-packages/zmq/decorators.pyR&   €   s    (   R   R   R   R   R	   R&   (    (    (    s-   lib/python2.7/site-packages/zmq/decorators.pyR$   o   s   		c          O   s   t  ƒ  |  | Ž  S(   så   Decorator for adding a Context to a function.
    
    Usage::
    
        @context()
        def foo(ctx):
            ...

    .. versionadded:: 15.3

    :param str name: the keyword argument passed to decorated function
    (   R    (   R   R   (    (    s-   lib/python2.7/site-packages/zmq/decorators.pyR    ›   s    c          O   s   t  ƒ  |  | Ž  S(   s_  Decorator for adding a socket to a function.
    
    Usage::
    
        @socket(zmq.PUSH)
        def foo(push):
            ...
    
    .. versionadded:: 15.3

    :param str name: the keyword argument passed to decorated function
    :param str context_name: the keyword only argument to identify context
                             object
    (   R$   (   R   R   (    (    s-   lib/python2.7/site-packages/zmq/decorators.pyR   «   s    (   R    R   (   R   t   __all__t	   functoolsR   R"   t   zmq.utils.strtypesR   t   objectR   R    R$   R    R   (    (    (    s-   lib/python2.7/site-packages/zmq/decorators.pyt   <module>   s    I,	