o
    -wi                     @  s   d dl mZ d dlZd dlZd dlZd dlZd dlmZ d dlm	Z
 ddlmZ ddlmZ eeZG dd	 d	eZG d
d dZdS )    )annotationsN)wandb_internal_pb2)wandb_server_pb2   )MailboxHandle)MailboxResponseHandlec                   @  s   e Zd ZdZdS )MailboxClosedErrorz/The mailbox has been closed and cannot be used.N)__name__
__module____qualname____doc__ r   r   V/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/wandb/sdk/mailbox/mailbox.pyr      s    r   c                   @  sB   e Zd ZdZdddZdd	d
ZdddZdddZdddZdS )Mailboxa9  Matches service responses to requests.

    The mailbox can set an address on a server request and create a handle for
    waiting for a response to that record. Responses are delivered by calling
    `deliver()`. The `close()` method abandons all handles in case the
    service process becomes unreachable.
    returnNonec                 C  s   i | _ t | _d| _d S )NF)_handles	threadingLock_handles_lock_closed)selfr   r   r   __init__   s   

zMailbox.__init__requestspb.ServerRequest | pb.Record!MailboxHandle[spb.ServerResponse]c                 C  s   t |tjr|j }rtd| d|  }||_n|jj }r)td| d|  }||j_| j | j	r;t
 t|}|| j|< W d   |S 1 sOw   Y  |S )a  Set a response address on a request.

        Args:
            request: The request on which to set a request ID or mailbox slot.
                This is mutated. An address must not already be set.

        Returns:
            A handle for waiting for the response to the request.

        Raises:
            MailboxClosedError: If the mailbox has been closed, in which case
                no new responses are expected to be delivered and new handles
                cannot be created.
        z Request already has an address ()N)
isinstancespbServerRequest
request_id
ValueError_new_addresscontrolmailbox_slotr   r   r   r   r   )r   r   addresshandler   r   r   require_response#   s$   

zMailbox.require_responsestrc                 C  s,   dd }| }|| j v r| }|| j v s|S )z[Returns an unused address for a request.

        Assumes `_handles_lock` is held.
        c                   S  s   d dd tdD S )N c                 s  s"    | ]}t tjtj V  qd S )N)secretschoicestringascii_lowercasedigits).0ir   r   r   	<genexpr>R   s
    
z9Mailbox._new_address.<locals>.generate.<locals>.<genexpr>   )joinranger   r   r   r   generateQ   s   
z&Mailbox._new_address.<locals>.generate)r   )r   r5   r%   r   r   r   r"   K   s   

zMailbox._new_addressresponsespb.ServerResponsec                 C  s   |j }|s#|d}|dkr|jd}d| }td|  dS | j | j|d}W d   n1 s8w   Y  |rF|| dS dS )zDeliver a response from the service.

        If the response address is invalid, this does nothing.
        It is a no-op if the mailbox has been closed.
        server_response_typeresult_communicateresult_typezresult_communicate.z(Received response with no mailbox slot: N)	r    
WhichOneofr9   _loggererrorr   r   popdeliver)r   r6   r%   kindr:   r&   r   r   r   r?   _   s   

zMailbox.deliverc                 C  sl   | j ) d| _tdt| j d | j D ]}|  q| j  W d   dS 1 s/w   Y  dS )zXIndicate no further responses will be delivered.

        Abandons all handles.
        TzClosing mailbox, abandoning z	 handles.N)	r   r   r<   infolenr   valuesabandonclear)r   r&   r   r   r   closey   s   
"zMailbox.closeN)r   r   )r   r   r   r   )r   r(   )r6   r7   r   r   )	r	   r
   r   r   r   r'   r"   r?   rF   r   r   r   r   r      s    


(
r   )
__future__r   loggingr*   r,   r   wandb.protor   pbr   r   mailbox_handler   response_handler   	getLoggerr	   r<   	Exceptionr   r   r   r   r   r   <module>   s    
