ó
o€
\c           @   s²   d  Z  d d l Z d d l Z d d l m Z d d l m Z d e f d „  ƒ  YZ d	 e f d
 „  ƒ  YZ	 d e	 f d „  ƒ  YZ
 d e	 f d „  ƒ  YZ d „  Z d d d g Z d S(   sE   Module implementing the Pool for :mod:``requests_toolbelt.threaded``.iÿÿÿÿNi   (   t   threadi   (   t   queuet   Poolc           B   s}   e  Z d  Z d
 d
 d
 e j d „ Z d „  Z e d „  ƒ Z	 e d
 d „ ƒ Z
 d „  Z d „  Z d „  Z d „  Z d	 „  Z RS(   s>  Pool that manages the threads containing sessions.

    :param queue:
        The queue you're expected to use to which you should add items.
    :type queue: queue.Queue
    :param initializer:
        Function used to initialize an instance of ``session``.
    :type initializer: collections.Callable
    :param auth_generator:
        Function used to generate new auth credentials for the session.
    :type auth_generator: collections.Callable
    :param int num_threads:
        Number of threads to create.
    :param session:
    :type session: requests.Session
    c      
   C   sÝ   | d  k r! t j ƒ  p d } n  | d k  r< t d ƒ ‚ n  | |  _ t j ƒ  |  _ t j ƒ  |  _ | |  _	 | pu t
 |  _ | p„ t
 |  _ | |  _ g  t |  j	 ƒ D]- } t j |  j ƒ  |  j |  j |  j ƒ ^ q£ |  _ d  S(   Ni   s)   Number of processes should at least be 1.(   t   Nonet   multiprocessingt	   cpu_countt
   ValueErrort
   _job_queueR   t   Queuet   _response_queuet
   _exc_queuet
   _processest	   _identityt   _initializert   _autht   _sessiont   rangeR    t   SessionThreadt   _new_sessiont   _pool(   t   selft	   job_queuet   initializert   auth_generatort   num_processest   sessiont   _(    (    sT   /data/av2000/b2b/venv/lib/python2.7/site-packages/requests_toolbelt/threaded/pool.pyt   __init__   s    			c         C   s   |  j  |  j |  j ƒ  ƒ ƒ S(   N(   R   R   R   (   R   (    (    sT   /data/av2000/b2b/venv/lib/python2.7/site-packages/requests_toolbelt/threaded/pool.pyR   0   s    c         K   s=   t  j ƒ  } x | D] } | j | j ƒ q W|  d | |  S(   s2  Create a :class:`~Pool` from an :class:`~ThreadException`\ s.

        Provided an iterable that provides :class:`~ThreadException` objects,
        this classmethod will generate a new pool to retry the requests that
        caused the exceptions.

        :param exceptions:
            Iterable that returns :class:`~ThreadException`
        :type exceptions: iterable
        :param kwargs:
            Keyword arguments passed to the :class:`~Pool` initializer.
        :returns: An initialized :class:`~Pool` object.
        :rtype: :class:`~Pool`
        R   (   R   R   t   putt   request_kwargs(   t   clst
   exceptionst   kwargsR   t   exc(    (    sT   /data/av2000/b2b/venv/lib/python2.7/site-packages/requests_toolbelt/threaded/pool.pyt   from_exceptions3   s    c         K   sz   i d d 6} | j  | p i  ƒ t j ƒ  } x; | D]3 } | j ƒ  } | j  i | d 6ƒ | j | ƒ q3 W|  d | |  S(   së  Create a :class:`~Pool` from an iterable of URLs.

        :param urls:
            Iterable that returns URLs with which we create a pool.
        :type urls: iterable
        :param dict request_kwargs:
            Dictionary of other keyword arguments to provide to the request
            method.
        :param kwargs:
            Keyword arguments passed to the :class:`~Pool` initializer.
        :returns: An initialized :class:`~Pool` object.
        :rtype: :class:`~Pool`
        t   GETt   methodt   urlR   (   t   updateR   R   t   copyR   (   R   t   urlsR   R    t   request_dictR   R%   t   job(    (    sT   /data/av2000/b2b/venv/lib/python2.7/site-packages/requests_toolbelt/threaded/pool.pyt	   from_urlsI   s    c         c   s2   x+ t  r- |  j ƒ  } | d k r% Pn  | Vq Wd S(   so   Iterate over all the exceptions in the pool.

        :returns: Generator of :class:`~ThreadException`
        N(   t   Truet   get_exceptionR   (   R   R!   (    (    sT   /data/av2000/b2b/venv/lib/python2.7/site-packages/requests_toolbelt/threaded/pool.pyR   b   s
    	c         C   sB   y |  j  j ƒ  \ } } Wn t j k
 r0 d SXt | | ƒ Sd S(   sS   Get an exception from the pool.

        :rtype: :class:`~ThreadException`
        N(   R
   t
   get_nowaitR   t   EmptyR   t   ThreadException(   R   t   requestR!   (    (    sT   /data/av2000/b2b/venv/lib/python2.7/site-packages/requests_toolbelt/threaded/pool.pyR-   m   s
    c         C   sB   y |  j  j ƒ  \ } } Wn t j k
 r0 d SXt | | ƒ Sd S(   sP   Get a response from the pool.

        :rtype: :class:`~ThreadResponse`
        N(   R	   R.   R   R/   R   t   ThreadResponse(   R   R1   t   response(    (    sT   /data/av2000/b2b/venv/lib/python2.7/site-packages/requests_toolbelt/threaded/pool.pyt   get_responsey   s
    c         c   s2   x+ t  r- |  j ƒ  } | d k r% Pn  | Vq Wd S(   sm   Iterate over all the responses in the pool.

        :returns: Generator of :class:`~ThreadResponse`
        N(   R,   R4   R   (   R   t   resp(    (    sT   /data/av2000/b2b/venv/lib/python2.7/site-packages/requests_toolbelt/threaded/pool.pyt	   responses…   s
    	c         C   s"   x |  j  D] } | j ƒ  q
 Wd S(   s*   Join all the threads to the master thread.N(   R   t   join(   R   t   session_thread(    (    sT   /data/av2000/b2b/venv/lib/python2.7/site-packages/requests_toolbelt/threaded/pool.pyt   join_all   s    N(   t   __name__t
   __module__t   __doc__R   t   requestst   SessionR   R   t   classmethodR"   R+   R   R-   R4   R6   R9   (    (    (    sT   /data/av2000/b2b/venv/lib/python2.7/site-packages/requests_toolbelt/threaded/pool.pyR   	   s   					t   ThreadProxyc           B   s   e  Z d Z d  „  Z RS(   c         C   sH   t  j } | |  j k r7 | |  |  j ƒ } t | | ƒ S| |  | ƒ Sd S(   s/   Proxy attribute accesses to the proxied object.N(   t   objectt   __getattribute__t   attrst   proxied_attrt   getattr(   R   t   attrt   getR3   (    (    sT   /data/av2000/b2b/venv/lib/python2.7/site-packages/requests_toolbelt/threaded/pool.pyt   __getattr__™   s
    	N(   R:   R;   R   RD   RH   (    (    (    sT   /data/av2000/b2b/venv/lib/python2.7/site-packages/requests_toolbelt/threaded/pool.pyR@   –   s   R2   c           B   s/   e  Z d  Z d Z e d d g ƒ Z d „  Z RS(   s>  A wrapper around a requests Response object.

    This will proxy most attribute access actions to the Response object. For
    example, if you wanted the parsed JSON from the response, you might do:

    .. code-block:: python

        thread_response = pool.get_response()
        json = thread_response.json()

    R3   R   c         C   s   | |  _  | |  _ d  S(   N(   R   R3   (   R   R   R3   (    (    sT   /data/av2000/b2b/venv/lib/python2.7/site-packages/requests_toolbelt/threaded/pool.pyR   ²   s    	(   R:   R;   R<   RD   t	   frozensetRC   R   (    (    (    sT   /data/av2000/b2b/venv/lib/python2.7/site-packages/requests_toolbelt/threaded/pool.pyR2   £   s   R0   c           B   s/   e  Z d  Z d Z e d d g ƒ Z d „  Z RS(   s=  A wrapper around an exception raised during a request.

    This will proxy most attribute access actions to the exception object. For
    example, if you wanted the message from the exception, you might do:

    .. code-block:: python

        thread_exc = pool.get_exception()
        msg = thread_exc.message

    t	   exceptionR   c         C   s   | |  _  | |  _ d  S(   N(   R   RJ   (   R   R   RJ   (    (    sT   /data/av2000/b2b/venv/lib/python2.7/site-packages/requests_toolbelt/threaded/pool.pyR   È   s    	(   R:   R;   R<   RD   RI   RC   R   (    (    (    sT   /data/av2000/b2b/venv/lib/python2.7/site-packages/requests_toolbelt/threaded/pool.pyR0   ¹   s   c         C   s   |  S(   N(    (   t   session_obj(    (    sT   /data/av2000/b2b/venv/lib/python2.7/site-packages/requests_toolbelt/threaded/pool.pyR   Ï   s    (   R<   R   R=   t    R    t   _compatR   RA   R   R@   R2   R0   R   t   __all__(    (    (    sT   /data/av2000/b2b/venv/lib/python2.7/site-packages/requests_toolbelt/threaded/pool.pyt   <module>   s   	