o
    |j6hc                     @   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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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-dedej.de-fdd Z/G d!d" d"Z0G d#d$ d$ej1Z2G d%d& d&e0ee Z3G d'd( d(e0Z4G d)d* d*e0Z5G d+d, d,e3e0ej6Z6G d-d. d.e4e0ej7Z7G d/d0 d0e5e3e0ej8Z8G d1d2 d2e5e4e0ej9Z9dS )3z6Invocation-side implementation of gRPC Asyncio Python.    N)partial)AnyAsyncIterator	GeneratorGenericOptionalTupleUnion)_common)cygrpc   )
_base_call)Metadata)DeserializingFunction)DoneCallbackType)EOFType)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"dejd	e	d
e	dee dee ddf fddZ
dejfddZdee fddZde	fddZde	fddZdefddZdefddZdefddZdef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__ ]/var/www/html/chefvision.cloud.itp360.com/venv/lib/python3.10/site-packages/grpc/aio/_call.pyr'   U   s   

zAioRpcError.__init__c                 C      | j S )ztAccesses the status code sent by the server.

        Returns:
          The `grpc.StatusCode` status code.
        )r   r(   r+   r+   r,   r    n      zAioRpcError.codec                 C   r-   )zkAccesses the details sent by the server.

        Returns:
          The description of the error.
        )r   r.   r+   r+   r,   r#   v   r/   zAioRpcError.detailsc                 C   r-   )zuAccesses the initial metadata sent by the server.

        Returns:
          The initial metadata received.
        )r   r.   r+   r+   r,   r!   ~   r/   zAioRpcError.initial_metadatac                 C   r-   )zwAccesses the trailing metadata sent by the server.

        Returns:
          The trailing metadata received.
        )r   r.   r+   r+   r,   r"      r/   zAioRpcError.trailing_metadatac                 C   r-   )zyAccesses the debug error string sent by the server.

        Returns:
          The debug error string received.
        )r   r.   r+   r+   r,   r$      r/   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 Nr3   r.   r+   r+   r,   __repr__      zAioRpcError.__repr__c                 C   r4   r5   r6   r.   r+   r+   r,   __str__   r8   zAioRpcError.__str__c                 C   s    t | | j| j| j| j| jffS r5   )typer   r   r   r   r   r.   r+   r+   r,   
__reduce__   s   zAioRpcError.__reduce__)NN)r2   
__module____qualname____doc__grpc
StatusCode__annotations__r   strr   r'   r    r#   r!   r"   r$   r3   r7   r9   r;   __classcell__r+   r+   r)   r,   r   H   s>   
 
	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!   rD   r+   r+   r,   _create_rpc_error   s   rG   c                   @   s\  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< d	e	j
d
edededejddfddZd4ddZdefddZdedefddZdefddZdefddZdeddfddZdee fd d!Zdefd"d#Zdefd$d%Zdejfd&d'Zdefd(d)Z defd*d+Z!d4d,d-Z"defd.d/Z#defd0d1Z$defd2d3Z%dS )5r   zyBase implementation of client RPC Call object.

    Implements logic around final status, metadata and cancellation.
    _loopr   _cython_call.	_metadata_request_serializer_response_deserializercython_callmetadatarequest_serializerresponse_deserializerloopr%   Nc                 C   s&   || _ || _t|| _|| _|| _d S r5   )rH   rI   tuplerJ   rK   rL   )r(   rM   rN   rO   rP   rQ   r+   r+   r,   r'      s
   

zCall.__init__c                 C   s*   t | dr| j s| t d S d S d S )NrI   )hasattrrI   done_cancel_GC_CANCELLATION_DETAILSr.   r+   r+   r,   __del__   s
   

zCall.__del__c                 C   
   | j  S r5   )rI   	cancelledr.   r+   r+   r,   rY         
zCall.cancelledr#   c                 C   s   | j  s| j | dS dS )z0Forwards the application cancellation reasoning.TF)rI   rT   cancel)r(   r#   r+   r+   r,   rU      s   
zCall._cancelc                 C   s
   |  tS r5   )rU   _LOCAL_CANCELLATION_DETAILSr.   r+   r+   r,   r[      rZ   zCall.cancelc                 C   rX   r5   )rI   rT   r.   r+   r+   r,   rT      rZ   z	Call.donecallbackc                 C   s   t || }| j| d S r5   )r   rI   add_done_callback)r(   r]   cbr+   r+   r,   r^      s   
zCall.add_done_callbackc                 C   rX   r5   )rI   time_remainingr.   r+   r+   r,   r`      rZ   zCall.time_remainingc                    s   | j  I d H }t|S r5   )rI   r!   r   rF   r(   raw_metadata_tupler+   r+   r,   r!      s   
zCall.initial_metadatac                    s    | j  I d H  }t|S r5   )rI   rD   r"   r   rF   ra   r+   r+   r,   r"      s
   
zCall.trailing_metadatac                    s    | j  I d H  }tj| S r5   )rI   rD   r    r
   rE   )r(   cygrpc_coder+   r+   r,   r      s   
z	Call.codec                       | j  I d H  S r5   )rI   rD   r#   r.   r+   r+   r,   r#        zCall.detailsc                    rd   r5   )rI   rD   r$   r.   r+   r+   r,   r$     re   zCall.debug_error_stringc                    sR   | j  r
t |  I d H }|tjjkr't| 	 I d H | j 
 I d H d S r5   )rI   is_locally_cancelledasyncioCancelledErrorr    r?   r@   OKrG   r!   rD   )r(   r    r+   r+   r,   _raise_for_status  s   
zCall._raise_for_statusc                 C   s
   t | jS r5   )reprrI   r.   r+   r+   r,   r3     rZ   z
Call._reprc                 C   r4   r5   r6   r.   r+   r+   r,   r7     r8   zCall.__repr__c                 C   r4   r5   r6   r.   r+   r+   r,   r9     r8   zCall.__str__r%   N)&r2   r<   r=   r>   rg   AbstractEventLooprA   r?   r@   r   _AioCallr   r   r   r   r   r'   rW   boolrY   rB   rU   r[   rT   r   r^   r   floatr`   r!   r"   r    r#   r$   rj   r3   r7   r9   r+   r+   r+   r,   r      sJ   
 





	r   c                   @   s   e Zd ZdZdZdZdS )	_APIStyler   r      N)r2   r<   r=   UNKNOWNASYNC_GENERATORREADER_WRITERr+   r+   r+   r,   rq     s    rq   c                       sV   e Zd ZU ejed< dejfddZdef fddZde	e
def fd	d
Z  ZS )_UnaryResponseMixin_call_responseresponse_taskc                 C   s
   || _ d S r5   )rw   )r(   rx   r+   r+   r,   _init_unary_response_mixin'  rZ   z._UnaryResponseMixin._init_unary_response_mixinr%   c                       t   r| j  dS dS NTF)r&   r[   rw   r.   r)   r+   r,   r[   *     

z_UnaryResponseMixin.cancelNc                 c   sh    z| j E dH }W n tjy   |  s|    w |tju r2| j r)t t	| jj
| jj|S )z+Wait till the ongoing RPC request finishes.N)rw   rg   rh   rY   r[   r   EOFrI   rf   rG   r   _status)r(   responser+   r+   r,   	__await__1  s    

z_UnaryResponseMixin.__await__)r2   r<   r=   rg   TaskrA   ry   ro   r[   r   r   r   r   rC   r+   r+   r)   r,   rv   $  s
   
 
 rv   c                       s   e Zd ZU ee ed< ejed< eed< dejfddZ	defdd	Z
d
ef fddZd
efddZd
ee fddZd
efddZd
eeef fddZ  ZS )_StreamResponseMixin_message_aiter_preparation_response_stylepreparationc                 C   s   d | _ || _tj| _d S r5   )r   r   rq   rs   r   )r(   r   r+   r+   r,   _init_stream_response_mixinT  s   z0_StreamResponseMixin._init_stream_response_mixinstylec                 C   s.   | j tju r|| _ d S | j |urttd S r5   )r   rq   rs   r   
UsageError_API_STYLE_ERRORr(   r   r+   r+   r,   _update_response_styleY  s
   


z+_StreamResponseMixin._update_response_styler%   c                    rz   r{   )r&   r[   r   r.   r)   r+   r,   r[   _  r|   z_StreamResponseMixin.cancelc                 C  sJ   |   I d H }|tjur|V  |   I d H }|tjus|  I d H  d S r5   )_readr   r}   rj   )r(   messager+   r+   r,   _fetch_stream_responsesf  s   

z,_StreamResponseMixin._fetch_stream_responsesc                 C   s&   |  tj | jd u r|  | _| jS r5   )r   rq   rt   r   r   r.   r+   r+   r,   	__aiter__o  s   

z_StreamResponseMixin.__aiter__c                    sd   | j I d H  z
| j I d H }W n tjy"   |  s!|    w |tju r+tjS t	
|| jS r5   )r   rI   receive_serialized_messagerg   rh   rY   r[   r   r}   r
   deserializerL   )r(   raw_responser+   r+   r,   r   u  s   
z_StreamResponseMixin._readc                    sT   |   r|  I d H  tjS | tj |  I d H }|tju r(|  I d H  |S r5   )rT   rj   r   r}   r   rq   ru   r   )r(   response_messager+   r+   r,   read  s   
z_StreamResponseMixin.read)r2   r<   r=   r   r   rA   rg   r   rq   r   r   ro   r[   r   r   r   r	   r   r   rC   r+   r+   r)   r,   r   O  s   
 
	r   c                       s   e Zd ZU ejed< eed< eej ed< e	ed< dee
 fddZde	fd	d
Zdef fddZdd Zde
ddfddZdeddfddZdddZdeddfddZdddZdddZ  ZS )_StreamRequestMixin_metadata_sent_done_writing_flag_async_request_poller_request_stylerequest_iteratorc                 C   sJ   t  | _d| _|d ur| j| || _tj	| _
d S d | _tj| _
d S )NF)rg   Eventr   r   rH   create_task_consume_request_iteratorr   rq   rt   r   ru   )r(   r   r+   r+   r,   _init_stream_request_mixin  s   
z._StreamRequestMixin._init_stream_request_mixinr   c                 C   s   | j |ur
ttd S r5   )r   r   r   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 r{   )r&   r[   r   r.   r)   r+   r,   r[     s
   


z_StreamRequestMixin.cancelc                 C   s   | j   d S r5   )r   setr.   r+   r+   r,   _metadata_sent_observer  s   z+_StreamRequestMixin._metadata_sent_observerNc                    s  znt |st|dr<|2 z+3 d H W }z
| |I d H  W q ty9 } ztd| W Y d }~ W d S d }~ww 6 n*|D ]'}z
| |I d H  W q> tye } ztd| W Y d }~ W d S d }~ww |  I d H  W d S    tdt	  | 
  Y d S )Nr   z2Exception while consuming the request_iterator: %sz,Client request_iterator raised exception:
%s)inspect
isasyncgenrS   _writer   _LOGGERdebug_done_writing	traceback
format_excr[   )r(   r   request	rpc_errorr+   r+   r,   r     sF   
z-_StreamRequestMixin._consume_request_iteratorr   c              
      s   |   r
tt| jrtt| j s*| j I d H  |   r*| 	 I d H  t
|| j}z| j|I d H  W d S  tjy` } z| jt| | 	 I d H  W Y d }~d S d }~w tjyp   |  so|    w r5   )rT   rg   InvalidStateError_RPC_ALREADY_FINISHED_DETAILSr   _RPC_HALF_CLOSED_DETAILSr   is_setwaitrj   r
   	serializerK   rI   send_serialized_messager   InternalErrorset_internal_errorrB   rh   rY   r[   )r(   r   serialized_requesterrr+   r+   r,   r     s.   


z_StreamRequestMixin._writec                    sX   |   rd S | js*d| _z| j I d H  W d S  tjy)   |  s(|    w d S )NT)rT   r   rI   send_receive_closerg   rh   rY   r[   r.   r+   r+   r,   r     s   z!_StreamRequestMixin._done_writingc                    s"   |  tj | |I d H  d S r5   )r   rq   ru   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   rq   ru   r   r.   r+   r+   r,   done_writing  s   z _StreamRequestMixin.done_writingc                    s0   | j  I d H  |  r|  I d H  d S d S r5   )r   r   rT   rj   r.   r+   r+   r,   wait_for_connection  s
   z'_StreamRequestMixin.wait_for_connectionrl   )r2   r<   r=   rg   r   rA   ro   r   r   rq   r   r   r   r[   r   r   r   r   r   r   r   r   rC   r+   r+   r)   r,   r     s*   
 


,

r   c                          e Zd ZU dZeed< ejed< dedee	 de
deej dee d	ejd
edededejddf fddZdefddZ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_taskr   deadlinerN   credentialswait_for_readychannelmethodrO   rP   rQ   r%   Nc                    P   t  ||||||||	|
 || _t | _|
|  | _	| 
| j	 d S r5   )r&   r'   callr   r   build_census_context_contextr   _invoker   ry   r(   r   r   rN   r   r   r   r   rO   rP   rQ   r)   r+   r,   r'   (  s   
zUnaryUnaryCall.__init__c                    st   t | j| j}z| j|| j| jI d H }W n tj	y*   | 
 s(|   Y nw | j r7t || jS tjS r5   )r
   r   r   rK   rI   unary_unaryrJ   r   rg   rh   rY   r[   is_okr   rL   r   r}   )r(   r   serialized_responser+   r+   r,   r   A  s$   

zUnaryUnaryCall._invokec                    ,   | j I d H  |  r|  I d H  d S d S r5   )r   rT   rj   r.   r+   r+   r,   r   X  
   z"UnaryUnaryCall.wait_for_connectionrl   )r2   r<   r=   r>   r   rA   rg   r   r   rp   r   r?   CallCredentialsro   r   
AioChannelbytesr   r   rm   r'   r   r   r   rC   r+   r+   r)   r,   r     s:   
 
	
r   c                       r   )r   zObject for managing unary-stream RPC calls.

    Returned when an instance of `UnaryStreamMultiCallable` object is called.
    r   _send_unary_request_taskr   r   rN   r   r   r   r   rO   rP   rQ   r%   Nc                    r   r5   )r&   r'   r   r   r   r   r   r   _send_unary_requestr   r   r   r)   r+   r,   r'   h  s   
zUnaryStreamCall.__init__c                    sV   t | j| j}z| j|| j| jI d H  W d S  tj	y*   | 
 s)|    w r5   )r
   r   r   rK   rI   initiate_unary_streamrJ   r   rg   rh   rY   r[   )r(   r   r+   r+   r,   r     s   
z#UnaryStreamCall._send_unary_requestc                    r   r5   )r   rT   rj   r.   r+   r+   r,   r     r   z#UnaryStreamCall.wait_for_connectionrl   )r2   r<   r=   r>   r   rA   rg   r   r   rp   r   r?   r   ro   r   r   r   r   r   rm   r'   r   r   r   rC   r+   r+   r)   r,   r   ^  s:   
 
	
r   c                       sr   e Zd ZdZdee dee dedeej	 dee
 dejded	ed
edejddf fddZdefddZ  ZS )StreamUnaryCallzObject for managing stream-unary RPC calls.

    Returned when an instance of `StreamUnaryMultiCallable` object is called.
    r   r   rN   r   r   r   r   rO   rP   rQ   r%   Nc                    sL   t  ||||||||	|
 t | _| | | |
| 	  d S r5   )
r&   r'   r   r   r   r   r   ry   r   _conduct_rpcr(   r   r   rN   r   r   r   r   rO   rP   rQ   r)   r+   r,   r'     s   

zStreamUnaryCall.__init__c                    sd   z| j | j| j| jI d H }W n tjy"   |  s!|    w | j 	 r/t
|| jS tjS r5   )rI   stream_unaryrJ   r   r   rg   rh   rY   r[   r   r
   r   rL   r   r}   )r(   r   r+   r+   r,   r     s   
zStreamUnaryCall._conduct_rpc)r2   r<   r=   r>   r   r   rp   r   r?   r   ro   r   r   r   r   r   rg   rm   r'   r   r   rC   r+   r+   r)   r,   r     s4    	
r   c                       sx   e Zd ZU dZejed< dee dee	 de
deej dee dejd	ed
ededejddf 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.
    _initializerr   r   rN   r   r   r   r   rO   rP   rQ   r%   Nc                    sV   t  ||||||||	|
 t | _| j|  | _	| 
| | | j	 d S r5   )r&   r'   r   r   r   r   rH   r   _prepare_rpcr   r   r   r   r)   r+   r,   r'     s   

zStreamStreamCall.__init__c                    sR   z| j | j| j| jI dH  W dS  tjy(   |  s%|   Y dS Y dS w )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)	rI   initiate_stream_streamrJ   r   r   rg   rh   rY   r[   r.   r+   r+   r,   r     s   zStreamStreamCall._prepare_rpc)r2   r<   r=   r>   rg   r   rA   r   r   rp   r   r?   r   ro   r   r   r   r   r   rm   r'   r   rC   r+   r+   r)   r,   r     s6   
 
	
r   ):r>   rg   enum	functoolsr   r   loggingr   typingr   r   r   r   r   r   r	   r?   r
   grpc._cythonr    r   rJ   r   _typingr   r   r   r   r   r   r   r   __all__r\   rV   r   r   r   _OK_CALL_REPRESENTATIONr0   	getLoggerr2   r   RpcErrorr   AioRpcStatusrG   r   IntEnumrq   rv   r   r   r   r   r   r   r+   r+   r+   r,   <module>   sn   $

j
`+G 	@
9

4
