a
    >xd,                     @   s   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 e	rhddlmZ eeZeed Zeed ZG d	d
 d
eZG dd deZeeeedddZeeedddZdS )z%Connection/Session management module.    N)	AwaitableCallableDictUnionTYPE_CHECKING)EventEmitter)NetworkError)Optionalz.Connectionz.CDPSessionc                       s   e Zd ZdZd%eejedd fddZe	eddd	Z
ddd
dZeeddddZd&eeedddZeddddZeddddZeg df ddddZeddddZddddZdddd Zed!d"d#d$Z  ZS )'
ConnectionzConnection management class.r   N)urlloopdelayreturnc                    st   t    || _d| _t | _|d | _|| _t | _|  d| _	t
jj| jd| j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   )super__init___url_lastIddict
_callbacks_delay_loop	_sessions
_connected
websocketsclientconnect_wscreate_task
_recv_loop	_recv_fut_closeCallback)selfr   r   r   	__class__ L/var/www/html/Ranjet/env/lib/python3.9/site-packages/pyppeteer/connection.pyr      s    


zConnection.__init__r   c                 C   s   | j S )zGet connected WebSocket url.)r   r"   r%   r%   r&   r   0   s    zConnection.urlc              
      s   | j 4 I d H }d| _|| _| jrz(| j I d H }|rH| |I d H  W n& tjtfyp   t	d Y qY n0 t
dI d H  qW d   I d H  q1 I d H s0    Y  | jr| j|   d S )NTzconnection closedr   )r   r   
connectionrecv_on_messager   ConnectionClosedConnectionResetErrorloggerinfoasynciosleepr   r   dispose)r"   r)   respr%   r%   r&   r   5   s    

<zConnection._recv_loop)msgcallback_idr   c                    s   | j st| jI d H  q z| j|I d H  W nP tjy   t	d | j
|d }|r|| s||d  |  I d H  Y n0 d S )Nzconnection unexpectedly closed)r   r0   r1   r   r)   sendr   r,   r.   errorr   getdone
set_resultr2   )r"   r4   r5   callbackr%   r%   r&   _async_sendE   s    

zConnection._async_sendmethodparamsr   c                 C   s   | j r| jstd|du r"t }|  j d7  _ | j }tt|||d}td|  | j	| 
|| | j }|| j|< t |_||_|S )z Send message via the connection.zConnection is closedN   idr>   r?   SEND: )r   r   ConnectionErrorr   jsondumpslogger_connectiondebugr   r   r<   create_futurer   r   r7   r>   )r"   r>   r?   _idr4   r;   r%   r%   r&   r6   Q   s$    

zConnection.sendr4   r   c                 C   sJ   | j |dd}|dr6|t|j|j| n||d d S )NrB   r7   result)r   popr8   set_exception_createProtocolErrorr7   r>   r:   )r"   r4   r;   r%   r%   r&   _on_responseg   s    
zConnection._on_responsec                 C   s   | di }| dd}| d}|dkrL| j |}|r|| d n6|dkrv| j |}|r|  | j|= n| || d S )Nr?   r>    	sessionId Target.receivedMessageFromTargetmessagezTarget.detachedFromTarget)r8   r   r+   
_on_closedemit)r"   r4   r?   r>   rS   sessionr%   r%   r&   	_on_queryt   s    

zConnection._on_query)r;   r   c                 C   s
   || _ dS )zSet closed callback.N)r!   )r"   r;   r%   r%   r&   setClosedCallback   s    zConnection.setClosedCallback)rU   r   c                    sV   t | jI d H  td|  t|}|d| jv rH| 	| n
| 
| d S )NRECV: rB   )r0   r1   r   rG   rH   rE   loadsr8   r   rQ   rY   )r"   rU   r4   r%   r%   r&   r+      s    
zConnection._on_messagec                    s   | j r|    d | _ | j D ] }|t|jd|j d q| j  | j D ]}|	  qT| j  t
| dr| j I d H  | j s| j  d S )NProtocol error : Target closed.r)   )r!   r   valuesrO   _rewriteErrorr7   r>   clearr   rV   hasattrr)   closer    r9   cancel)r"   cbrX   r%   r%   r&   	_on_close   s     




zConnection._on_closec                    s   d| _ |  I dH  dS )zClose all connection.FN)r   rf   r(   r%   r%   r&   r2      s    zConnection.dispose
CDPSession)
targetInfor   c                    sF   |  dd|d iI dH }|d}t| |d || j}|| j|< |S )zCreate new session.zTarget.attachToTargetZtargetIdNrS   type)r6   r8   rg   r   r   )r"   rh   r3   rS   rX   r%   r%   r&   createSession   s    



zConnection.createSession)r   )N)__name__
__module____qualname____doc__strr0   AbstractEventLoopintr   propertyr   r   r<   r   r   r6   rQ   rY   r   rZ   r+   rf   r2   r   rj   __classcell__r%   r%   r#   r&   r
      s"    	r
   c                       s   e Zd ZdZeed f eeejdd fddZ	dee
edddZedd	d
dZddddZddddZeed dddZ  ZS )rg   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/>`_.
    N)r)   
targetTyperS   r   r   c                    s:   t    d| _i | _|| _|| _|| _t | _|| _	dS )zMake new session.r   N)
r   r   r   r   _connection_targetType
_sessionIdr   r   r   )r"   r)   rt   rS   r   r#   r%   r&   r      s    
zCDPSession.__init__r=   c              
   C   s   | j std| d| j d|  jd7  _| j}tt|||d}td|  | j	
 }|| j|< t |_||_z| j d| j|d W nX ty } z@|| jv r| j|= | j| }|t|j|jd	  W Y d
}~n
d
}~0 0 |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.r@   rA   rC   zTarget.sendMessageToTarget)rS   rU   r   N)ru   r   rv   r   rE   rF   r   logger_sessionrH   r   rI   r   r7   r>   r6   rw   	ExceptionrO   r`   args)r"   r>   r?   rJ   r4   r;   e	_callbackr%   r%   r&   r6      s8    



zCDPSession.sendrK   c           	      C   s  t d|  t|}|d}|r| j|}|r| j|= |drb|t|j|j	| n |d}|r|
 s|| n|di }|ddkr| j|d}|r||d	 n8|dd
kr|d}| j|}|r|  | j|= | |d|d d S )Nr[   rB   r7   rM   r?   r>   rT   rS   rU   Target.detachFromTarget)rx   rH   rE   r\   r8   r   rO   rP   r7   r>   r9   r:   r   r+   rV   rW   )	r"   r4   objrJ   r;   rM   r?   rX   rS   r%   r%   r&   r+      s8    




zCDPSession._on_messager'   c                    s,   | j std| j dd| jiI dH  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}   rS   N)ru   r   r6   rw   r(   r%   r%   r&   detach  s
    zCDPSession.detachc                 C   s@   | j  D ] }|t|jd|j d q
| j   d | _d S )Nr]   r^   )r   r_   rO   r`   r7   r>   ra   ru   )r"   re   r%   r%   r&   rV   !  s    
zCDPSession._on_closed)rt   rS   r   c                 C   s   t | ||| j}|| j|< |S N)rg   r   r   )r"   rt   rS   rX   r%   r%   r&   _createSession*  s    
zCDPSession._createSession)N)rk   rl   rm   rn   r   r
   ro   r0   rp   r   r   r   r6   r+   r   rV   r   rs   r%   r%   r#   r&   rg      s   
$ 	rg   )r7   r>   r~   r   c                 C   sD   d| d|d d  }d|d v r:|d|d d  7 }t | |S )NzProtocol error (z): r7   rU   data )r`   )r7   r>   r~   rU   r%   r%   r&   rP   0  s    rP   )r7   rU   r   c                 C   s   |f| _ | S r   )rz   )r7   rU   r%   r%   r&   r`   8  s    r`   )rn   r0   rE   loggingtypingr   r   r   r   r   Zpyeer   r   Zpyppeteer.errorsr   r	   	getLoggerrk   r.   rG   rx   r
   rg   ry   ro   rP   r`   r%   r%   r%   r&   <module>   s&   
 !x