
    h.                        U d dl Z d dlZd dlZd dlmZ d dlmZmZ d dlm	Z	 ddl
mZmZ ddlmZmZ ddlmZmZmZ dd	lmZ d
gZej        dk    r$ e	            Ze	ed<   dZde j        de j        fdZnde j        de j        fdZd ZdZdZdZ dZ!dZ"dZ#dZ$dZ%dZ&dZ'd Z(dZ)dZ*	  ej+                    de,dede,fd            Z- ej+                    de,de,dedede,f
d            Z. G d d
          Z/dS )    N)suppress)AnyOptional)WeakKeyDictionary   )ffilib)	CurlECodeCurlMOpt)DEFAULT_CACERTCurl	CurlError)CurlCffiWarning	AsyncCurlwin32
_selectorsa  
    Proactor event loop does not implement add_reader family of methods required.
    Registering an additional selector thread for add_reader support.
    To avoid this warning use:
        asyncio.set_event_loop_policy(WindowsSelectorEventLoopPolicy())
    asyncio_loopreturnc           
      @     t           v rt                     S t           t          t          dt	          d                              s S t          j        t          t          d           ddl	m
}  |           xt            <    j         fd}| _        S )zGet selector-compatible loop

        Returns an object with ``add_reader`` family of methods,
        either the loop itself or a SelectorThread instance.

        Workaround Windows proactor removal of *reader methods.
        ProactorEventLoopN   
stacklevelr   )AddThreadSelectorEventLoopc                  t     _         t                               d                                              d S N)closer   pop)r   
loop_closeselector_loops   K/var/www/html/marketdash/venv/lib/python3.11/site-packages/curl_cffi/aio.py_close_selector_and_loopz.get_selector.<locals>._close_selector_and_loop7   s9     ",LNN<...!!!!!    )r   
isinstancegetattrasynciotypewarningswarnPROACTOR_WARNINGr   _asyncio_selectorr   r   )r   r   r"   r   r    s   `  @@r!   get_selectorr,      s     :%%l++''+>T

KK
 
 	   &AFFFFAAAAAA3M3M4
 4
 	

<0
 "'
	" 	" 	" 	" 	" 	" 	" 6r#   loopc                     | S r    )r-   s    r!   r,   r,   C   s    r#   r         
timeout_msclientpc                     t          j        |          }|j        r |j                                         d|_        |j                            |dz  |j        t          t                    |_        dS )zD
    see: https://curl.se/libcurl/c/CURLMOPT_TIMERFUNCTION.html
    Ni  r   )	r   from_handle_timercancelr-   
call_laterprocess_dataCURL_SOCKET_TIMEOUTCURL_POLL_NONE)curlmr3   r4   
async_curls       r!   timer_functionr?   u   st    
 ))J  !  """ 
 #22T	 J 1r#   sockfdwhatdatac                    t          j        |          }|j        }||j        v r*|                    |           |                    |           |t          z  r<|                    ||j        |t                     |j        
                    |           |t          z  r<|                    ||j        |t                     |j        
                    |           |t          k    r|j                            |           dS )z[This callback is called when libcurl decides it's time to interact with certain
    socketsr   )r   r6   r-   _sockfdsremove_readerremove_writerCURL_POLL_IN
add_readerr:   CURL_CSELECT_INaddCURL_POLL_OUT
add_writerCURL_CSELECT_OUTCURL_POLL_REMOVEremove)curlr@   rA   r4   rB   r>   r-   s          r!   socket_functionrQ      s    
 ))J?D $$$6"""6""" l (
 7QQQ''' m (
 7AQRRR''' ""6***1r#   c                       e Zd ZdZddefdZd Zd Zd Zd	e	fd
Z
dededefdZdedefdZd	e	fdZd	e	fdZd	e	fdZd	e	fdZdedefdZd ZdS )r   zhWrapper around curl_multi handle to provide asyncio support. It uses the libcurl
    socket_action APIs. Ncacertc                 ~   t          j                    | _        |pt          | _        i | _        i | _        t                      | _        t          ||nt          j                              | _        | j                            |                                           | _        d| _        |                                  dS )z
        Parameters:
            cacert: CA cert path to use, by default, certs from ``certifi`` are used.
            loop: EventLoop to use.
        N)r	   curl_multi_init_curlmr   _cacert_curl2future
_curl2curlsetrD   r,   r&   get_running_loopr-   create_task_force_timeout_timeout_checkerr7   _setup)selfrT   r-   s      r!   __init__zAsyncCurl.__init__   s     )++/8:13"%%% $DD'*B*D*D
 
	 !%	 5 5d6I6I6K6K L L59r#   c                 t   |                      t          j        t          j                   |                      t          j        t          j                   t          j        |           | _	        |                      t          j
        | j	                   |                      t          j        | j	                   d S r   )setoptr   TIMERFUNCTIONr	   r?   SOCKETFUNCTIONrQ   r   
new_handle_self_handle
SOCKETDATA	TIMERDATAra   s    r!   r`   zAsyncCurl._setup   s    H*C,>???H+S-@AAAN400H'):;;;H&(9:::::r#   c                   K   | j                                          t          t          j                  5  | j          d{V  ddd           n# 1 swxY w Y   | j                                        D ]a\  }}t          j        | j	        |j
                   |                                s)|                                s|                    d           bt          j        | j	                   d| _	        | j        D ]6}| j                            |           | j                            |           7| j        r| j                                         dS dS )z?Close and cleanup running timers, readers, writers and handles.N)r_   r8   r   r&   CancelledErrorrY   itemsr	   curl_multi_remove_handlerW   _curldone	cancelled
set_resultcurl_multi_cleanuprD   r-   rE   rF   r7   )ra   rP   futurer@   s       r!   r   zAsyncCurl.close   s      	$$&&&g,-- 	( 	(''''''''	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( !-3355 	( 	(LD&(djAAA;;== ()9)9);); (!!$''' 	t{+++ m 	, 	,FI##F+++I##F++++ ; 	!K     	! 	!s   AAAc                    K   	 | j         sdS |                     t          t                     t	          j        d           d{V  D)zpThis coroutine is used to safeguard from any missing signals from curl, and
        put everything back on trackTg?N)rW   socket_actionr;   r<   r&   sleeprk   s    r!   r^   zAsyncCurl._force_timeout   sX      	%; 2NCCC-$$$$$$$$$		%r#   rP   c                     |                                  t          j        | j        |j                  }|                     |           | j                                        }|| j        |<   || j	        |j        <   |S )znAdd a curl handle to be managed by curl_multi. This is the equivalent of
        `perform` in the async world.)
_ensure_cacertr	   curl_multi_add_handlerW   rp   _check_errorr-   create_futurerY   rZ   )ra   rP   errcoderu   s       r!   
add_handlezAsyncCurl.add_handle   ss     	+DKDD'"""((**"($&*
#r#   r@   
ev_bitmaskr   c                     t          j        d          }t          j        | j        |||          }|                     |           |d         S )zYwrapper for curl_multi_socket_action,
        returns the number of running curl handles.int *r   )r   newr	   curl_multi_socket_actionrW   r|   )ra   r@   r   running_handler~   s        r!   rw   zAsyncCurl.socket_action   sQ     )).K^
 
 	'"""a  r#   c                 `   | j         st          j        dt          d           dS |                     ||           t          j        d          }	 	 t          j        | j         |          }|t
          j	        k    rdS |j
        t          k    re| j        |j                 }|j        j        }|dk    r|                     |           n:|                     ||                    |d                     nt'          d	           n,# t(          $ r t          j        d
t          d           Y nw xY w)z8Call curl_multi_info_read to read data for given socket.z0Curlm already closed! quitting from process_datar   r   Nr   Tr   performzNOT DONEzLUnexpected curl multi state in process_data, please open an issue on GitHub
)rW   r(   r)   r   rw   r   r   r	   curl_multi_info_readNULLmsgCURLMSG_DONErZ   easy_handlerB   resultrs   set_exception
_get_errorprint	Exception)ra   r@   r   msg_in_queuecurl_msgrP   retcodes          r!   r:   zAsyncCurl.process_data  sR   { 	MB   
 F6:...ww''	3DKNNsx''E<<//?8+?@D&m2G!||----**4)1T1TUUUU*%%%   7# 	     	s   *D >BD &D,+D,c                     t          j        | j        |j                  }|                     |           | j                            |j        d            | j                            |d           S r   )r	   ro   rW   rp   r|   rZ   r   rY   )ra   rP   r~   s      r!   _pop_futurezAsyncCurl._pop_future'  s_    .t{DJGG'"""DJ--- $$T4000r#   c                     |                      |          }|r>|                                s,|                                s|                                 dS dS dS dS )z&Cancel a future for given curl handle.N)r   rq   rr   r8   ra   rP   ru   s      r!   remove_handlezAsyncCurl.remove_handle-  sn    !!$'' 	&++-- 	0@0@0B0B 	MMOOOOO	 	 	 	 	 	r#   c                     |                      |          }|r?|                                s-|                                s|                    d           dS dS dS dS )z,Mark a future as done for given curl handle.N)r   rq   rr   rs   r   s      r!   rs   zAsyncCurl.set_result3  sw    !!$'' 	$&++-- 	$0@0@0B0B 	$d#####	$ 	$ 	$ 	$ 	$ 	$r#   c                     |                      |          }|r?|                                s-|                                s|                    |           dS dS dS dS )z2Raise exception of a future for given curl handle.N)r   rq   rr   r   )ra   rP   	exceptionru   s       r!   r   zAsyncCurl.set_exception9  sw    !!$'' 	,&++-- 	,0@0@0B0B 	,  +++++	, 	, 	, 	, 	, 	,r#   r~   argsc           	          |t           j        k    rd S t          j        |          }d                    d |D                       }t          d| d| d| d          )N c                 ,    g | ]}t          |          S r/   )str).0as     r!   
<listcomp>z*AsyncCurl._check_error.<locals>.<listcomp>C  s    000a3q66000r#   z
Failed in z
, multi: (z) z. See https://curl.se/libcurl/c/libcurl-errors.html first for more details. Please open an issue on GitHub to help debug this error.)r
   OKr	   curl_multi_strerrorjoinr   )ra   r~   r   errmsgactions        r!   r|   zAsyncCurl._check_error?  s    il""F(1100400011P P P7 P Pf P P P
 
 	
r#   c                     |t           j        t           j        t           j        t           j        t           j        t           j        fv rt          j        d|          }n|}t          j
        | j        ||          S )z!Wrapper around curl_multi_setopt.zlong*)r   
PIPELININGMAXCONNECTSMAX_HOST_CONNECTIONSMAX_PIPELINE_LENGTHMAX_TOTAL_CONNECTIONSMAX_CONCURRENT_STREAMSr   r   r	   curl_multi_setoptrW   )ra   optionvaluec_values       r!   rd   zAsyncCurl.setoptJ  si     )(*+
 
 
 ggu--GGG$T[&'BBBr#   )rS   N)__name__
__module____qualname____doc__r   rb   r`   r   r^   r   r   intrw   r:   r   r   rs   r   r   r|   rd   r/   r#   r!   r   r      st         s    $; ; ;! ! !6% % %
t 
 
 
 
!C !S !S ! ! ! !"3 "C " " " "H1 1 1 1 1$    $t $ $ $ $,$ , , , ,	
C 	
 	
 	
 	
 	
C C C C Cr#   )0r&   sysr(   
contextlibr   typingr   r   weakrefr   _wrapperr   r	   constr
   r   rP   r   r   r   utilsr   __all__platformr   __annotations__r*   AbstractEventLoopr,   r<   rG   rK   CURL_POLL_INOUTrN   r;   CURL_SOCKET_BADrI   rM   CURL_CSELECT_ERRr   CURLPIPE_NOTHINGCURLPIPE_HTTP1CURLPIPE_MULTIPLEX
def_externr   r?   rQ   r   r/   r#   r!   <module>r      ss    



                        % % % % % %         & & & & & & & & 1 1 1 1 1 1 1 1 1 1 " " " " " "-<7$5$5$7$7J!777&/&		"& & & & &T74 9R           4 c C C    . # S 3 c c    :lC lC lC lC lC lC lC lC lC lCr#   