ó
L]c           @   sè   d  Z  d d l Z d d l Z d d l m Z d d l Z d d l m Z d d l	 m
 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 e a d „  Z e j e ƒ d e e f d „  ƒ  YZ d g Z d S(   s   Python bindings for 0MQ.iÿÿÿÿN(   t   Lock(   t   Contexti   (   t	   constants(   t   AttributeSetter(   t   ENOTSUPt   LINGERt   ctx_opt_names(   t   Socket(   t   ZMQErrorc           C   s
   t  a d  S(   N(   t   Truet   _exiting(    (    (    s0   lib/python2.7/site-packages/zmq/sugar/context.pyt   _notice_atexit   s    R   c           B   s
  e  Z d  Z d Z d Z e ƒ  Z d Z e	 Z
 d Z d d „ Z d „  Z d „  Z d „  Z d d „ Z e Z e d „  ƒ Z e d „  ƒ Z e d d	 „ ƒ Z d
 „  Z d „  Z d „  Z d d „ Z e d „  ƒ Z d „  Z d „  Z d „  Z d „  Z d „  Z  d „  Z! RS(   s[   Create a zmq Context

    A zmq Context creates sockets via its ``ctx.socket`` method.
    i   c         K   s\   t  t |  ƒ j d | |  | j d t ƒ r: t |  _ n	 t |  _ i  |  _ t ƒ  |  _	 d  S(   Nt
   io_threadst   shadow(
   t   superR   t   __init__t   gett   FalseR	   t   _shadowt   sockoptst   sett   _sockets(   t   selfR   t   kwargs(    (    s0   lib/python2.7/site-packages/zmq/sugar/context.pyR   &   s    		c         C   s"   |  j  r t r |  j ƒ  n  d S(   sM   deleting a Context should terminate it, without trying non-threadsafe destroyN(   R   R
   t   term(   R   (    (    s0   lib/python2.7/site-packages/zmq/sugar/context.pyt   __del__/   s    c         C   s   |  S(   N(    (   R   (    (    s0   lib/python2.7/site-packages/zmq/sugar/context.pyt	   __enter__4   s    c         O   s   |  j  ƒ  d  S(   N(   R   (   R   t   argsR   (    (    s0   lib/python2.7/site-packages/zmq/sugar/context.pyt   __exit__7   s    c         C   s   |  j  j |  j ƒ S(   s'   Copying a Context creates a shadow copy(   t	   __class__R   t
   underlying(   R   t   memo(    (    s0   lib/python2.7/site-packages/zmq/sugar/context.pyt   __copy__:   s    c         C   s)   d d l  m } | | ƒ } |  d | ƒ S(   s¹   Shadow an existing libzmq context
        
        address is the integer address of the libzmq context
        or an FFI pointer to it.
        
        .. versionadded:: 14.1
        iÿÿÿÿ(   t   cast_int_addrR   (   t   zmq.utils.interopR!   (   t   clst   addressR!   (    (    s0   lib/python2.7/site-packages/zmq/sugar/context.pyR   @   s    	c         C   sH   d d l  m } d d l m } | j | ƒ } | | ƒ } |  d | ƒ S(   s…   Shadow an existing pyczmq context
        
        ctx is the FFI `zctx_t *` pointer
        
        .. versionadded:: 14.1
        iÿÿÿÿ(   t   zctx(   R!   R   (   t   pyczmqR%   R"   R!   R   (   R#   t   ctxR%   R!   R   R$   (    (    s0   lib/python2.7/site-packages/zmq/sugar/context.pyt   shadow_pyczmqM   s
    c         C   sž   |  j  d k s0 |  j t j ƒ  k s0 |  j  j r— |  j Y |  j  d k sj |  j t j ƒ  k sj |  j  j rŽ |  d | ƒ |  _  t j ƒ  |  _ n  Wd QXn  |  j  S(   s  Returns a global Context instance.

        Most single-threaded applications have a single, global Context.
        Use this method instead of passing around Context instances
        throughout your code.

        A common pattern for classes that depend on Contexts is to use
        a default argument to enable programs with multiple Contexts
        but not require the argument for simpler applications::

            class MyClass(object):
                def __init__(self, context=None):
                    self.context = context or Context.instance()

        .. versionchanged:: 18.1

            When called in a subprocess after forking,
            a new global instance is created instead of inheriting
            a Context that won't work from the parent process.
        R   N(   t	   _instancet   Nonet   _instance_pidt   ost   getpidt   closedt   _instance_lock(   R#   R   (    (    s0   lib/python2.7/site-packages/zmq/sugar/context.pyt   instance]   s    
c         C   s4   t  |  j ƒ } x t f D] } | j | ƒ q W| S(   N(   t   dirR   R   t   extend(   R   t   keyst
   collection(    (    s0   lib/python2.7/site-packages/zmq/sugar/context.pyt   __dir__†   s
    c         C   s#   t  j | ƒ } |  j j | ƒ | S(   N(   t   weakreft   refR   t   add(   R   t   socketR7   (    (    s0   lib/python2.7/site-packages/zmq/sugar/context.pyt   _add_socket“   s    c         C   s5   t  j | ƒ } | |  j k r1 |  j j | ƒ n  d  S(   N(   R6   R7   R   t   remove(   R   R9   R7   (    (    s0   lib/python2.7/site-packages/zmq/sugar/context.pyt
   _rm_socket˜   s    c         C   s†   |  j  r d S|  j } t ƒ  |  _ xS | D]K } | ƒ  } | r) | j  r) | d k	 rg | j t | ƒ n  | j ƒ  q) q) W|  j ƒ  d S(   sª  Close all sockets associated with this context and then terminate
        the context.

        .. warning::

            destroy involves calling ``zmq_close()``, which is **NOT** threadsafe.
            If there are active sockets in other threads, this must not be called.

        Parameters
        ----------

        linger : int, optional
            If specified, set LINGER on sockets prior to closing them.
        N(   R.   R   R   R*   t
   setsockoptR   t   closeR   (   R   t   lingert   socketst   s(    (    s0   lib/python2.7/site-packages/zmq/sugar/context.pyt   destroy   s    			c         C   s   t  S(   N(   R   (   R   (    (    s0   lib/python2.7/site-packages/zmq/sugar/context.pyt   _socket_classº   s    c         K   s†   |  j  r t t ƒ ‚ n  |  j |  | |  } xE |  j j ƒ  D]4 \ } } y | j | | ƒ Wq= t k
 rp q= Xq= W|  j | ƒ | S(   s]  Create a Socket associated with this Context.

        Parameters
        ----------
        socket_type : int
            The socket type, which can be any of the 0MQ socket types:
            REQ, REP, PUB, SUB, PAIR, DEALER, ROUTER, PULL, PUSH, etc.

        kwargs:
            will be passed to the __init__ method of the socket class.
        (   R.   R   R   RC   R   t   itemsR=   R:   (   R   t   socket_typeR   RA   t   optt   value(    (    s0   lib/python2.7/site-packages/zmq/sugar/context.pyR9   ¾   s    	c         C   s   | |  j  | <d S(   sk   set default socket options for new sockets created by this Context

        .. versionadded:: 13.0
        N(   R   (   R   RF   RG   (    (    s0   lib/python2.7/site-packages/zmq/sugar/context.pyR=   Ø   s    c         C   s   |  j  | S(   sk   get default socket options for new sockets created by this Context

        .. versionadded:: 13.0
        (   R   (   R   RF   (    (    s0   lib/python2.7/site-packages/zmq/sugar/context.pyt
   getsockoptß   s    c         C   s0   | t  j k r |  j | | ƒ S| |  j | <d S(   s"   set default sockopts as attributesN(   R   R   R   R   (   R   t   nameRF   RG   (    (    s0   lib/python2.7/site-packages/zmq/sugar/context.pyt   _set_attr_optæ   s    c         C   sI   | t  j k r |  j | ƒ S| |  j k r: t | ƒ ‚ n |  j | Sd S(   s"   get default sockopts as attributesN(   R   R   R   R   t   AttributeError(   R   RI   RF   (    (    s0   lib/python2.7/site-packages/zmq/sugar/context.pyt   _get_attr_optí   s
    c         C   so   | j  ƒ  } y t t | ƒ } Wn! t k
 rB t d | ƒ ‚ n) X| |  j k ra t | ƒ ‚ n
 |  j | =d S(   s%   delete default sockopts as attributess   no such socket option: %sN(   t   uppert   getattrR   RK   R   (   R   t   keyRF   (    (    s0   lib/python2.7/site-packages/zmq/sugar/context.pyt   __delattr__÷   s    N("   t   __name__t
   __module__t   __doc__R*   R   R)   R    R/   R+   R   R   R   R   R   R   R   R    t   __deepcopy__t   classmethodR   R(   R0   R5   R:   R<   RB   t   propertyRC   R9   R=   RH   RJ   RL   RP   (    (    (    s0   lib/python2.7/site-packages/zmq/sugar/context.pyR      s8   					(								
(   RS   t   atexitR,   t	   threadingR    R6   t   zmq.backendR   t   ContextBaset    R   t	   attrsettrR   R   R   R   R9   R   t	   zmq.errorR   R   R
   R   t   registert   __all__(    (    (    s0   lib/python2.7/site-packages/zmq/sugar/context.pyt   <module>   s   	ê