a
    v=ic5                     @   sV  d Z ddlZddl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ZG dd dejZG dd dejZdd Zdd ZG dd dejZdd Zdd Zdd Zdd Zdd  ZG d!d" d"ed#d$ej Z!d%d& Z"d'd( Z#G d)d* d*ej$Z%G d+d, d,ej&Z'd-d. Z(dS )/zCTranslates gRPC's server-side API into gRPC's server-side Beta API.    N)_common)	_metadata)
interfaces)cardinality)style)abandonment)logging_pool)stream)face   c                   @   s$   e Zd Zdd Zdd Zdd ZdS )_ServerProtocolContextc                 C   s
   || _ d S N_servicer_contextselfservicer_context r   n/home/droni/.local/share/virtualenvs/DPS-5Je3_V2c/lib/python3.9/site-packages/grpc/beta/_server_adaptations.py__init__%   s    z_ServerProtocolContext.__init__c                 C   s
   | j  S r   )r   peerr   r   r   r   r   (   s    z_ServerProtocolContext.peerc                 C   s   d S r   r   r   r   r   r   !disable_next_response_compression+   s    z8_ServerProtocolContext.disable_next_response_compressionN)__name__
__module____qualname__r   r   r   r   r   r   r   r   #   s   r   c                   @   sd   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd Zdd Zdd ZdS )_FaceServicerContextc                 C   s
   || _ d S r   r   r   r   r   r   r   1   s    z_FaceServicerContext.__init__c                 C   s
   | j  S r   )r   	is_activer   r   r   r   r   4   s    z_FaceServicerContext.is_activec                 C   s
   | j  S r   )r   time_remainingr   r   r   r   r   7   s    z#_FaceServicerContext.time_remainingc                 C   s   t dd S )Nz6add_abortion_callback no longer supported server-side!)NotImplementedError)r   Zabortion_callbackr   r   r   add_abortion_callback:   s    z*_FaceServicerContext.add_abortion_callbackc                 C   s   | j   d S r   )r   cancelr   r   r   r   r!   >   s    z_FaceServicerContext.cancelc                 C   s
   t | jS r   )r   r   r   r   r   r   protocol_contextA   s    z%_FaceServicerContext.protocol_contextc                 C   s   t | j S r   )r   betar   invocation_metadatar   r   r   r   r$   D   s    z(_FaceServicerContext.invocation_metadatac                 C   s   | j t| d S r   )r   Zsend_initial_metadatar   unbeta)r   initial_metadatar   r   r   r&   G   s    z%_FaceServicerContext.initial_metadatac                 C   s   | j t| d S r   )r   Zset_terminal_metadatar   r%   )r   terminal_metadatar   r   r   r'   K   s    z&_FaceServicerContext.terminal_metadatac                 C   s   | j | d S r   )r   Zset_code)r   coder   r   r   r(   O   s    z_FaceServicerContext.codec                 C   s   | j | d S r   )r   Zset_details)r   detailsr   r   r   r)   R   s    z_FaceServicerContext.detailsN)r   r   r   r   r   r   r    r!   r"   r$   r&   r'   r(   r)   r   r   r   r   r   /   s   r   c                    s    fdd}|S )Nc                    s    | t |S r   r   )requestr   unary_request_inliner   r   
adaptationX   s    z/_adapt_unary_request_inline.<locals>.adaptationr   )r-   r.   r   r,   r   _adapt_unary_request_inlineV   s    r/   c                    s    fdd}|S )Nc                    s    | t |S r   r*   )request_iteratorr   stream_request_inliner   r   r.   a   s    z0_adapt_stream_request_inline.<locals>.adaptationr   )r2   r.   r   r1   r   _adapt_stream_request_inline_   s    r3   c                   @   sD   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dS )	_Callbackc                 C   s    t  | _g | _d| _d| _d S NF)	threading	Condition
_condition_values_terminated
_cancelledr   r   r   r   r   j   s    
z_Callback.__init__c                 C   s@   | j & | j| | j   W d    n1 s20    Y  d S r   )r8   r9   append
notify_allr   valuer   r   r   consumep   s    z_Callback.consumec                 C   s:   | j   d| _| j   W d    n1 s,0    Y  d S NT)r8   r:   r=   r   r   r   r   	terminateu   s    z_Callback.terminatec                 C   sF   | j , | j| d| _| j   W d    n1 s80    Y  d S rA   )r8   r9   r<   r:   r=   r>   r   r   r   consume_and_terminatez   s    z_Callback.consume_and_terminatec                 C   s:   | j   d| _| j   W d    n1 s,0    Y  d S rA   )r8   r;   r=   r   r   r   r   r!      s    z_Callback.cancelc                 C   s|   | j b | jrt q| jr8| jdW  d    S | jrNW d    d S | j   qW d    n1 sn0    Y  d S Nr   )r8   r;   r   	Abandonedr9   popr:   waitr   r   r   r   draw_one_value   s    
z_Callback.draw_one_valuec                 C   sn   | j T | jrt q| jr@t| j}d | _|W  d    S | j   qW d    n1 s`0    Y  d S r   )r8   r;   r   rE   r:   tupler9   rG   )r   Z
all_valuesr   r   r   draw_all_values   s    

z_Callback.draw_all_valuesN)
r   r   r   r   r@   rB   rC   r!   rH   rJ   r   r   r   r   r4   h   s   r4   c                    s8   t   fdd}t j|d}d|_|  d S )Nc                     sL   D ]:}   r r d S  |    r8 r d S q   d S r   )r   is_setr@   rB   )r+   request_consumerr0   r   Zthread_joinedr   r   pipe_requests   s    
z/_run_request_pipe_thread.<locals>.pipe_requests)targetT)r6   EventThreaddaemonstart)r0   rM   r   rN   Zrequest_pipe_threadr   rL   r   _run_request_pipe_thread   s
    	rT   c                    s    fdd}|S )Nc                    s8   t  }||jst  | |jt| | d S rD   )r4   add_callbackr!   r   rE   rC   r   rJ   )r+   r   callbackunary_unary_eventr   r   r.      s    z,_adapt_unary_unary_event.<locals>.adaptationr   )rX   r.   r   rW   r   _adapt_unary_unary_event   s    rY   c                    s    fdd}|S )Nc                 3   sJ   t  }||jst  | |t| | }|d u r>d S |V  q*d S r   )r4   rU   r!   r   rE   r   rH   )r+   r   rV   responseunary_stream_eventr   r   r.      s    z-_adapt_unary_stream_event.<locals>.adaptationr   )r\   r.   r   r[   r   _adapt_unary_stream_event   s    r]   c                    s    fdd}|S )Nc                    sB   t  }||jst  |jt|}t| || | d S rD   )	r4   rU   r!   r   rE   rC   r   rT   rJ   )r0   r   rV   rM   stream_unary_eventr   r   r.      s    z-_adapt_stream_unary_event.<locals>.adaptationr   )r_   r.   r   r^   r   _adapt_stream_unary_event   s    r`   c                    s    fdd}|S )Nc                 3   sT   t  }||jst  |t|}t| || | }|d u rHd S |V  q4d S r   )r4   rU   r!   r   rE   r   rT   rH   )r0   r   rV   rM   rZ   stream_stream_eventr   r   r.      s    z._adapt_stream_stream_event.<locals>.adaptationr   )rb   r.   r   ra   r   _adapt_stream_stream_event   s    rc   c                   @   s   e Zd ZdS )_SimpleMethodHandlerN)r   r   r   r   r   r   r   rd      s   rd   Z_MethodHandler)Zrequest_streamingZresponse_streamingrequest_deserializerresponse_serializerZunary_unaryZunary_streamZstream_unaryZstream_streamc              
   C   s  | j t jju r| jtjju r8tdd||t| jd d d S | jtjj	u rbtdd||d t| j
d d S | jtjju rtdd||d d t| jd S | jtjju rtdd||d d d t| jS n| j t jju rv| jtjju rtdd||t| jd d d S | jtjj	u rtdd||d t| jd d S | jtjju rJtdd||d d t| jd S | jtjju rvtdd||d d d t| jS t d S )NFT)r   ZServiceZINLINEr   ZCardinalityZUNARY_UNARYrd   r/   Zunary_unary_inlineZUNARY_STREAMZunary_stream_inlineZSTREAM_UNARYr3   Zstream_unary_inlineZSTREAM_STREAMZstream_stream_inlineZEVENTrY   rX   r]   r\   r`   r_   rc   rb   
ValueError)implementationre   rf   r   r   r   _simple_method_handler  sr    





ri   c                 C   s:   | pi } i }| D ]$}t |d |d }| | ||< q|S )Nr      )r   Zfully_qualified_method)Zmethod_pair_mapflat_mapZmethod_pairmethodr   r   r   _flatten_method_pair_map3  s    rm   c                   @   s   e Zd Zdd Zdd ZdS )_GenericRpcHandlerc                 C   s(   t || _t || _t || _|| _d S r   )rm   _method_implementations_request_deserializers_response_serializers_multi_method_implementation)r   Zmethod_implementationsmulti_method_implementationrequest_deserializersresponse_serializersr   r   r   r   >  s    z_GenericRpcHandler.__init__c                 C   sf   | j |j}|d ur6t|| j|j| j|jS | jd u rDd S zW d S  tjy`   Y d S 0 d S r   )	ro   getrl   ri   rp   rq   rr   r
   ZNoSuchMethodError)r   Zhandler_call_detailsZmethod_implementationr   r   r   serviceH  s    
z_GenericRpcHandler.serviceN)r   r   r   r   rw   r   r   r   r   rn   <  s   
rn   c                   @   sD   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dS )_Serverc                 C   s
   || _ d S r   )_grpc_server)r   Zgrpc_serverr   r   r   r   [  s    z_Server.__init__c                 C   s   | j |S r   )ry   add_insecure_port)r   addressr   r   r   rz   ^  s    z_Server.add_insecure_portc                 C   s   | j ||S r   )ry   add_secure_port)r   r{   Zserver_credentialsr   r   r   r|   a  s    z_Server.add_secure_portc                 C   s   | j   d S r   ry   rS   r   r   r   r   rS   d  s    z_Server.startc                 C   s   | j |S r   ry   stop)r   Zgracer   r   r   r   g  s    z_Server.stopc                 C   s   | j   | S r   r}   r   r   r   r   	__enter__j  s    
z_Server.__enter__c                 C   s   | j d  dS r5   r~   )r   exc_typeexc_valexc_tbr   r   r   __exit__n  s    z_Server.__exit__N)
r   r   r   r   rz   r|   rS   r   r   r   r   r   r   r   rx   Y  s   rx   c                 C   sF   t | |||}|d u r.t|d u r&tn|}n|}ttj||fdS )N)handlers)rn   r   pool_DEFAULT_POOL_SIZErx   grpcserver)Zservice_implementationsrs   rt   ru   Zthread_poolZthread_pool_sizeZgeneric_rpc_handlerZeffective_thread_poolr   r   r   r   s  s    r   ))__doc__collectionsr6   r   r   Z	grpc.betar   r   Zgrpc.framework.commonr   r   Zgrpc.framework.foundationr   r   r	   Zgrpc.framework.interfaces.facer
   r   ZGRPCServicerContextr   ZServicerContextr   r/   r3   ZConsumerr4   rT   rY   r]   r`   rc   
namedtupleZRpcMethodHandlerrd   ri   rm   ZGenericRpcHandlerrn   Serverrx   r   r   r   r   r   <module>   s@   '		6

	2	