U
    ^»{jx  ã                   @  sz   d Z ddlmZ ddlmZ ddlZddlmZ erPddlmZm	Z	 ddl
mZ e e¡ZG dd	„ d	ƒZG d
d„ dƒZdS )z>Provide a lower-level sync interface to the Devtools Protocol.é    )Úannotations)ÚTYPE_CHECKINGN)Úprotocol)ÚAnyÚMutableMapping)Ú
BrokerSyncc                   @  sH   e Zd ZU dZded< ded< ddddœd	d
„Zdddddœdd„ZdS )ÚSessionSyncz8A session is a single conversation with a single target.ÚstrÚ
session_idÚintÚ
message_idr   ÚNone)r
   ÚbrokerÚreturnc                 C  s:   t |tƒstdƒ‚|| _|| _t d|› d�¡ d| _dS )a,  
        Construct a session from the browser as an object.

        A session is like an open conversation with a target.
        All commands are sent on sessions.

        Args:
            broker:  a reference to the browser's broker
            session_id:  the id given by the browser

        zsession_id must be a stringzNew session: Ú.r   N)Ú
isinstancer	   Ú	TypeErrorÚ_brokerr
   Ú_loggerÚdebugr   )Úselfr
   r   © r   úc/var/www/html/bwave/python/venv/lib/python3.8/site-packages/choreographer/protocol/devtools_sync.pyÚ__init__   s    
zSessionSync.__init__NúMutableMapping[str, Any] | Noner   ©ÚcommandÚparamsr   c                 C  sj   | j }|  j d7  _ t ||dœ¡}| jr4| j|d< |r@||d< t d|› d|› d| j› �¡ | j |¡S )a/  
        Send a devtools command on the session.

        https://chromedevtools.github.io/devtools-protocol/

        Args:
            command: devtools command to send
            params: the parameters to send

        Returns:
            A message key (session, message id) tuple or None

        é   )ÚidÚmethodZ	sessionIdr   zSending z with z on session )r   r   ZBrowserCommandr
   r   r   r   Ú
write_json)r   r   r   Z
current_idZjson_commandr   r   r   Úsend_command2   s    þÿ
ÿzSessionSync.send_command)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú__annotations__r   r"   r   r   r   r   r      s   
 ýr   c                   @  st   e Zd ZU dZded< ded< dddœdd	„Zd
ddœdd„Zdddœdd„Zd
dœdd„Zdddddœdd„Z	dS )Ú
TargetSynczLA target like a browser, tab, or others. It sends commands. It has sessions.r	   Ú	target_idz MutableMapping[str, SessionSync]Úsessionsr   )r)   r   c                 C  s:   t |tƒstdƒ‚|| _i | _|| _t d|› d�¡ dS )zÉ
        Create a target after one ahs been created by the browser.

        Args:
            broker:  a reference to the browser's broker
            target_id:  the id given by the browser

        ztarget_id must be stringzCreated new target r   N)r   r	   r   r   r*   r)   r   r   )r   r)   r   r   r   r   r   _   s    	
zTargetSync.__init__r   r   )Úsessionr   c                 C  s"   t |tƒstdƒ‚|| j|j< d S )Nz$session must be a session type class)r   r   r   r*   r
   ©r   r+   r   r   r   Ú_add_sessionr   s    
zTargetSync._add_session)r
   r   c                 C  s"   t |tƒr|j}| j |d ¡}d S )N)r   r   r
   r*   Úpop)r   r
   Ú_r   r   r   Ú_remove_sessionw   s    
zTargetSync._remove_session)r   c                 C  s(   | j  ¡ stdƒ‚tt| j  ¡ ƒƒ}|S )z7Retrieve the first session of the target, if it exists.z9Cannot use this method without at least one valid session)r*   ÚvaluesÚRuntimeErrorÚnextÚiterr,   r   r   r   Úget_session|   s    
ÿzTargetSync.get_sessionNr   r   r   c                 C  s&   | j  ¡ stdƒ‚|  ¡ }| ||¡S )zè
        Send a command to the first session in a target.

        https://chromedevtools.github.io/devtools-protocol/

        Args:
            command: devtools command to send
            params: the parameters to send

        z6Cannot send_command without at least one valid session)r*   r1   r2   r5   r"   )r   r   r   r+   r   r   r   r"   …   s    
zTargetSync.send_command)N)
r#   r$   r%   r&   r'   r   r-   r0   r5   r"   r   r   r   r   r(   W   s   
 ýr(   )r&   Ú
__future__r   Útypingr   ZlogistroZchoreographerr   r   r   Zchoreographer._brokersr   Ú	getLoggerr#   r   r   r(   r   r   r   r   Ú<module>   s   
C