a
    
daa                     @   s  d Z ddlZddlZddlm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m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 dd
lmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ dZdZ dZ!dZ"dZ#dZ$dZ%dZ&e'e(Z)G dd dej*Z+eej,e+dddZ-G dd dZ.G d d! d!ej/Z0G d"d# d#e.ee Z1G d$d% d%e.Z2G d&d' d'e.Z3G d(d) d)e1e.ej4Z4G d*d+ d+e2e.ej5Z5G d,d- d-e3e1e.ej6Z6G d.d/ d/e3e2e.ej7Z7dS )0z6Invocation-side implementation of gRPC Asyncio Python.    N)partial)AnyAsyncIterator	GeneratorGenericOptionalTuple)_common)cygrpc   )
_base_call)Metadata)DeserializingFunction)DoneCallbackType)MetadatumType)RequestIterableType)RequestType)ResponseType)SerializingFunction)AioRpcErrorCallUnaryUnaryCallUnaryStreamCallz!Locally cancelled by application!z"Cancelled upon garbage collection!zRPC already finished.z0RPC is half closed after calling "done_writing".zBThe iterator and read/write APIs may not be mixed on a single RPC.z?<{} of RPC that terminated with:
	status = {}
	details = "{}"
>zZ<{} of RPC that terminated with:
	status = {}
	details = "{}"
	debug_error_string = "{}"
>c                       s   e Zd ZU dZejed< ee ed< ee	 ed< ee	 ed< ee ed< deje	e	ee ee dd fd	d
Z
ejdddZee dddZe	dddZe	dddZedddZedddZedddZedddZdd Z  ZS )r   zAn implementation of RpcError to be used by the asynchronous API.

    Raised RpcError is a snapshot of the final status of the RPC, values are
    determined. Hence, its methods no longer needs to be coroutines.
    _code_details_initial_metadata_trailing_metadata_debug_error_stringN)codeinitial_metadatatrailing_metadatadetailsdebug_error_stringreturnc                    s,   t    || _|| _|| _|| _|| _dS )ah  Constructor.

        Args:
          code: The status code with which the RPC has been finalized.
          details: Optional details explaining the reason of the error.
          initial_metadata: Optional initial metadata that could be sent by the
            Server.
          trailing_metadata: Optional metadata that could be sent by the Server.
        N)super__init__r   r   r   r   r   )selfr   r   r    r!   r"   	__class__ W/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/grpc/aio/_call.pyr%   L   s    
zAioRpcError.__init__r#   c                 C   s   | j S )ztAccesses the status code sent by the server.

        Returns:
          The `grpc.StatusCode` status code.
        )r   r&   r)   r)   r*   r   e   s    zAioRpcError.codec                 C   s   | j S )zkAccesses the details sent by the server.

        Returns:
          The description of the error.
        )r   r,   r)   r)   r*   r!   m   s    zAioRpcError.detailsc                 C   s   | j S )zuAccesses the initial metadata sent by the server.

        Returns:
          The initial metadata received.
        )r   r,   r)   r)   r*   r   u   s    zAioRpcError.initial_metadatac                 C   s   | j S )zwAccesses the trailing metadata sent by the server.

        Returns:
          The trailing metadata received.
        )r   r,   r)   r)   r*   r    }   s    zAioRpcError.trailing_metadatac                 C   s   | j S )zyAccesses the debug error string sent by the server.

        Returns:
          The debug error string received.
        )r   r,   r)   r)   r*   r"      s    zAioRpcError.debug_error_stringc                 C   s   t | jj| j| j| jS )z-Assembles the error string for the RPC error.)_NON_OK_CALL_REPRESENTATIONformatr(   __name__r   r   r   r,   r)   r)   r*   _repr   s    zAioRpcError._reprc                 C   s   |   S Nr0   r,   r)   r)   r*   __repr__   s    zAioRpcError.__repr__c                 C   s   |   S r1   r2   r,   r)   r)   r*   __str__   s    zAioRpcError.__str__c                 C   s    t | | j| j| j| j| jffS r1   )typer   r   r   r   r   r,   r)   r)   r*   
__reduce__   s    zAioRpcError.__reduce__)NN)r/   
__module____qualname____doc__grpc
StatusCode__annotations__r   strr   r%   r   r!   r   r    r"   r0   r3   r4   r6   __classcell__r)   r)   r'   r*   r   ?   s2   

  	r   )r   statusr#   c                 C   s4   t tj|  t| t| | | dS )N)r!   r"   )	r   r	   !CYGRPC_STATUS_CODE_TO_STATUS_CODEr   r   
from_tupler    r!   r"   )r   r?   r)   r)   r*   _create_rpc_error   s    rB   c                   @   sV  e Zd ZU dZejed< ejed< e	j
ed< eedf ed< eed< eed< e	j
eeeejd	d
ddZd	dddZedddZeedddZedddZedddZed	dddZee dddZedddZedd d!Zejdd"d#Zedd$d%Z edd&d'Z!d	dd(d)Z"edd*d+Z#edd,d-Z$edd.d/Z%d	S )0r   zyBase implementation of client RPC Call object.

    Implements logic around final status, metadata and cancellation.
    _loopr   _cython_call.	_metadata_request_serializer_response_deserializerN)cython_callmetadatarequest_serializerresponse_deserializerloopr#   c                 C   s&   || _ || _t|| _|| _|| _d S r1   )rC   rD   tuplerE   rF   rG   )r&   rH   rI   rJ   rK   rL   r)   r)   r*   r%      s
    
zCall.__init__r+   c                 C   s"   t | dr| j s| t d S )NrD   )hasattrrD   done_cancel_GC_CANCELLATION_DETAILSr,   r)   r)   r*   __del__   s    

zCall.__del__c                 C   s
   | j  S r1   )rD   	cancelledr,   r)   r)   r*   rS      s    zCall.cancelled)r!   r#   c                 C   s"   | j  s| j | dS dS dS )z0Forwards the application cancellation reasoning.TFN)rD   rO   cancel)r&   r!   r)   r)   r*   rP      s    
zCall._cancelc                 C   s
   |  tS r1   )rP   _LOCAL_CANCELLATION_DETAILSr,   r)   r)   r*   rT      s    zCall.cancelc                 C   s
   | j  S r1   )rD   rO   r,   r)   r)   r*   rO      s    z	Call.done)callbackr#   c                 C   s   t || }| j| d S r1   )r   rD   add_done_callback)r&   rV   cbr)   r)   r*   rW      s    
zCall.add_done_callbackc                 C   s
   | j  S r1   )rD   time_remainingr,   r)   r)   r*   rY      s    zCall.time_remainingc                    s   | j  I d H }t|S r1   )rD   r   r   rA   r&   Zraw_metadata_tupler)   r)   r*   r      s    zCall.initial_metadatac                    s   | j  I d H  }t|S r1   )rD   r?   r    r   rA   rZ   r)   r)   r*   r       s    zCall.trailing_metadatac                    s   | j  I d H  }tj| S r1   )rD   r?   r   r	   r@   )r&   Zcygrpc_coder)   r)   r*   r      s    z	Call.codec                    s   | j  I d H  S r1   )rD   r?   r!   r,   r)   r)   r*   r!      s    zCall.detailsc                    s   | j  I d H  S r1   )rD   r?   r"   r,   r)   r)   r*   r"      s    zCall.debug_error_stringc                    sP   | j  rt |  I d H }|tjjkrLt| 	 I d H | j 
 I d H d S r1   )rD   is_locally_cancelledasyncioCancelledErrorr   r:   r;   OKrB   r   r?   )r&   r   r)   r)   r*   _raise_for_status  s    
zCall._raise_for_statusc                 C   s
   t | jS r1   )reprrD   r,   r)   r)   r*   r0     s    z
Call._reprc                 C   s   |   S r1   r2   r,   r)   r)   r*   r3     s    zCall.__repr__c                 C   s   |   S r1   r2   r,   r)   r)   r*   r4     s    zCall.__str__)&r/   r7   r8   r9   r\   AbstractEventLoopr<   r:   r;   r
   Z_AioCallr   r   r   r   r   r%   rR   boolrS   r=   rP   rT   rO   r   rW   r   floatrY   r   r    r   r!   r"   r_   r0   r3   r4   r)   r)   r)   r*   r      s<   



	r   c                   @   s   e Zd ZdZdZdZdS )	_APIStyler   r      N)r/   r7   r8   UNKNOWNASYNC_GENERATORREADER_WRITERr)   r)   r)   r*   rd     s   rd   c                       sV   e Zd ZU ejed< ejdddZed fddZe	e
def dd	d
Z  ZS )_UnaryResponseMixin_call_response)response_taskc                 C   s
   || _ d S r1   )rj   )r&   rk   r)   r)   r*   _init_unary_response_mixin  s    z._UnaryResponseMixin._init_unary_response_mixinr+   c                    s    t   r| j  dS dS d S NTF)r$   rT   rj   r,   r'   r)   r*   rT   !  s    

z_UnaryResponseMixin.cancelNc                 c   sr   z| j E dH }W n& tjy6   |  s0|    Y n0 |tju rj| j rVt qnt	| jj
| jjn|S dS )z+Wait till the ongoing RPC request finishes.N)rj   r\   r]   rS   rT   r
   EOFrD   r[   rB   r   Z_status)r&   responser)   r)   r*   	__await__(  s    


z_UnaryResponseMixin.__await__)r/   r7   r8   r\   Taskr<   rl   rb   rT   r   r   r   rp   r>   r)   r)   r'   r*   ri     s   

ri   c                       s   e Zd ZU ee ed< ejed< eed< ejdddZ	eddd	Z
ed
 fddZed
ddZee d
ddZed
ddZed
ddZ  ZS )_StreamResponseMixin_message_aiter_preparation_response_style)preparationc                 C   s   d | _ || _tj| _d S r1   )rs   rt   rd   rf   ru   )r&   rv   r)   r)   r*   _init_stream_response_mixinK  s    z0_StreamResponseMixin._init_stream_response_mixinstylec                 C   s,   | j tju r|| _ n| j |ur(ttd S r1   )ru   rd   rf   r
   
UsageError_API_STYLE_ERRORr&   ry   r)   r)   r*   _update_response_styleP  s    
z+_StreamResponseMixin._update_response_styler+   c                    s    t   r| j  dS dS d S rm   )r$   rT   rt   r,   r'   r)   r*   rT   V  s    

z_StreamResponseMixin.cancelc                 C  s@   |   I d H }|tjur.|V  |   I d H }q|  I d H  d S r1   )_readr
   rn   r_   )r&   messager)   r)   r*   _fetch_stream_responses]  s
    
z,_StreamResponseMixin._fetch_stream_responsesc                 C   s&   |  tj | jd u r |  | _| jS r1   )r}   rd   rg   rs   r   r,   r)   r)   r*   	__aiter__f  s    

z_StreamResponseMixin.__aiter__c                    sj   | j I d H  z| j I d H }W n& tjyF   |  s@|    Y n0 |tju rXtjS t	
|| jS d S r1   )rt   rD   Zreceive_serialized_messager\   r]   rS   rT   r
   rn   r	   deserializerG   )r&   Zraw_responser)   r)   r*   r~   l  s    
z_StreamResponseMixin._readc                    sR   |   r|  I d H  tjS | tj |  I d H }|tju rN|  I d H  |S r1   )rO   r_   r
   rn   r}   rd   rh   r~   )r&   Zresponse_messager)   r)   r*   read  s    
z_StreamResponseMixin.read)r/   r7   r8   r   r   r<   r\   rq   rd   rw   r}   rb   rT   r   r   r~   r   r>   r)   r)   r'   r*   rr   F  s   

	rr   c                       s   e Zd ZU ejed< eed< eej ed< e	ed< ee
 dddZe	dd	d
Zed fddZdd Ze
ddddZeddddZddddZeddddZddddZddddZ  ZS )_StreamRequestMixin_metadata_sent_done_writing_flag_async_request_poller_request_style)request_iteratorc                 C   sH   t  | _d| _|d ur6| j| || _tj	| _
nd | _tj| _
d S )NF)r\   Eventr   r   rC   create_task_consume_request_iteratorr   rd   rg   r   rh   )r&   r   r)   r)   r*   _init_stream_request_mixin  s    

z._StreamRequestMixin._init_stream_request_mixinrx   c                 C   s   | j |urttd S r1   )r   r
   rz   r{   r|   r)   r)   r*   _raise_for_different_style  s    
z._StreamRequestMixin._raise_for_different_styler+   c                    s*   t   r"| jd ur| j  dS dS d S rm   )r$   rT   r   r,   r'   r)   r*   rT     s
    


z_StreamRequestMixin.cancelc                 C   s   | j   d S r1   )r   setr,   r)   r)   r*   _metadata_sent_observer  s    z+_StreamRequestMixin._metadata_sent_observerN)r   r#   c                    s  zt |st|drx|2 zX3 d H W }z| |I d H  W q typ } ztd| W Y d }~ W d S d }~0 0 q6 nV|D ]P}z| |I d H  W q| ty } ztd| W Y d }~ W d S d }~0 0 q||  I d H  W n$   tdt	  | 
  Y n0 d S )Nr   z2Exception while consuming the request_iterator: %sz,Client request_iterator raised exception:
%s)inspect
isasyncgenrN   _writer   _LOGGERdebug_done_writing	traceback
format_excrT   )r&   r   requestZ	rpc_errorr)   r)   r*   r     s:    "z-_StreamRequestMixin._consume_request_iterator)r   r#   c                    s   |   rtt| jr"tt| j sR| j I d H  |   rR| 	 I d H  t
|| j}z| j|I d H  W nF tjy   | 	 I d H  Y n& tjy   |  s|    Y n0 d S r1   )rO   r\   ZInvalidStateError_RPC_ALREADY_FINISHED_DETAILSr   _RPC_HALF_CLOSED_DETAILSr   is_setwaitr_   r	   	serializerF   rD   Zsend_serialized_messager
   ZInternalErrorr]   rS   rT   )r&   r   serialized_requestr)   r)   r*   r     s&    


z_StreamRequestMixin._writec                    sX   |   rd S | jsTd| _z| j I d H  W n& tjyR   |  sL|    Y n0 d S )NT)rO   r   rD   Zsend_receive_closer\   r]   rS   rT   r,   r)   r)   r*   r     s    z!_StreamRequestMixin._done_writingc                    s    |  tj | |I d H  d S r1   )r   rd   rh   r   )r&   r   r)   r)   r*   write  s    z_StreamRequestMixin.writec                    s   |  tj |  I dH  dS )zUSignal peer that client is done writing.

        This method is idempotent.
        N)r   rd   rh   r   r,   r)   r)   r*   done_writing  s    z _StreamRequestMixin.done_writingc                    s*   | j  I d H  |  r&|  I d H  d S r1   )r   r   rO   r_   r,   r)   r)   r*   wait_for_connection  s    z'_StreamRequestMixin.wait_for_connection)r/   r7   r8   r\   r   r<   rb   r   rq   rd   r   r   r   rT   r   r   r   r   r   r   r   r   r>   r)   r)   r'   r*   r     s"   

,r   c                       s|   e Zd ZU dZeed< ejed< eee	 e
eej ee ejeeeejdd fddZedd	d
ZddddZ  ZS )r   z}Object for managing unary-unary RPC calls.

    Returned when an instance of `UnaryUnaryMultiCallable` object is called.
    _request_invocation_taskNr   deadlinerI   credentialswait_for_readychannelmethodrJ   rK   rL   r#   c                    sF   t  ||||||||	|
 || _|
|  | _| | j d S r1   )r$   r%   callr   r   _invoker   rl   r&   r   r   rI   r   r   r   r   rJ   rK   rL   r'   r)   r*   r%     s    zUnaryUnaryCall.__init__r+   c                    sr   t | j| j}z| j|| jI d H }W n$ tjyN   | 	 sJ| 
  Y n0 | j rht || jS tjS d S r1   )r	   r   r   rF   rD   Zunary_unaryrE   r\   r]   rS   rT   is_okr   rG   r
   rn   )r&   r   serialized_responser)   r)   r*   r   6  s    
zUnaryUnaryCall._invokec                    s&   | j I d H  |  r"|  I d H  d S r1   )r   rO   r_   r,   r)   r)   r*   r   M  s    z"UnaryUnaryCall.wait_for_connection)r/   r7   r8   r9   r   r<   r\   rq   r   rc   r   r:   CallCredentialsrb   r
   
AioChannelbytesr   r   ra   r%   r   r   r   r>   r)   r)   r'   r*   r     s"   

r   c                       s|   e Zd ZU dZeed< ejed< eee	 e
eej ee ejeeeejdd fddZedd	d
ZddddZ  ZS )r   zObject for managing unary-stream RPC calls.

    Returned when an instance of `UnaryStreamMultiCallable` object is called.
    r   _send_unary_request_taskNr   c                    sF   t  ||||||||	|
 || _|
|  | _| | j d S r1   )r$   r%   r   r   r   _send_unary_requestr   rw   r   r'   r)   r*   r%   ]  s    zUnaryStreamCall.__init__r+   c                    sV   t | j| j}z| j|| jI d H  W n& tjyP   | 	 sJ| 
   Y n0 d S r1   )r	   r   r   rF   rD   Zinitiate_unary_streamrE   r\   r]   rS   rT   )r&   r   r)   r)   r*   r   w  s    z#UnaryStreamCall._send_unary_requestc                    s&   | j I d H  |  r"|  I d H  d S r1   )r   rO   r_   r,   r)   r)   r*   r     s    z#UnaryStreamCall.wait_for_connection)r/   r7   r8   r9   r   r<   r\   rq   r   rc   r   r:   r   rb   r
   r   r   r   r   ra   r%   r   r   r   r>   r)   r)   r'   r*   r   S  s"   

r   c                       s^   e Zd ZdZee ee eeej	 ee
 ejeeeejdd fddZedddZ  ZS )	StreamUnaryCallzObject for managing stream-unary RPC calls.

    Returned when an instance of `StreamUnaryMultiCallable` object is called.
    Nr   r   rI   r   r   r   r   rJ   rK   rL   r#   c                    sB   t  ||||||||	|
 | | | |
|   d S r1   )r$   r%   r   r   rl   r   _conduct_rpcr&   r   r   rI   r   r   r   r   rJ   rK   rL   r'   r)   r*   r%     s    
zStreamUnaryCall.__init__r+   c                    sf   z| j | j| jI d H }W n& tjyB   |  s<|    Y n0 | j  r\t	
|| jS tjS d S r1   )rD   Zstream_unaryrE   r   r\   r]   rS   rT   r   r	   r   rG   r
   rn   )r&   r   r)   r)   r*   r     s    
zStreamUnaryCall._conduct_rpc)r/   r7   r8   r9   r   r   rc   r   r:   r   rb   r
   r   r   r   r   r\   ra   r%   r   r   r>   r)   r)   r'   r*   r     s   r   c                       sd   e Zd ZU dZejed< ee ee	 e
eej ee ejeeeejdd fddZdd Z  ZS )	StreamStreamCallzObject for managing stream-stream RPC calls.

    Returned when an instance of `StreamStreamMultiCallable` object is called.
    _initializerNr   c                    sL   t  ||||||||	|
 | j|  | _| | | | j d S r1   )	r$   r%   r   rC   r   _prepare_rpcr   r   rw   r   r'   r)   r*   r%     s    
zStreamStreamCall.__init__c                    sF   z| j | j| jI dH  W n$ tjy@   |  s<|   Y n0 dS )zThis method prepares the RPC for receiving/sending messages.

        All other operations around the stream should only happen after the
        completion of this method.
        N)rD   Zinitiate_stream_streamrE   r   r\   r]   rS   rT   r,   r)   r)   r*   r     s    zStreamStreamCall._prepare_rpc)r/   r7   r8   r9   r\   rq   r<   r   r   rc   r   r:   r   rb   r
   r   r   r   r   ra   r%   r   r>   r)   r)   r'   r*   r     s   

r   )8r9   r\   enum	functoolsr   r   loggingr   typingr   r   r   r   r   r   r:   r	   Zgrpc._cythonr
    r   rE   r   _typingr   r   r   r   r   r   r   __all__rU   rQ   r   r   r{   Z_OK_CALL_REPRESENTATIONr-   	getLoggerr/   r   ZRpcErrorr   ZAioRpcStatusrB   r   IntEnumrd   ri   rr   r   r   r   r   r   r)   r)   r)   r*   <module>   sb    
k`+G ?8

3

