o
    ib                     @   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+dedej,de+f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 d0d1 d1e3e2e.ej7Z7dS )2z6Invocation-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"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__ I/var/www/edux/Edux_v2/venv/lib/python3.10/site-packages/grpc/aio/_call.pyr%   L   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   e      zAioRpcError.codec                 C   r+   )zkAccesses the details sent by the server.

        Returns:
          The description of the error.
        )r   r,   r)   r)   r*   r!   m   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   u   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r1   r,   r)   r)   r*   __repr__      zAioRpcError.__repr__c                 C   r2   r3   r4   r,   r)   r)   r*   __str__   r6   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)r0   
__module____qualname____doc__grpc
StatusCode__annotations__r   strr   r%   r   r!   r   r    r"   r1   r5   r7   r9   __classcell__r)   r)   r'   r*   r   ?   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   rB   r)   r)   r*   _create_rpc_error   s   rE   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 r3   )rF   rG   tuplerH   rI   rJ   )r&   rK   rL   rM   rN   rO   r)   r)   r*   r%      s
   

zCall.__init__c                 C   s*   t | dr| j s| t d S d S d S )NrG   )hasattrrG   done_cancel_GC_CANCELLATION_DETAILSr,   r)   r)   r*   __del__   s
   

zCall.__del__c                 C   
   | j  S r3   )rG   	cancelledr,   r)   r)   r*   rW         
zCall.cancelledr!   c                 C   s   | j  s| j | dS dS )z0Forwards the application cancellation reasoning.TF)rG   rR   cancel)r&   r!   r)   r)   r*   rS      s   
zCall._cancelc                 C   s
   |  tS r3   )rS   _LOCAL_CANCELLATION_DETAILSr,   r)   r)   r*   rY      rX   zCall.cancelc                 C   rV   r3   )rG   rR   r,   r)   r)   r*   rR      rX   z	Call.donecallbackc                 C   s   t || }| j| d S r3   )r   rG   add_done_callback)r&   r[   cbr)   r)   r*   r\      s   
zCall.add_done_callbackc                 C   rV   r3   )rG   time_remainingr,   r)   r)   r*   r^      rX   zCall.time_remainingc                    s   | j  I d H }t|S r3   )rG   r   r   rD   r&   raw_metadata_tupler)   r)   r*   r      s   
zCall.initial_metadatac                    s    | j  I d H  }t|S r3   )rG   rB   r    r   rD   r_   r)   r)   r*   r       s
   
zCall.trailing_metadatac                    s    | j  I d H  }tj| S r3   )rG   rB   r   r	   rC   )r&   cygrpc_coder)   r)   r*   r      s   
z	Call.codec                       | j  I d H  S r3   )rG   rB   r!   r,   r)   r)   r*   r!         zCall.detailsc                    rb   r3   )rG   rB   r"   r,   r)   r)   r*   r"      rc   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 r3   )rG   is_locally_cancelledasyncioCancelledErrorr   r=   r>   OKrE   r   rB   )r&   r   r)   r)   r*   _raise_for_status  s   
zCall._raise_for_statusc                 C   s
   t | jS r3   )reprrG   r,   r)   r)   r*   r1     rX   z
Call._reprc                 C   r2   r3   r4   r,   r)   r)   r*   r5     r6   zCall.__repr__c                 C   r2   r3   r4   r,   r)   r)   r*   r7     r6   zCall.__str__r#   N)&r0   r:   r;   r<   re   AbstractEventLoopr?   r=   r>   r
   _AioCallr   r   r   r   r   r%   rU   boolrW   r@   rS   rY   rR   r   r\   r   floatr^   r   r    r   r!   r"   rh   r1   r5   r7   r)   r)   r)   r*   r      sJ   
 





	r   c                   @   s   e Zd ZdZdZdZdS )	_APIStyler   r      N)r0   r:   r;   UNKNOWNASYNC_GENERATORREADER_WRITERr)   r)   r)   r*   ro     s    ro   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 r3   )ru   )r&   rv   r)   r)   r*   _init_unary_response_mixin  rX   z._UnaryResponseMixin._init_unary_response_mixinr#   c                       t   r| j  dS dS NTF)r$   rY   ru   r,   r'   r)   r*   rY   !     

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)ru   re   rf   rW   rY   r
   EOFrG   rd   rE   r   _status)r&   responser)   r)   r*   	__await__(  s    

z_UnaryResponseMixin.__await__)r0   r:   r;   re   Taskr?   rw   rm   rY   r   r   r   r~   rA   r)   r)   r'   r*   rt     s
   
 
 rt   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fddZ  ZS )_StreamResponseMixin_message_aiter_preparation_response_stylepreparationc                 C   s   d | _ || _tj| _d S r3   )r   r   ro   rq   r   )r&   r   r)   r)   r*   _init_stream_response_mixinK  s   z0_StreamResponseMixin._init_stream_response_mixinstylec                 C   s.   | j tju r|| _ d S | j |urttd S r3   )r   ro   rq   r
   
UsageError_API_STYLE_ERRORr&   r   r)   r)   r*   _update_response_styleP  s
   


z+_StreamResponseMixin._update_response_styler#   c                    rx   ry   )r$   rY   r   r,   r'   r)   r*   rY   V  rz   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 r3   )_readr
   r{   rh   )r&   messager)   r)   r*   _fetch_stream_responses]  s   

z,_StreamResponseMixin._fetch_stream_responsesc                 C   s&   |  tj | jd u r|  | _| jS r3   )r   ro   rr   r   r   r,   r)   r)   r*   	__aiter__f  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 r3   )r   rG   receive_serialized_messagere   rf   rW   rY   r
   r{   r	   deserializerJ   )r&   raw_responser)   r)   r*   r   l  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 r3   )rR   rh   r
   r{   r   ro   rs   r   )r&   response_messager)   r)   r*   read  s   
z_StreamResponseMixin.read)r0   r:   r;   r   r   r?   re   r   ro   r   r   rm   rY   r   r   r   r   rA   r)   r)   r'   r*   r   F  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)re   Eventr   r   rF   create_task_consume_request_iteratorr   ro   rr   r   rs   )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 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 ur| j  dS dS ry   )r$   rY   r   r,   r'   r)   r*   rY     s
   


z_StreamRequestMixin.cancelc                 C   s   | j   d S r3   )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
isasyncgenrQ   _writer   _LOGGERdebug_done_writing	traceback
format_excrY   )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 r3   )rR   re   InvalidStateError_RPC_ALREADY_FINISHED_DETAILSr   _RPC_HALF_CLOSED_DETAILSr   is_setwaitrh   r	   	serializerI   rG   send_serialized_messager
   InternalErrorset_internal_errorr@   rf   rW   rY   )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)rR   r   rG   send_receive_closere   rf   rW   rY   r,   r)   r)   r*   r     s   z!_StreamRequestMixin._done_writingc                    s"   |  tj | |I d H  d S r3   )r   ro   rs   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   ro   rs   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 r3   )r   r   rR   rh   r,   r)   r)   r*   wait_for_connection  s
   z'_StreamRequestMixin.wait_for_connectionrj   )r0   r:   r;   re   r   r?   rm   r   r   ro   r   r   r   rY   r   r   r   r   r   r   r   r   rA   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   deadlinerL   credentialswait_for_readychannelmethodrM   rN   rO   r#   Nc                    P   t  ||||||||	|
 || _t | _|
|  | _	| 
| j	 d S r3   )r$   r%   callr   r
   build_census_context_contextr   _invoker   rw   r&   r   r   rL   r   r   r   r   rM   rN   rO   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 r3   )r	   r   r   rI   rG   unary_unaryrH   r   re   rf   rW   rY   is_okr   rJ   r
   r{   )r&   r   serialized_responser)   r)   r*   r   8  s$   

zUnaryUnaryCall._invokec                    ,   | j I d H  |  r|  I d H  d S d S r3   )r   rR   rh   r,   r)   r)   r*   r   O  
   z"UnaryUnaryCall.wait_for_connectionrj   )r0   r:   r;   r<   r   r?   re   r   r   rn   r   r=   CallCredentialsrm   r
   
AioChannelbytesr   r   rk   r%   r   r   r   rA   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   rL   r   r   r   r   rM   rN   rO   r#   Nc                    r   r3   )r$   r%   r   r   r
   r   r   r   _send_unary_requestr   r   r   r'   r)   r*   r%   _  s   
zUnaryStreamCall.__init__c                    sV   t | j| j}z| j|| j| jI d H  W d S  tj	y*   | 
 s)|    w r3   )r	   r   r   rI   rG   initiate_unary_streamrH   r   re   rf   rW   rY   )r&   r   r)   r)   r*   r   z  s   
z#UnaryStreamCall._send_unary_requestc                    r   r3   )r   rR   rh   r,   r)   r)   r*   r     r   z#UnaryStreamCall.wait_for_connectionrj   )r0   r:   r;   r<   r   r?   re   r   r   rn   r   r=   r   rm   r
   r   r   r   r   rk   r%   r   r   r   rA   r)   r)   r'   r*   r   U  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   rL   r   r   r   r   rM   rN   rO   r#   Nc                    sL   t  ||||||||	|
 t | _| | | |
| 	  d S r3   )
r$   r%   r   r
   r   r   r   rw   r   _conduct_rpcr&   r   r   rL   r   r   r   r   rM   rN   rO   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 r3   )rG   stream_unaryrH   r   r   re   rf   rW   rY   r   r	   r   rJ   r
   r{   )r&   r   r)   r)   r*   r     s   
zStreamUnaryCall._conduct_rpc)r0   r:   r;   r<   r   r   rn   r   r=   r   rm   r
   r   r   r   r   re   rk   r%   r   r   rA   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   rL   r   r   r   r   rM   rN   rO   r#   Nc                    sV   t  ||||||||	|
 t | _| j|  | _	| 
| | | j	 d S r3   )r$   r%   r   r
   r   r   rF   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)	rG   initiate_stream_streamrH   r   r   re   rf   rW   rY   r,   r)   r)   r*   r     s   zStreamStreamCall._prepare_rpc)r0   r:   r;   r<   re   r   r?   r   r   rn   r   r=   r   rm   r
   r   r   r   r   rk   r%   r   rA   r)   r)   r'   r*   r     s6   
 
	
r   )8r<   re   enum	functoolsr   r   loggingr   typingr   r   r   r   r   r   r=   r	   grpc._cythonr
    r   rH   r   _typingr   r   r   r   r   r   r   __all__rZ   rT   r   r   r   _OK_CALL_REPRESENTATIONr.   	getLoggerr0   r   RpcErrorr   AioRpcStatusrE   r   IntEnumro   rt   r   r   r   r   r   r   r)   r)   r)   r*   <module>   sl    
j
`+G 	@
9

4
