o
    Vh                     @   s   d Z ddlZddlZddlmZ ddlmZ G dd deZG d	d
 d
eZ	G dd de	Z
G dd de	Zdd Zg dZdS )zEModule implementing the Pool for :mod:``requests_toolbelt.threaded``.    N   )thread   )queuec                   @   sn   e Zd ZdZdddejfddZdd Zedd Z	edd	d
Z
dd Zdd Zdd Zdd Zdd ZdS )Poola>  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_process:
        Number of threads to create.
    :param session:
    :type session: requests.Session
    Nc                    s|   |d u r
t  p	d}|dk rtd| _t  _t  _| _|p%t	 _
|p*t	 _| _ fddt jD  _d S )Nr   z)Number of processes should at least be 1.c                    s&   g | ]}t    j j jqS  )r   SessionThread_new_session
_job_queue_response_queue
_exc_queue).0_selfr   s/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/requests_toolbelt/threaded/pool.py
<listcomp>*   s    z!Pool.__init__.<locals>.<listcomp>)multiprocessing	cpu_count
ValueErrorr
   r   Queuer   r   
_processes	_identity_initializer_auth_sessionrange_pool)r   	job_queueinitializerauth_generatornum_processessessionr   r   r   __init__   s   




zPool.__init__c                 C   s   |  | |  S N)r   r   r   r   r   r   r   r	   0   s   zPool._new_sessionc                 K   s0   t  }|D ]}||j q| dd|i|S )a2  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   Nr   )r   r   putrequest_kwargs)cls
exceptionskwargsr   excr   r   r   from_exceptions3   s   zPool.from_exceptionsc                 K   sZ   ddi}| |p	i  t }|D ]}| }| d|i || q| dd|i|S )a  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`
        methodGETurlr   Nr   )updater   r   copyr%   )r'   urlsr&   r)   request_dictr   r.   jobr   r   r   	from_urlsI   s   zPool.from_urlsc                 c        	 |   }|du rdS |V  q)zoIterate over all the exceptions in the pool.

        :returns: Generator of :class:`~ThreadException`
        TN)get_exception)r   r*   r   r   r   r(   b      zPool.exceptionsc                 C   4   z	| j  \}}W n tjy   Y dS w t||S )zSGet an exception from the pool.

        :rtype: :class:`~ThreadException`
        N)r   
get_nowaitr   EmptyThreadException)r   requestr*   r   r   r   r6   m      
zPool.get_exceptionc                 C   r8   )zPGet a response from the pool.

        :rtype: :class:`~ThreadResponse`
        N)r   r9   r   r:   ThreadResponse)r   r<   responser   r   r   get_responsey   r=   zPool.get_responsec                 c   r5   )zmIterate over all the responses in the pool.

        :returns: Generator of :class:`~ThreadResponse`
        TN)r@   )r   respr   r   r   	responses   r7   zPool.responsesc                 C   s   | j D ]}|  qdS )z*Join all the threads to the master thread.N)r   join)r   session_threadr   r   r   join_all   s   

zPool.join_allr$   )__name__
__module____qualname____doc__requestsSessionr#   r	   classmethodr+   r4   r(   r6   r@   rB   rE   r   r   r   r   r   	   s    

r   c                   @   s   e Zd ZdZdd ZdS )ThreadProxyNc                 C   s0   t j}|| jvr|| | j}t||S || |S )z/Proxy attribute accesses to the proxied object.)object__getattribute__attrsproxied_attrgetattr)r   attrgetr?   r   r   r   __getattr__   s
   


zThreadProxy.__getattr__)rF   rG   rH   rQ   rU   r   r   r   r   rM      s    rM   c                   @   (   e Zd ZdZdZeddgZdd ZdS )r>   a>  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()

    r?   r&   c                 C      || _ || _d S r$   )r&   r?   )r   r&   r?   r   r   r   r#         
zThreadResponse.__init__NrF   rG   rH   rI   rQ   	frozensetrP   r#   r   r   r   r   r>      
    r>   c                   @   rV   )r;   a=  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

    	exceptionr&   c                 C   rW   r$   )r&   r\   )r   r&   r\   r   r   r   r#      rX   zThreadException.__init__NrY   r   r   r   r   r;      r[   r;   c                 C   s   | S r$   r   )session_objr   r   r   r      s   r   )r;   r>   r   )rI   r   rJ    r   _compatr   rN   r   rM   r>   r;   r   __all__r   r   r   r   <module>   s     