U
    ^»{j0  ã                   @  s<  d Z ddlmZ ddlmZ ddlmZmZmZm	Z	m
Z
mZ edeeef ƒZedeeef ƒZede
ee	e f ƒ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G dd„ deƒZdddœdd„Zdddœdd„Zddddœdd„Zdddœd d!„Zdd"dœd#d$„Zdd"dœd%d&„Zdd"dœd'd(„Z d)S )*z~
Provides various implementations of Session and Target.

It includes helpers and constants for the Chrome Devtools Protocol.
é    )Úannotations)ÚEnum)ÚAnyÚMutableMappingÚNewTypeÚOptionalÚTupleÚcastÚBrowserResponseÚBrowserCommandÚ
MessageKeyc                   @  s   e Zd ZdZdZdS )ÚEcodez9Ecodes are a list of possible error codes chrome returns.i¦€ÿÿN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__ZTARGET_NOT_FOUND© r   r   ú^/var/www/html/bwave/python/venv/lib/python3.8/site-packages/choreographer/protocol/__init__.pyr      s   r   c                      s(   e Zd ZdZdddœ‡ fdd„Z‡  ZS )ÚDevtoolsProtocolErrorz8Raise a general error reported by the devtools protocol.r
   ÚNone©ÚresponseÚreturnc                   s,   t ƒ  |¡ |d d | _|d d | _dS )zˆ
        Construct a new DevtoolsProtocolError.

        Args:
            response: the json response that contains the error

        ÚerrorÚcodeÚmessageN)ÚsuperÚ__init__r   r   )Úselfr   ©Ú	__class__r   r   r      s    zDevtoolsProtocolError.__init__©r   r   r   r   r   Ú__classcell__r   r   r   r   r      s   r   c                      s,   e Zd ZdZdddddœ‡ fdd„Z‡  ZS )	ÚMessageTypeErrorú8An error for poorly formatted devtools protocol message.Ústrr   Útyper   )ÚkeyÚvalueÚexpected_typer   c              	     s:   t |tƒst|ƒn|}tƒ  d|› d|› d|› d�¡ dS )a  
        Construct a message about a poorly formed protocol message.

        Args:
            key: the key that has the badly typed value
            value: the type of the value that is incorrect
            expected_type: the type that was expected

        zMessage with key z must have type z, not Ú.N)Ú
isinstancer&   r   r   )r   r'   r(   r)   r   r   r   r   /   s    
ÿzMessageTypeError.__init__r!   r   r   r   r   r#   ,   s   r#   c                      s*   e Zd ZdZddddœ‡ fdd„Z‡  ZS )ÚMissingKeyErrorr$   r%   r   r   )r'   Úobjr   c                   s   t ƒ  d|› d|› �¡ dS )z­
        Construct a MissingKeyError specifying which key was missing.

        Args:
            key: the missing key
            obj: the message without the key

        zMessage missing required key/s z. Message received: N)r   r   )r   r'   r-   r   r   r   r   B   s    	ÿzMissingKeyError.__init__r!   r   r   r   r   r,   ?   s   r,   c                   @  s   e Zd ZdZdS )ÚExperimentalFeatureWarningz8An warning to report that a feature may or may not work.N)r   r   r   r   r   r   r   r   r.   P   s   r.   r   )r-   r   c                 C  s˜   d}t tdœ}| ¡ D ]<\}}|| kr0t|| ƒ‚t| | |ƒst|t| | ƒ|ƒ‚q|d7 }d| krl|d7 }d| kr||d7 }t|  ¡ ƒ|kr”t	dƒ‚dS )	a  
    Verify the message obj hast he proper keys and values.

    Args:
        obj: the object to check.

    Raises:
        MissingKeyError: if a key is missing.
        MessageTypeError: if a value type is incorrect.
        RuntimeError: if there are strange keys.

    r   )ÚidÚmethodé   Úparamsé   Ú	sessionIdzUMessage objects must have id and method keys, and may have params and sessionId keys.N)
Úintr%   Úitemsr,   r+   r#   r&   ÚlenÚkeysÚRuntimeError)r-   Zn_keysÚrequired_keysr'   Ztype_keyr   r   r   Úverify_paramsT   s     

ÿr;   z BrowserResponse | BrowserCommandzMessageKey | None)Úmsgr   c                 C  s.   |   dd¡}|   d¡}|dkr"dS t||fƒS )zð
    Given a message to/from the browser, calculate the key corresponding to the command.

    Every message is uniquely identified by its sessionId and id (counter).

    Args:
        msg: the message for which to calculate the key.

    r4   Ú r/   N)Úgetr   )r<   Ú
session_idÚ
message_idr   r   r   Úcalculate_message_keyw   s
    

rA   Úbool)r   r'   r   c                 C  sX   |\}}d| kr|dks2d| kr.| d |kr.ndS d| krPt | d ƒt |ƒkrPndS dS )zÐ
    Report True if a response matches with a certain key (sessionId, id).

    Args:
        response: the object response from the browser
        key: the (sessionId, id) key tubple we're looking for

    r?   r=   Fr/   T)r%   )r   r'   r?   r@   r   r   r   Úmatch_message_keyˆ   s    	ÿ
ÿrC   r   c                 C  s   ddh}||   ¡ kod| kS )z=Return true if the browser response is an event notification.r0   r2   r/   )r8   )r   r:   r   r   r   Úis_event    s    rD   z
str | Nonec                 C  s.   d| kr&d| d kr&t d| d d ƒS dS dS )z…
    Extract target id from a browser response.

    Args:
        response: the browser response to extract the targetId from.

    ÚresultZtargetIdr%   N©r	   ©r   r   r   r   Úget_target_id_from_result¦   s    rH   c                 C  s.   d| kr&d| d kr&t d| d d ƒS dS dS )z‡
    Extract session id from a browser response.

    Args:
        response: the browser response to extract the sessionId from.

    rE   r4   r%   NrF   rG   r   r   r   Úget_session_id_from_result´   s    rI   c                 C  s   d| krt d| d ƒS dS dS )z~
    Extract error from a browser response.

    Args:
        response: the browser response to extract the error from.

    r   r%   NrF   rG   r   r   r   Úget_error_from_resultÂ   s    rJ   N)!r   Ú
__future__r   Úenumr   Útypingr   r   r   r   r   r	   r%   r
   r   r5   r   r   Ú	Exceptionr   Ú	TypeErrorr#   Ú
ValueErrorr,   ÚUserWarningr.   r;   rA   rC   rD   rH   rI   rJ   r   r   r   r   Ú<module>   s$    #