o
     iRc                     @   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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!Z0G d"d# d#Z1G d$d% d%ej2Z3G d&d' d'e1ee  Z4G d(d) d)e1Z5G d*d+ d+e1Z6G d,d- d-e4e1ej7Z7G d.d/ d/e5e1ej8Z8G d0d1 d1e6e4e1ej9Z9G d2d3 d3e6e5e1ej:Z:dS )4z6Invocation-side implementation of gRPC Asyncio Python.    N)partial)AnyAsyncIterator	GeneratorGenericOptionalTupleUnion)_common)cygrpc   )
_base_call)Metadata)DeserializingFunction)DoneCallbackType)EOFType)MetadataType)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 )a  Constructor.

        Args:
          code: The status code with which the RPC has been finalized.
          initial_metadata: Optional initial metadata that could be sent by the
            Server.
          trailing_metadata: Optional metadata that could be sent by the Server.
          details: Optional details explaining the reason of the error.
          debug_error_string: Optional string
        N)super__init__r   r   r   r   r    )selfr!   r"   r#   r$   r%   	__class__ n/var/www/snowflake_co_dev_github/snow_flake_back_end_deploy/env/lib/python3.10/site-packages/grpc/aio/_call.pyr(   V   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!   o      zAioRpcError.codec                 C   r.   )zkAccesses the details sent by the server.

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

        Returns:
          The initial metadata received.
        )r   r/   r,   r,   r-   r"      r0   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#      r0   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%      r0   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r4   r/   r,   r,   r-   __repr__      zAioRpcError.__repr__c                 C   r5   r6   r7   r/   r,   r,   r-   __str__   r9   zAioRpcError.__str__c                 C   s    t | | j| j| j| j| jffS r6   )typer   r   r   r   r    r/   r,   r,   r-   
__reduce__   s   zAioRpcError.__reduce__)NN)r3   
__module____qualname____doc__grpc
StatusCode__annotations__r   strr   r(   r!   r$   r"   r#   r%   r4   r8   r:   r<   __classcell__r,   r,   r*   r-   r   I   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"   rE   r,   r,   r-   _create_rpc_error   s   rH   c                   @   sl  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 ed< ee ed< d	e	j
d
edee de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 r6   )rI   rJ   tuplerK   rL   rM   )r)   rN   rO   rP   rQ   rR   r,   r,   r-   r(      s
   

zCall.__init__c                 C   s*   t | dr| j s| t d S d S d S )NrJ   )hasattrrJ   done_cancel_GC_CANCELLATION_DETAILSr/   r,   r,   r-   __del__   s
   

zCall.__del__c                 C   
   | j  S r6   )rJ   	cancelledr/   r,   r,   r-   rZ         
zCall.cancelledr$   c                 C   s   | j  s| j | dS dS )z0Forwards the application cancellation reasoning.TF)rJ   rU   cancel)r)   r$   r,   r,   r-   rV      s   
zCall._cancelc                 C   s
   |  tS r6   )rV   _LOCAL_CANCELLATION_DETAILSr/   r,   r,   r-   r\      r[   zCall.cancelc                 C   rY   r6   )rJ   rU   r/   r,   r,   r-   rU      r[   z	Call.donecallbackc                 C   s   t || }| j| d S r6   )r   rJ   add_done_callback)r)   r^   cbr,   r,   r-   r_      s   
zCall.add_done_callbackc                 C   rY   r6   )rJ   time_remainingr/   r,   r,   r-   ra      r[   zCall.time_remainingc                    s   | j  I d H }t|S r6   )rJ   r"   r   rG   r)   raw_metadata_tupler,   r,   r-   r"      s   
zCall.initial_metadatac                    s    | j  I d H  }t|S r6   )rJ   rE   r#   r   rG   rb   r,   r,   r-   r#      s
   
zCall.trailing_metadatac                    s    | j  I d H  }tj| S r6   )rJ   rE   r!   r
   rF   )r)   cygrpc_coder,   r,   r-   r!     s   
z	Call.codec                       | j  I d H  S r6   )rJ   rE   r$   r/   r,   r,   r-   r$        zCall.detailsc                    re   r6   )rJ   rE   r%   r/   r,   r,   r-   r%   	  rf   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 r6   )rJ   is_locally_cancelledasyncioCancelledErrorr!   r@   rA   OKrH   r"   rE   )r)   r!   r,   r,   r-   _raise_for_status  s   
zCall._raise_for_statusc                 C   s
   t | jS r6   )reprrJ   r/   r,   r,   r-   r4     r[   z
Call._reprc                 C   r5   r6   r7   r/   r,   r,   r-   r8     r9   zCall.__repr__c                 C   r5   r6   r7   r/   r,   r,   r-   r:     r9   zCall.__str__r&   N)&r3   r=   r>   r?   rh   AbstractEventLooprB   r@   rA   r   _AioCallr   r   r   r   r   r   r(   rX   boolrZ   rC   rV   r\   rU   r   r_   floatra   r"   r#   r!   r$   r%   rk   r4   r8   r:   r,   r,   r,   r-   r      sJ   
 






r   c                   @   s   e Zd ZdZdZdZdS )	_APIStyler   r      N)r3   r=   r>   UNKNOWNASYNC_GENERATORREADER_WRITERr,   r,   r,   r-   rr      s    rr   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 r6   )rx   )r)   ry   r,   r,   r-   _init_unary_response_mixin)  r[   z._UnaryResponseMixin._init_unary_response_mixinr&   c                       t   r| j  dS dS NTF)r'   r\   rx   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)rx   rh   ri   rZ   r\   r   EOFrJ   rg   rH   r   _status)r)   responser,   r,   r-   	__await__2  s    

z_UnaryResponseMixin.__await__)r3   r=   r>   rh   TaskrB   rz   rp   r\   r   r   r   r   rD   r,   r,   r*   r-   rw   &  s
   
 
 rw   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 r6   )r   r   rr   rt   r   )r)   r   r,   r,   r-   _init_stream_response_mixinU  s   z0_StreamResponseMixin._init_stream_response_mixinstylec                 C   s.   | j tju r|| _ d S | j |urttd S r6   )r   rr   rt   r   
UsageError_API_STYLE_ERRORr)   r   r,   r,   r-   _update_response_styleZ  s
   


z+_StreamResponseMixin._update_response_styler&   c                    r{   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 r6   )_readr   r~   rk   )r)   messager,   r,   r-   _fetch_stream_responsesf  s   

z,_StreamResponseMixin._fetch_stream_responsesc                 C   s&   |  tj | jd u r|  | _| jS r6   )r   rr   ru   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 r6   )r   rJ   receive_serialized_messagerh   ri   rZ   r\   r   r~   r
   deserializerM   )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 r6   )rU   rk   r   r~   r   rr   rv   r   )r)   response_messager,   r,   r-   read  s   
z_StreamResponseMixin.read)r3   r=   r>   r   r   rB   rh   r   rr   r   r   rp   r\   r   r   r   r	   r   r   rD   r,   r,   r*   r-   r   P  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)rh   Eventr   r   rI   create_task_consume_request_iteratorr   rr   ru   r   rv   )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 r6   )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 r6   )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
isasyncgenrT   _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 r6   )rU   rh   InvalidStateError_RPC_ALREADY_FINISHED_DETAILSr   _RPC_HALF_CLOSED_DETAILSr   is_setwaitrk   r
   	serializerL   rJ   send_serialized_messager   InternalErrorset_internal_errorrC   ri   rZ   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)rU   r   rJ   send_receive_closerh   ri   rZ   r\   r/   r,   r,   r-   r     s   z!_StreamRequestMixin._done_writingc                    s"   |  tj | |I d H  d S r6   )r   rr   rv   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   rr   rv   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 r6   )r   r   rU   rk   r/   r,   r,   r-   wait_for_connection  s
   z'_StreamRequestMixin.wait_for_connectionrm   )r3   r=   r>   rh   r   rB   rp   r   r   rr   r   r   r   r\   r   r   r   r   r   r   r   r   rD   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e de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   deadlinerO   credentialswait_for_readychannelmethodrP   rQ   rR   r&   Nc                    P   t  ||||||||	|
 || _t | _|
|  | _	| 
| j	 d S r6   )r'   r(   callr   r   build_census_context_contextr   _invoker   rz   r)   r   r   rO   r   r   r   r   rP   rQ   rR   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 r6   )r
   r   r   rL   rJ   unary_unaryrK   r   rh   ri   rZ   r\   is_okr   rM   r   r~   )r)   r   serialized_responser,   r,   r-   r   =  s$   

zUnaryUnaryCall._invokec                    ,   | j I d H  |  r|  I d H  d S d S r6   )r   rU   rk   r/   r,   r,   r-   r   S  
   z"UnaryUnaryCall.wait_for_connectionrm   )r3   r=   r>   r?   r   rB   rh   r   r   rq   r   r@   CallCredentialsrp   r   
AioChannelbytesr   r   rn   r(   r   r   r   rD   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   rO   r   r   r   r   rP   rQ   rR   r&   Nc                    r   r6   )r'   r(   r   r   r   r   r   r   _send_unary_requestr   r   r   r*   r,   r-   r(   c  s   
zUnaryStreamCall.__init__c                    sV   t | j| j}z| j|| j| jI d H  W d S  tj	y*   | 
 s)|    w r6   )r
   r   r   rL   rJ   initiate_unary_streamrK   r   rh   ri   rZ   r\   )r)   r   r,   r,   r-   r   ~  s   
z#UnaryStreamCall._send_unary_requestc                    r   r6   )r   rU   rk   r/   r,   r,   r-   r     r   z#UnaryStreamCall.wait_for_connectionrm   )r3   r=   r>   r?   r   rB   rh   r   r   rq   r   r@   r   rp   r   r   r   r   r   rn   r(   r   r   r   rD   r,   r,   r*   r-   r   Y  s:   
 
	
r   c                       sz   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e d
e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   rO   r   r   r   r   rP   rQ   rR   r&   Nc                    sL   t  ||||||||	|
 t | _| | | |
| 	  d S r6   )
r'   r(   r   r   r   r   r   rz   r   _conduct_rpcr)   r   r   rO   r   r   r   r   rP   rQ   rR   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 r6   )rJ   stream_unaryrK   r   r   rh   ri   rZ   r\   r   r
   r   rM   r   r~   )r)   r   r,   r,   r-   r     s   
zStreamUnaryCall._conduct_rpc)r3   r=   r>   r?   r   r   rq   r   r@   r   rp   r   r   r   r   r   rh   rn   r(   r   r   rD   r,   r,   r*   r-   r     s4    	
r   c                       s   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e de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   rO   r   r   r   r   rP   rQ   rR   r&   Nc                    sV   t  ||||||||	|
 t | _| j|  | _	| 
| | | j	 d S r6   )r'   r(   r   r   r   r   rI   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 )zPrepares the RPC for receiving/sending messages.

        All other operations around the stream should only happen after the
        completion of this method.
        N)	rJ   initiate_stream_streamrK   r   r   rh   ri   rZ   r\   r/   r,   r,   r-   r     s   zStreamStreamCall._prepare_rpc)r3   r=   r>   r?   rh   r   rB   r   r   rq   r   r@   r   rp   r   r   r   r   r   rn   r(   r   rD   r,   r,   r*   r-   r     s6   
 
	
r   );r?   rh   enum	functoolsr   r   loggingr   typingr   r   r   r   r   r   r	   r@   r
   grpc._cythonr    r   rK   r   _typingr   r   r   r   r   r   r   r   r   __all__r]   rW   r   r   r   _OK_CALL_REPRESENTATIONr1   	getLoggerr3   r   RpcErrorr   AioRpcStatusrH   r   IntEnumrr   rw   r   r   r   r   r   r   r,   r,   r,   r-   <module>   sp   $

j
`*C ?
9

3
