o
    ˷e_                     @   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 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,Z-G d$d% d%e+Z.G d&d' d'e+Z/G d(d) d)e+Z0G d*d+ d+e.e+ej1Z1G d,d- d-e/e+ej2Z2G d.d/ d/e0e.e+ej3Z3G d0d1 d1e0e/e+ej4Z4dS )2z6Invocation-side implementation of gRPC Asyncio Python.    N)partial)AsyncIterable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  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__ E/var/www/ideatree/venv/lib/python3.10/site-packages/grpc/aio/_call.pyr"   I   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   `      zAioRpcError.codec                 C   r(   )zkAccesses the details sent by the server.

        Returns:
          The description of the error.
        )r   r)   r&   r&   r'   r   h   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   p   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   x   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r.   r)   r&   r&   r'   __repr__      zAioRpcError.__repr__c                 C   r/   r0   r1   r)   r&   r&   r'   __str__   r3   zAioRpcError.__str__)NN)r-   
__module____qualname____doc__grpc
StatusCode__annotations__r   strr
   r"   r   r   r   r   r   r.   r2   r4   __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   r=   r&   r&   r'   _create_rpc_error   s   r@   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 r0   )rA   rB   tuplerC   rD   rE   )r#   rF   rG   rH   rI   rJ   r&   r&   r'   r"      s
   

zCall.__init__c                 C   s*   t | dr| j s| t d S d S d S )NrB   )hasattrrB   done_cancel_GC_CANCELLATION_DETAILSr)   r&   r&   r'   __del__   s
   

zCall.__del__c                 C   
   | j  S r0   )rB   	cancelledr)   r&   r&   r'   rR         
zCall.cancelledr   c                 C   s   | j  s| j | dS dS )z0Forwards the application cancellation reasoning.TF)rB   rM   cancel)r#   r   r&   r&   r'   rN      s   
zCall._cancelc                 C   s
   |  tS r0   )rN   _LOCAL_CANCELLATION_DETAILSr)   r&   r&   r'   rT      rS   zCall.cancelc                 C   rQ   r0   )rB   rM   r)   r&   r&   r'   rM      rS   z	Call.donecallbackc                 C   s   t || }| j| d S r0   )r   rB   add_done_callback)r#   rV   cbr&   r&   r'   rW      s   
zCall.add_done_callbackc                 C   rQ   r0   )rB   time_remainingr)   r&   r&   r'   rY      rS   zCall.time_remainingc                    s   | j  I d H }t|S r0   )rB   r   r
   r?   r#   raw_metadata_tupler&   r&   r'   r      s   
zCall.initial_metadatac                    s    | j  I d H  }t|S r0   )rB   r=   r   r
   r?   rZ   r&   r&   r'   r      s   
zCall.trailing_metadatac                    s    | j  I d H  }tj| S r0   )rB   r=   r   r   r>   )r#   cygrpc_coder&   r&   r'   r      s   
z	Call.codec                       | j  I d H  S r0   )rB   r=   r   r)   r&   r&   r'   r         zCall.detailsc                    r]   r0   )rB   r=   r   r)   r&   r&   r'   r      r^   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 r0   )rB   is_locally_cancelledasyncioCancelledErrorr   r8   r9   OKr@   r   r=   )r#   r   r&   r&   r'   _raise_for_status   s   

zCall._raise_for_statusc                 C   s
   t | jS r0   )reprrB   r)   r&   r&   r'   r.      rS   z
Call._reprc                 C   r/   r0   r1   r)   r&   r&   r'   r2      r3   zCall.__repr__c                 C   r/   r0   r1   r)   r&   r&   r'   r4      r3   zCall.__str__r    N)&r-   r5   r6   r7   r`   AbstractEventLoopr:   r8   r9   r   _AioCallr   r   r   r   r
   r"   rP   boolrR   r;   rN   rT   rM   r   rW   r   floatrY   r   r   r   r   r   rc   r.   r2   r4   r&   r&   r&   r'   r      sB   
 






r   c                   @   s   e Zd ZdZdZdZdS )	_APIStyler   r      N)r-   r5   r6   UNKNOWNASYNC_GENERATORREADER_WRITERr&   r&   r&   r'   rj      s    rj   c                       sL   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	fdd	Z
  ZS )
_UnaryResponseMixin_call_responseresponse_taskc                 C   s
   || _ d S r0   )rp   )r#   rq   r&   r&   r'   _init_unary_response_mixin  rS   z._UnaryResponseMixin._init_unary_response_mixinr    c                       t   r| j  dS dS NTF)r!   rT   rp   r)   r$   r&   r'   rT        

z_UnaryResponseMixin.cancelc                 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)rp   r`   ra   rR   rT   r   EOFrB   r_   r@   r   _status)r#   responser&   r&   r'   	__await__  s   

z_UnaryResponseMixin.__await__)r-   r5   r6   r`   Taskr:   rr   rh   rT   r   ry   r<   r&   r&   r$   r'   ro      s
   
 
ro   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 r0   )r|   r}   rj   rl   r~   )r#   r   r&   r&   r'   _init_stream_response_mixin-  s   z0_StreamResponseMixin._init_stream_response_mixinstylec                 C   s.   | j tju r|| _ d S | j |urttd S r0   )r~   rj   rl   r   
UsageError_API_STYLE_ERRORr#   r   r&   r&   r'   _update_response_style2  s
   


z+_StreamResponseMixin._update_response_styler    c                    rs   rt   )r!   rT   r}   r)   r$   r&   r'   rT   8  ru   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 r0   )_readr   rv   rc   )r#   messager&   r&   r'   _fetch_stream_responses?  s   

z,_StreamResponseMixin._fetch_stream_responsesc                 C   s&   |  tj | jd u r|  | _| jS r0   )r   rj   rm   r|   r   r)   r&   r&   r'   	__aiter__H  s   

z_StreamResponseMixin.__aiter__c                    st   | j I d H  z
| j I d H }W n tjy*   |  s!|   |  I d H  Y nw |tj	u r3tj	S t
|| jS r0   )r}   rB   receive_serialized_messager`   ra   rR   rT   rc   r   rv   r   deserializerE   )r#   raw_responser&   r&   r'   r   N  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 r0   )rM   rc   r   rv   r   rj   rn   r   )r#   response_messager&   r&   r'   read`  s   
z_StreamResponseMixin.read)r-   r5   r6   r   r   r:   r`   rz   rj   r   r   rh   rT   r   r   r   r   r<   r&   r&   r$   r'   r{   (  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)r`   Eventr   r   rA   create_task_consume_request_iteratorr   rj   rm   r   rn   )r#   r   r&   r&   r'   _init_stream_request_mixint  s   
z._StreamRequestMixin._init_stream_request_mixinr   c                 C   s   | j |ur
ttd S r0   )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 rt   )r!   rT   r   r)   r$   r&   r'   rT     s
   


z_StreamRequestMixin.cancelc                 C   s   | j   d S r0   )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
isasyncgenrL   _writer   _LOGGERdebug_done_writing	traceback
format_excrT   )r#   r   request	rpc_errorr&   r&   r'   r     sD   	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yO   | 	 I d H  Y d S  tjyh   |  s^|   | 	 I d H  Y d S w r0   )rM   r`   InvalidStateError_RPC_ALREADY_FINISHED_DETAILSr   _RPC_HALF_CLOSED_DETAILSr   is_setwaitrc   r   	serializerD   rB   send_serialized_messager   InternalErrorra   rR   rT   )r#   r   serialized_requestr&   r&   r'   r     s*   


z_StreamRequestMixin._writec                    sj   |   rd S | js3d| _z| j I d H  W d S  tjy2   |  s(|   |  I d H  Y d S w d S )NT)	rM   r   rB   send_receive_closer`   ra   rR   rT   rc   r)   r&   r&   r'   r     s   z!_StreamRequestMixin._done_writingc                    s"   |  tj | |I d H  d S r0   )r   rj   rn   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   rj   rn   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 r0   )r   r   rM   rc   r)   r&   r&   r'   wait_for_connection  s
   z'_StreamRequestMixin.wait_for_connectionre   )r-   r5   r6   r`   r   r:   rh   r   rz   rj   r   r   r   rT   r   r   r   r   r   r   r   r   r<   r&   r&   r$   r'   r   n  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   deadlinerG   credentialswait_for_readychannelmethodrH   rI   rJ   r    Nc                    F   t  ||||||||	|
 || _|
|  | _| | j d S r0   )r!   r"   callr   r   _invoker   rr   r#   r   r   rG   r   r   r   r   rH   rI   rJ   r$   r&   r'   r"     s   zUnaryUnaryCall.__init__c                    sp   t | j| j}z| j|| jI d H }W n tjy(   | 	 s&| 
  Y nw | j r5t || jS tjS r0   )r   r   r   rD   rB   unary_unaryrC   r`   ra   rR   rT   is_okr   rE   r   rv   )r#   r   serialized_responser&   r&   r'   r     s$   
zUnaryUnaryCall._invokec                    ,   | j I d H  |  r|  I d H  d S d S r0   )r   rM   rc   r)   r&   r&   r'   r     
   z"UnaryUnaryCall.wait_for_connectionre   )r-   r5   r6   r7   r   r:   r`   rz   r   ri   r
   r8   CallCredentialsrh   r   
AioChannelbytesr   r   rf   r"   r   r   r   r<   r&   r&   r$   r'   r     s2   
 
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   rG   r   r   r   r   rH   rI   rJ   r    Nc                    r   r0   )r!   r"   r   r   r   _send_unary_requestr   r   r   r$   r&   r'   r"      s   zUnaryStreamCall.__init__c                    sR   t | j| j}z| j|| jI d H  W d S  tjy(   | 	 s'| 
   w r0   )r   r   r   rD   rB   initiate_unary_streamrC   r`   ra   rR   rT   )r#   r   r&   r&   r'   r   /  s   z#UnaryStreamCall._send_unary_requestc                    r   r0   )r   rM   rc   r)   r&   r&   r'   r   :  r   z#UnaryStreamCall.wait_for_connectionre   )r-   r5   r6   r7   r   r:   r`   rz   r   ri   r
   r8   r   rh   r   r   r   r   r   rf   r"   r   r   r   r<   r&   r&   r$   r'   r     s2   
 
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   rG   r   r   r   r   rH   rI   rJ   r    Nc                    sB   t  ||||||||	|
 | | | |
|   d S r0   )r!   r"   r   r   rr   r   _conduct_rpcr#   r   r   rG   r   r   r   r   rH   rI   rJ   r$   r&   r'   r"   H  s   
zStreamUnaryCall.__init__c                    sb   z| j | j| jI d H }W n tjy!   |  s|   Y nw | j  r.t	
|| jS tjS r0   )rB   stream_unaryrC   r   r`   ra   rR   rT   r   r   r   rE   r   rv   )r#   r   r&   r&   r'   r   V  s   
zStreamUnaryCall._conduct_rpc)r-   r5   r6   r7   r   r   ri   r
   r8   r   rh   r   r   r   r   r   r`   rf   r"   r   r   r<   r&   r&   r$   r'   r   @  s0    
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   rG   r   r   r   r   rH   rI   rJ   r    Nc                    sL   t  ||||||||	|
 | j|  | _| | | | j d S r0   )	r!   r"   r   rA   r   _prepare_rpcr   r   r   r   r$   r&   r'   r"   n  s   
zStreamStreamCall.__init__c                    sN   z| 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)rB   initiate_stream_streamrC   r   r`   ra   rR   rT   r)   r&   r&   r'   r   |  s   zStreamStreamCall._prepare_rpc)r-   r5   r6   r7   r`   rz   r:   r   r   ri   r
   r8   r   rh   r   r   r   r   r   rf   r"   r   r<   r&   r&   r$   r'   r   e  s2   
 

r   )5r7   r`   enum	functoolsr   r   loggingr   typingr   r   r   r8   r   grpc._cythonr    r	   rC   r
   _typingr   r   r   r   r   r   r   __all__rU   rO   r   r   r   _OK_CALL_REPRESENTATIONr+   	getLoggerr-   r   RpcErrorr   AioRpcStatusr@   r   IntEnumrj   ro   r{   r   r   r   r   r   r&   r&   r&   r'   <module>   s`   
Y
Y)Fx1)%