U
    "FZh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-eej.e-d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-d. d.e5e3e0ej8Z8G d/d0 d0e5e4e0ej9Z9dS )1z6Invocation-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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__ U/home/aprabhat/apps/x.techxrdev.in/venv/lib/python3.8/site-packages/grpc/aio/_call.pyr'   U   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    n   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#   v   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!   ~   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r2   r.   r+   r+   r,   __repr__   s    zAioRpcError.__repr__c                 C   s   |   S r3   r4   r.   r+   r+   r,   __str__   s    zAioRpcError.__str__c                 C   s    t | | j| j| j| j| jffS r3   )typer   r   r   r   r   r.   r+   r+   r,   
__reduce__   s    zAioRpcError.__reduce__)NN)r1   
__module____qualname____doc__grpc
StatusCode__annotations__r   strr   r'   r    r#   r!   r"   r$   r2   r5   r6   r8   __classcell__r+   r+   r)   r,   r   H   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!   rA   r+   r+   r,   _create_rpc_error   s    rD   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 r3   )rE   rF   tuplerG   rH   rI   )r(   rJ   rK   rL   rM   rN   r+   r+   r,   r'      s
    
zCall.__init__r-   c                 C   s"   t | dr| j s| t d S )NrF   )hasattrrF   done_cancel_GC_CANCELLATION_DETAILSr.   r+   r+   r,   __del__   s    

zCall.__del__c                 C   s
   | j  S r3   )rF   	cancelledr.   r+   r+   r,   rU      s    zCall.cancelled)r#   r%   c                 C   s"   | j  s| j | dS dS dS )z0Forwards the application cancellation reasoning.TFN)rF   rQ   cancel)r(   r#   r+   r+   r,   rR      s    
zCall._cancelc                 C   s
   |  tS r3   )rR   _LOCAL_CANCELLATION_DETAILSr.   r+   r+   r,   rV      s    zCall.cancelc                 C   s
   | j  S r3   )rF   rQ   r.   r+   r+   r,   rQ      s    z	Call.done)callbackr%   c                 C   s   t || }| j| d S r3   )r   rF   add_done_callback)r(   rX   cbr+   r+   r,   rY      s    
zCall.add_done_callbackc                 C   s
   | j  S r3   )rF   time_remainingr.   r+   r+   r,   r[      s    zCall.time_remainingc                    s   | j  I d H }t|S r3   )rF   r!   r   rC   r(   Zraw_metadata_tupler+   r+   r,   r!      s    zCall.initial_metadatac                    s   | j  I d H  }t|S r3   )rF   rA   r"   r   rC   r\   r+   r+   r,   r"      s    zCall.trailing_metadatac                    s   | j  I d H  }tj| S r3   )rF   rA   r    r
   rB   )r(   Zcygrpc_coder+   r+   r,   r      s    z	Call.codec                    s   | j  I d H  S r3   )rF   rA   r#   r.   r+   r+   r,   r#     s    zCall.detailsc                    s   | j  I d H  S r3   )rF   rA   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 r3   )rF   is_locally_cancelledasyncioCancelledErrorr    r<   r=   OKrD   r!   rA   )r(   r    r+   r+   r,   _raise_for_status  s    
 zCall._raise_for_statusc                 C   s
   t | jS r3   )reprrF   r.   r+   r+   r,   r2     s    z
Call._reprc                 C   s   |   S r3   r4   r.   r+   r+   r,   r5     s    zCall.__repr__c                 C   s   |   S r3   r4   r.   r+   r+   r,   r6     s    zCall.__str__)&r1   r9   r:   r;   r^   AbstractEventLoopr>   r<   r=   r   Z_AioCallr   r   r   r   r   r'   rT   boolrU   r?   rR   rV   rQ   r   rY   r   floatr[   r!   r"   r    r#   r$   ra   r2   r5   r6   r+   r+   r+   r,   r      s<   



	r   c                   @   s   e Zd ZdZdZdZdS )	_APIStyler   r      N)r1   r9   r:   UNKNOWNASYNC_GENERATORREADER_WRITERr+   r+   r+   r,   rf     s   rf   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 r3   )rl   )r(   rm   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&   rV   rl   r.   r)   r+   r,   rV   *  s    

z_UnaryResponseMixin.cancelNc                 c   st   z| j E dH }W n( tjk
r8   |  s2|    Y nX |tjkrl| j rXt qpt	| jj
| jjn|S dS )z+Wait till the ongoing RPC request finishes.N)rl   r^   r_   rU   rV   r   EOFrF   r]   rD   r   Z_status)r(   responser+   r+   r,   	__await__1  s    


z_UnaryResponseMixin.__await__)r1   r9   r:   r^   Taskr>   rn   rd   rV   r   r   r   rr   r@   r+   r+   r)   r,   rk   $  s   

rk   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eef d
ddZ  ZS )_StreamResponseMixin_message_aiter_preparation_response_style)preparationc                 C   s   d | _ || _tj| _d S r3   )ru   rv   rf   rh   rw   )r(   rx   r+   r+   r,   _init_stream_response_mixinT  s    z0_StreamResponseMixin._init_stream_response_mixinstylec                 C   s,   | j tjkr|| _ n| j |k	r(ttd S r3   )rw   rf   rh   r   
UsageError_API_STYLE_ERRORr(   r{   r+   r+   r,   _update_response_styleY  s    
z+_StreamResponseMixin._update_response_styler-   c                    s    t   r| j  dS dS d S ro   )r&   rV   rv   r.   r)   r+   r,   rV   _  s    

z_StreamResponseMixin.cancelc                 C  s@   |   I d H }|tjk	r.|V  |   I d H }q|  I d H  d S r3   )_readr   rp   ra   )r(   messager+   r+   r,   _fetch_stream_responsesf  s
    
z,_StreamResponseMixin._fetch_stream_responsesc                 C   s&   |  tj | jd kr |  | _| jS r3   )r   rf   ri   ru   r   r.   r+   r+   r,   	__aiter__o  s    

z_StreamResponseMixin.__aiter__c                    sl   | j I d H  z| j I d H }W n( tjk
rH   |  sB|    Y nX |tjkrZtjS t	
|| jS d S r3   )rv   rF   Zreceive_serialized_messager^   r_   rU   rV   r   rp   r
   deserializerI   )r(   Zraw_responser+   r+   r,   r   u  s    
 z_StreamResponseMixin._readc                    sR   |   r|  I d H  tjS | tj |  I d H }|tjkrN|  I d H  |S r3   )rQ   ra   r   rp   r   rf   rj   r   )r(   Zresponse_messager+   r+   r,   read  s    
z_StreamResponseMixin.read)r1   r9   r:   r   r   r>   r^   rs   rf   ry   r   rd   rV   r   r   r   r	   r   r   r@   r+   r+   r)   r,   rt   O  s   

	rt   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 k	r6| j| || _tj	| _
nd | _tj| _
d S )NF)r^   Eventr   r   rE   create_task_consume_request_iteratorr   rf   ri   r   rj   )r(   r   r+   r+   r,   _init_stream_request_mixin  s    

z._StreamRequestMixin._init_stream_request_mixinrz   c                 C   s   | j |k	rttd S r3   )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 k	r| j  dS dS d S ro   )r&   rV   r   r.   r)   r+   r,   rV     s
    


z_StreamRequestMixin.cancelc                 C   s   | j   d S r3   )r   setr.   r+   r+   r,   _metadata_sent_observer  s    z+_StreamRequestMixin._metadata_sent_observerN)r   r%   c                    s  zt |st|drz|2 zZ3 d H W }z| |I d H  W q tk
rr } ztd| W Y  W d S d }~X Y qX q6 nX|D ]R}z| |I d H  W q~ tk
r } ztd| W Y  W d S d }~X Y q~X q~|  I d H  W n$   tdt	  | 
  Y nX d S )Nr   z2Exception while consuming the request_iterator: %sz,Client request_iterator raised exception:
%s)inspect
isasyncgenrP   _writer   _LOGGERdebug_done_writing	traceback
format_excrV   )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 nh tjk
r } z"| jt| | 	 I d H  W 5 d }~X Y n( tjk
r   |  s|    Y nX d S r3   )rQ   r^   ZInvalidStateError_RPC_ALREADY_FINISHED_DETAILSr   _RPC_HALF_CLOSED_DETAILSr   is_setwaitra   r
   	serializerH   rF   Zsend_serialized_messager   ZInternalErrorZset_internal_errorr?   r_   rU   rV   )r(   r   serialized_requesterrr+   r+   r,   r     s*    


 z_StreamRequestMixin._writec                    sZ   |   rd S | jsVd| _z| j I d H  W n( tjk
rT   |  sN|    Y nX d S )NT)rQ   r   rF   Zsend_receive_closer^   r_   rU   rV   r.   r+   r+   r,   r     s    z!_StreamRequestMixin._done_writingc                    s    |  tj | |I d H  d S r3   )r   rf   rj   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   rf   rj   r   r.   r+   r+   r,   done_writing  s    z _StreamRequestMixin.done_writingc                    s*   | j  I d H  |  r&|  I d H  d S r3   )r   r   rQ   ra   r.   r+   r+   r,   wait_for_connection  s    z'_StreamRequestMixin.wait_for_connection)r1   r9   r:   r^   r   r>   rd   r   rs   rf   r   r   r   rV   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   deadlinerK   credentialswait_for_readychannelmethodrL   rM   rN   r%   c                    sP   t  ||||||||	|
 || _t | _|
|  | _	| 
| j	 d S r3   )r&   r'   callr   r   build_census_context_contextr   _invoker   rn   r(   r   r   rK   r   r   r   r   rL   rM   rN   r)   r+   r,   r'   (  s    
zUnaryUnaryCall.__init__r-   c                    sx   t | j| j}z| j|| j| jI d H }W n& tj	k
rT   | 
 sP|   Y nX | j rnt || jS tjS d S r3   )r
   r   r   rH   rF   Zunary_unaryrG   r   r^   r_   rU   rV   is_okr   rI   r   rp   )r(   r   serialized_responser+   r+   r,   r   A  s&       
 zUnaryUnaryCall._invokec                    s&   | j I d H  |  r"|  I d H  d S r3   )r   rQ   ra   r.   r+   r+   r,   r   X  s    z"UnaryUnaryCall.wait_for_connection)r1   r9   r:   r;   r   r>   r^   rs   r   re   r   r<   CallCredentialsrd   r   
AioChannelbytesr   r   rc   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                    sP   t  ||||||||	|
 || _t | _|
|  | _	| 
| j	 d S r3   )r&   r'   r   r   r   r   r   r   _send_unary_requestr   ry   r   r)   r+   r,   r'   h  s    
zUnaryStreamCall.__init__r-   c                    s\   t | j| j}z| j|| j| jI d H  W n( tj	k
rV   | 
 sP|    Y nX d S r3   )r
   r   r   rH   rF   Zinitiate_unary_streamrG   r   r^   r_   rU   rV   )r(   r   r+   r+   r,   r     s       z#UnaryStreamCall._send_unary_requestc                    s&   | j I d H  |  r"|  I d H  d S r3   )r   rQ   ra   r.   r+   r+   r,   r     s    z#UnaryStreamCall.wait_for_connection)r1   r9   r:   r;   r   r>   r^   rs   r   re   r   r<   r   rd   r   r   r   r   r   rc   r'   r   r   r   r@   r+   r+   r)   r,   r   ^  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   rK   r   r   r   r   rL   rM   rN   r%   c                    sL   t  ||||||||	|
 t | _| | | |
| 	  d S r3   )
r&   r'   r   r   r   r   r   rn   r   _conduct_rpcr(   r   r   rK   r   r   r   r   rL   rM   rN   r)   r+   r,   r'     s    

zStreamUnaryCall.__init__r-   c                    sl   z | j | j| j| jI d H }W n( tjk
rH   |  sB|    Y nX | j 	 rbt
|| jS tjS d S r3   )rF   Zstream_unaryrG   r   r   r^   r_   rU   rV   r   r
   r   rI   r   rp   )r(   r   r+   r+   r,   r     s       
 zStreamUnaryCall._conduct_rpc)r1   r9   r:   r;   r   r   re   r   r<   r   rd   r   r   r   r   r   r^   rc   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                    sV   t  ||||||||	|
 t | _| j|  | _	| 
| | | j	 d S r3   )r&   r'   r   r   r   r   rE   r   _prepare_rpcr   r   ry   r   r)   r+   r,   r'     s    

zStreamStreamCall.__init__c                    sL   z | j | j| j| jI dH  W n& tjk
rF   |  sB|   Y nX 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)	rF   Zinitiate_stream_streamrG   r   r   r^   r_   rU   rV   r.   r+   r+   r,   r     s      zStreamStreamCall._prepare_rpc)r1   r9   r:   r;   r^   rs   r>   r   r   re   r   r<   r   rd   r   r   r   r   r   rc   r'   r   r@   r+   r+   r)   r,   r     s   

r   ):r;   r^   enum	functoolsr   r   loggingr   typingr   r   r   r   r   r   r	   r<   r
   Zgrpc._cythonr    r   rG   r   Z_typingr   r   r   r   r   r   r   r   __all__rW   rS   r   r   r}   Z_OK_CALL_REPRESENTATIONr/   	getLoggerr1   r   ZRpcErrorr   ZAioRpcStatusrD   r   IntEnumrf   rk   rt   r   r   r   r   r   r+   r+   r+   r,   <module>   sr   $

k `+G 	@9
   4
   