o
    xi                     @  s   d dl mZ d dlZd dlmZmZ d dlmZmZ d dl	m
Z
 edZedZG dd	 d	eZG d
d deZG dd dejee ZG dd dee ee ZdS )    )annotationsN)CallableGeneric)TypeVaroverride)asyncio_manager_T_Sc                   @     e Zd ZdZdS )HandleAbandonedErrorz2The handle has no response and has been abandoned.N__name__
__module____qualname____doc__ r   r   T/home/ubuntu/.local/lib/python3.10/site-packages/wandb/sdk/mailbox/mailbox_handle.pyr          r   c                   @  r
   )ServerResponseErrorzThe handle received a generic error response.

    In practice, this corresponds to receiving the ServerErrorResponse message.
    Nr   r   r   r   r   r      r   r   c                   @  sb   e Zd ZdZdddZeddd	ZdddZej	dddZ
ej	dddZej	dddZdS )MailboxHandlez0A handle for waiting on a response to a request.asyncerasyncio_manager.AsyncioManagerreturnNonec                 C  s
   || _ d S N_asyncer)selfr   r   r   r   __init__   s   
zMailboxHandle.__init__c                 C  s   | j S )z{The asyncio thread to which the handle belongs.

        The handle's async methods must be run using this object.
        r   r   r   r   r   r      s   zMailboxHandle.asyncerfnCallable[[_T], _S]MailboxHandle[_S]c                 C  s
   t | |S )aO  Returns a transformed handle.

        Methods on the returned handle call methods on this handle, but the
        response type is derived using the given function.

        Args:
            fn: A function to apply to this handle's result to get the new
                handle's result. The function should be pure and fast.
        )_MailboxMappedHandle)r   r    r   r   r   map'   s   

zMailboxHandle.mapc                 C     dS )a#  Cancel the handle, requesting any associated work to not complete.

        Any calls to `wait_or` or `wait_async` will raise `HandleAbandonedError`
        if they aren't resolved within a short time.

        Cancellation is best-effort. Most exceptions are logged and suppressed.
        Nr   r   r   r   r   cancel3       zMailboxHandle.canceltimeoutfloat | Noner   c                C  r%   )a  Wait for a response or a timeout.

        It is an error to call this from an async function.
        On error, including KeyboardInterrupt or a timeout,
        the handle cancels itself.

        Args:
            timeout: A finite number of seconds or None to never time out.
                If less than or equal to zero, times out immediately unless
                the response is available.

        Returns:
            The response if it arrives before the timeout or has already arrived.

        Raises:
            TimeoutError: If the timeout is reached.
            HandleAbandonedError: If the handle becomes abandoned.
            ServerResponseError: If the handle received an error response.
        Nr   r   r(   r   r   r   wait_or=   r'   zMailboxHandle.wait_orc                  s   dS )ab  Wait for a response or timeout.

        This must run in an `asyncio` event loop.
        On error, including asyncio cancellation, KeyboardInterrupt or
        a timeout, the handle cancels itself.

        Args:
            timeout: A finite number of seconds or None to never time out.

        Returns:
            The response if it arrives before the timeout or has already arrived.

        Raises:
            TimeoutError: If the timeout is reached.
            HandleAbandonedError: If the handle becomes abandoned.
            ServerResponseError: If the handle received an error response.
        Nr   r*   r   r   r   
wait_asyncS   s    zMailboxHandle.wait_asyncN)r   r   r   r   )r   r   )r    r!   r   r"   r   r   )r(   r)   r   r   )r   r   r   r   r   propertyr   r$   abcabstractmethodr&   r+   r,   r   r   r   r   r      s    

	r   c                      sL   e Zd ZdZd fdd	Zedd
dZedddZedddZ  Z	S )r#   z=A mailbox handle whose result is derived from another handle.handleMailboxHandle[_T]r    r!   r   r   c                   s   t  |j || _|| _d S r   )superr   r   _handle_fn)r   r1   r    	__class__r   r   r   k   s   
z_MailboxMappedHandle.__init__c                 C  s   | j   d S r   )r4   r&   r   r   r   r   r&   t   s   z_MailboxMappedHandle.cancelr(   r)   r	   c                C  s   |  | jj|dS N)r(   )r5   r4   r+   r*   r   r   r   r+   x   s   z_MailboxMappedHandle.wait_orc                  s    | j j|dI d H }| |S r8   )r4   r,   r5   )r   r(   responser   r   r   r,   |   s   
z_MailboxMappedHandle.wait_async)r1   r2   r    r!   r   r   r-   )r(   r)   r   r	   )
r   r   r   r   r   r   r&   r+   r,   __classcell__r   r   r6   r   r#   h   s    	r#   )
__future__r   r/   typingr   r   typing_extensionsr   r   wandb.sdk.libr   r   r	   	Exceptionr   r   ABCr   r#   r   r   r   r   <module>   s    O