
    &th,                     J   d Z ddlZddlZddlZddlmZmZmZmZm	Z	 ddl
mZ ddlZddlmZ ddlmZ e	rddlmZ  ej        e          Z ej        edz             Z ej        ed	z             Z G d
 de          Z G d de          ZdedededefdZdededefdZdS )z%Connection/Session management module.    N)	AwaitableCallableDictUnionTYPE_CHECKING)EventEmitter)connect)NetworkError)Optionalz.Connectionz.CDPSessionc            	            e Zd ZdZ	 ddedej        deddf fdZe	defd	            Z
dd
ZdededdfdZddededefdZdeddfdZdeddfdZdeg df         ddfdZdeddfdZddZddZdeddfdZ xZS )
ConnectionzConnection management class.r   urlloopdelayreturnNc                    t                                                       || _        d| _        t	                      | _        |dz  | _        || _        t	                      | _        |  d| _	        t          | j        d| j        dd          | _        | j                            |                                           | _        d| _        dS )zMake connection.

        :arg str url: WebSocket url to connect devtool.
        :arg int delay: delay to wait before processing received messages.
        r   i  FN)max_sizer   ping_intervalping_timeout)super__init___url_lastIddict
_callbacks_delay_loop	_sessions
_connected
ws_connect_wscreate_task
_recv_loop	_recv_fut_closeCallback)selfr   r   r   	__class__s       h/var/www/html/mycamper/aliexpress-site/backend/venv/lib/python3.11/site-packages/pyppeteer/connection.pyr   zConnection.__init__   s     		59VVdl
04#di$TZW[jnooo//0A0ABB<@    c                     | j         S )zGet connected WebSocket url.)r   r&   s    r(   r   zConnection.url0   s     yr)   c                 ,  K   | j         4 d {V }d| _        || _        | j        r	 | j                                         d {V }|r|                     |           d {V  n6# t
          j        t          f$ r t          	                    d           Y n%w xY wt          j        d           d {V  | j        	 d d d           d {V  n# 1 d {V swxY w Y   | j        r.| j                            |                                            d S d S )NTzconnection closedr   )r!   r   
connectionrecv_on_message
websocketsConnectionClosedConnectionResetErrorloggerinfoasynciosleepr   r"   dispose)r&   r-   resps      r(   r#   zConnection._recv_loop5   s     8 	' 	' 	' 	' 	' 	' 	'z"DO(DO/ '!%!5!5!7!7777777D 5"..t444444444"35IJ   KK 3444E mA&&&&&&&&& / ' '	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' ? 	3J""4<<>>22222	3 	3s4   C<A$#C$0BCB$C
CCmsgcallback_idc                   K   | j         s&t          j        | j                   d {V  | j         &	 | j                            |           d {V  d S # t          j        $ r t          	                    d           | j
                            |d           }|rF|                                s5|                    d            |                                  d {V  Y d S Y d S Y d S w xY w)Nzconnection unexpectedly closed)r   r5   r6   r   r-   sendr0   r1   r3   errorr   getdone
set_resultr7   )r&   r9   r:   callbacks       r(   _async_sendzConnection._async_sendE   s-     / 	--,,,,,,,,, / 	-	%/&&s+++++++++++* 	% 	% 	%LL9:::**;==H % %##D)))llnn$$$$$$$$$$% % % % % %	%s    A B	C&%C&methodparamsc                    | j         r| j        st          d          |t                      }| xj         dz  c_         | j         }t	          j        t          |||                    }t                              d|            | j        	                    | 
                    ||                     | j                                        }|| j        |<   t                      |_        ||_        |S )z Send message via the connection.zConnection is closedN   idrC   rD   SEND: )r   r   ConnectionErrorr   jsondumpslogger_connectiondebugr   r"   rB   create_futurer   r
   r=   rC   )r&   rC   rD   _idr9   rA   s         r(   r<   zConnection.sendQ   s     < 	: 	:!"8999>VVFlj
 
 
  
 	///
t//S99::::++--'$0NN%r)   c                 <   | j                             |                    dd                    }|                    d          r0|                    t	          |j        |j        |                     d S |                    |                    d                     d S )NrH   r=   result)r   popr>   set_exception_createProtocolErrorr=   rC   r@   )r&   r9   rA   s      r(   _on_responsezConnection._on_responseg   s    ?&&swwtR'8'899777 		3""$NO       1 122222r)   c                    |                     di           }|                     dd          }|                     d          }|dk    rH| j                             |          }|r*|                    |                     d                     d S d S |dk    r<| j                             |          }|r|                                 | j        |= d S d S |                     ||           d S )NrD   rC    	sessionId Target.receivedMessageFromTargetmessagezTarget.detachedFromTarget)r>   r   r/   
_on_closedemit)r&   r9   rD   rC   rZ   sessions         r(   	_on_queryzConnection._on_queryt   s   2&&2&&JJ{++	777n((33G ;##FJJy$9$9:::::; ;222n((33G .""$$$N9---. . IIff%%%%%r)   rA   c                     || _         dS )zSet closed callback.N)r%   )r&   rA   s     r(   setClosedCallbackzConnection.setClosedCallback   s    &r)   r\   c                 :  K   t          j        | j                   d {V  t                              d|            t          j        |          }|                    d          | j        v r| 	                    |           d S | 
                    |           d S )NRECV: rH   )r5   r6   r   rM   rN   rK   loadsr>   r   rW   r`   )r&   r\   r9   s      r(   r/   zConnection._on_message   s      mDK((((((((( 2 2 2333j!!774==DO++c"""""NN3r)   c           	      r  K   | j         r|                                   d | _         | j                                        D ]3}|                    t	          |j        d|j         d                     4| j                                         | j                                        D ]}|	                                 | j                                         t          | d          r| j                                         d {V  | j                                        s| j                                         d S d S )NProtocol error : Target closed.r-   )r%   r   valuesrU   _rewriteErrorr=   rC   clearr   r]   hasattrr-   closer$   r?   cancel)r&   cbr_   s      r(   	_on_closezConnection._on_close   sV      	'!!!"&D/((** 	 	B]=")===      	~,,.. 	! 	!G     4&& 	*/'')))))))))~""$$ 	$N!!#####	$ 	$r)   c                 L   K   d| _         |                                  d{V  dS )zClose all connection.FN)r   rp   r+   s    r(   r7   zConnection.dispose   s5      nnr)   
targetInfo
CDPSessionc                    K   |                      dd|d         i           d{V }|                    d          }t          | |d         || j                  }|| j        |<   |S )zCreate new session.zTarget.attachToTargettargetIdNrZ   type)r<   r>   rs   r   r   )r&   rr   r8   rZ   r_   s        r(   createSessionzConnection.createSession   s      YY#J/0
 
 
 
 
 
 
 
 HH[))	T:f#5y$*MM$+y!r)   )r   r   NN)__name__
__module____qualname____doc__strr5   AbstractEventLoopintr   propertyr   r#   rB   r   r   r<   rW   r`   r   rb   r/   rp   r7   r   rw   __classcell__r'   s   @r(   r   r      s       &&  A AC Aw'@ AA$(A A A A A A( S    X3 3 3 3 
%S 
%s 
%t 
% 
% 
% 
% 3  	    ,3 3 3 3 3 3&T &d & & & & '(2t8*< ' ' ' ' '           $ $ $ $,   
	d 	| 	 	 	 	 	 	 	 	r)   r   c            
            e Zd ZdZdeed f         dededej        ddf
 fdZ	dd	ed
e
defdZdeddfdZddZddZdededd fdZ xZS )rs   a|  Chrome Devtools Protocol Session.

    The :class:`CDPSession` instances are used to talk raw Chrome Devtools
    Protocol:

    * protocol methods can be called with :meth:`send` method.
    * protocol events can be subscribed to with :meth:`on` method.

    Documentation on DevTools Protocol can be found
    `here <https://chromedevtools.github.io/devtools-protocol/>`__.
    r-   
targetTyperZ   r   r   Nc                     t                                                       d| _        i | _        || _        || _        || _        t                      | _        || _	        dS )zMake new session.r   N)
r   r   r   r   _connection_targetType
_sessionIdr   r   r   )r&   r-   r   rZ   r   r'   s        r(   r   zCDPSession.__init__   sV     	571;%#04


r)   rC   rD   c                    | j         st          d| d| j         d          | xj        dz  c_        | j        }t	          j        t          |||                    }t                              d|            | j	        
                                }|| j        |<   t                      |_        ||_        	 | j                             d| j        |d           nh# t           $ r[}|| j        v rH| j        |         }| j        |= |                    t%          |j        |j        d	                              Y d
}~nd
}~ww xY w|S )zSend message to the connected session.

        :arg str method: Protocol method name.
        :arg dict params: Optional method parameters.
        zProtocol Error (z#): Session closed. Most likely the z has been closed.rF   rG   rI   zTarget.sendMessageToTarget)rZ   r\   r   N)r   r
   r   r   rK   rL   r   logger_sessionrN   r   rO   r   r=   rC   r<   r   	ExceptionrU   rj   args)r&   rC   rD   rP   r9   rA   e	_callbacks           r(   r<   zCDPSession.send   s     	76 7 7#7 7 7   	ljVFCCCDD^c^^,,,:++--'$0NN%	!!">!_A A      	 	 	do%% OC0	OC(''OF1I) )   	 s   9#C 
E'AD==Er9   c                 J   t                               d|            t          j        |          }|                    d          }|r| j                            |          }|r| j        |= |                    d          r0|                    t          |j        |j	        |                     d S |                    d          }|r-|
                                s|                    |           d S d S d S d S |                    di           }|                    d          dk    rX| j                            |                    d                    }|r(|                    |                    d	                     nf|                    d          d
k    rM|                    d          }| j                            |          }|r|                                 | j        |= |                     |                    d          |                    d                     d S )Nrd   rH   r=   rS   rD   rC   r[   rZ   r\   Target.detachFromTarget)r   rN   rK   re   r>   r   rU   rV   r=   rC   r?   r@   r   r/   r]   r^   )	r&   r9   objrP   rA   rS   rD   r_   rZ   s	            r(   r/   zCDPSession._on_message   s$   ^c^^,,,jooggdmm 	<**3//H 4OC(777## 	4**+?  , ,      !WWX..F 4 4 ++F333334 44 4 4 4 WWXr**Fwwx  $FFF.,,VZZ-D-DEE ?''

9(=(=>>>""&???"JJ{33	.,,Y77 2&&(((y1IIcggh''):):;;;;;r)   c                    K   | j         st          d          | j                             dd| j        i           d{V  dS )zDetach session from target.

        Once detached, session won't emit any events and can't be used to send
        messages.
        zConnection already closed.r   rZ   N)r   r
   r<   r   r+   s    r(   detachzCDPSession.detach  s}        	=;<<<##$=%0$/$BD D 	D 	D 	D 	D 	D 	D 	D 	D 	Dr)   c           	          | j                                         D ]3}|                    t          |j        d|j         d                     4| j                                          d | _        d S )Nrg   rh   )r   ri   rU   rj   r=   rC   rk   r   )r&   ro   s     r(   r]   zCDPSession._on_closed!  s    /((** 	 	B]=")===      	r)   c                 H    t          | ||| j                  }|| j        |<   |S ry   )rs   r   r   )r&   r   rZ   r_   s       r(   _createSessionzCDPSession._createSession*  s(    T:y$*EE$+y!r)   ry   rx   )rz   r{   r|   r}   r   r   r~   r5   r   r   r   r   r<   r/   r   r]   r   r   r   s   @r(   rs   rs      s"       
 
5\)A#B  -0059     " "3 " "	 " " " "H<s <t < < < <@	D 	D 	D 	D                 r)   rs   r=   rC   r   r   c                     d| d|d         d          }d|d         v r|d|d         d          z  }t          | |          S )NzProtocol error (z): r=   r\   data )rj   )r=   rC   r   r\   s       r(   rV   rV   0  s[    EEECL,CEEGW-s7|F+---(((r)   r\   c                     |f| _         | S ry   )r   )r=   r\   s     r(   rj   rj   8  s    EJLr)   )r}   r5   rK   loggingtypingr   r   r   r   r   pyeer   r0   websockets.legacy.clientr	   r    pyppeteer.errorsr
   r   	getLoggerrz   r3   rM   r   r   rs   r   r~   rV   rj    r)   r(   <module>r      s   , +    B B B B B B B B B B B B B B           : : : : : : ) ) ) ) ) )  		8	$	$%G%h&>?? ""8m#;<<\ \ \ \ \ \ \ \~u u u u u u u up)	 )3 )T )') ) ) ) S Y      r)   