U
    ËZj9  ã                   @   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   ú`/var/www/html/TRUCKING_PROJECT/venv/lib/python3.8/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   /   s    z_FaceServicerContext.__init__c                 C   s
   | j  ¡ S r   )r   Ú	is_activer   r   r   r   r   2   s    z_FaceServicerContext.is_activec                 C   s
   | j  ¡ S r   )r   Útime_remainingr   r   r   r   r   5   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_callback8   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_context@   s    z%_FaceServicerContext.protocol_contextc                 C   s   t  | j ¡ ¡S r   )r   Úbetar   Úinvocation_metadatar   r   r   r   r$   C   s    z(_FaceServicerContext.invocation_metadatac                 C   s   | j  t |¡¡ d S r   )r   Zsend_initial_metadatar   Úunbeta)r   Úinitial_metadatar   r   r   r&   F   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(   P   s    z_FaceServicerContext.codec                 C   s   | j  |¡ d S r   )r   Zset_details)r   Údetailsr   r   r   r)   S   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_inlineW   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 5 Q R X d S r   )r8   r9   ÚappendÚ
notify_all©r   Úvaluer   r   r   Úconsumep   s    z_Callback.consumec              	   C   s&   | j � d| _| j  ¡  W 5 Q R X d S ©NT)r8   r:   r=   r   r   r   r   Ú	terminateu   s    z_Callback.terminatec              	   C   s2   | j �" | j |¡ d| _| j  ¡  W 5 Q R X 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 5 Q R X d S rA   )r8   r;   r=   r   r   r   r   r!   €   s    z_Callback.cancelc              
   C   sd   | j �T | jrt ¡ ‚q| jr6| j d¡W  5 Q R £ S | jrJW 5 Q R £ d S | j  ¡  qW 5 Q R X d S ©Nr   )r8   r;   r   Ú	Abandonedr9   Úpopr:   Úwaitr   r   r   r   Údraw_one_value…   s    
z_Callback.draw_one_valuec              
   C   sX   | j �H | jrt ¡ ‚q| jr>t| jƒ}d | _|W  5 Q R £ S | j  ¡  qW 5 Q R X 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   i   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 k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 k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kr¸| jtjjkr8tdd||t| jƒd d d ƒS | jtjj	krbtdd||d t| j
ƒd d ƒS | jtjjkrŒtdd||d d t| jƒd ƒS | jtjjkr¶tdd||d d d t| jƒƒS n¾| j t jjk�rv| jtjjkròtdd||t| jƒd d d ƒS | jtjj	k�rtdd||d t| jƒd d ƒS | jtjjk�rJtdd||d d t| jƒd ƒS | jtjjk�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  sÂ    ø
ø
ÿöÿÿøø
ø
øÿø
ri   c                 C   s:   | pi } i }| D ]$}t  |d |d ¡}| | ||< q|S )Nr   é   )r   Zfully_qualified_method)Zmethod_pair_mapZflat_mapZmethod_pairÚmethodr   r   r   Ú_flatten_method_pair_maps  s    rl   c                   @   s   e Zd Zdd„ Zdd„ ZdS )Ú_GenericRpcHandlerc                 C   s(   t |ƒ| _t |ƒ| _t |ƒ| _|| _d S r   )rl   Ú_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   sh   | j  |j¡}|d k	r6t|| j |j¡| j |j¡ƒS | jd krDd S zW d S  tjk
rb   Y d S X d S r   )	rn   Úgetrk   ri   ro   rp   rq   r
   ZNoSuchMethodError)r   Zhandler_call_detailsZmethod_implementationr   r   r   Úservice�  s    ÿý
z_GenericRpcHandler.serviceN)r   r   r   r   rv   r   r   r   r   rm   |  s   rm   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   )rx   Úadd_insecure_port)r   Úaddressr   r   r   ry   ¦  s    z_Server.add_insecure_portc                 C   s   | j  ||¡S r   )rx   Úadd_secure_port)r   rz   Zserver_credentialsr   r   r   r{   ©  s    z_Server.add_secure_portc                 C   s   | j  ¡  d S r   ©rx   rS   r   r   r   r   rS   ¬  s    z_Server.startc                 C   s   | j  |¡S r   ©rx   Ústop)r   Zgracer   r   r   r~   ¯  s    z_Server.stopc                 C   s   | j  ¡  | S r   r|   r   r   r   r   Ú	__enter__²  s    
z_Server.__enter__c                 C   s   | j  d ¡ dS r5   r}   )r   Úexc_typeÚexc_valÚexc_tbr   r   r   Ú__exit__¶  s    z_Server.__exit__N)
r   r   r   r   ry   r{   rS   r~   r   rƒ   r   r   r   r   rw   ¢  s   rw   c                 C   sF   t | |||ƒ}|d kr.t |d kr&tn|¡}n|}ttj||fd�ƒS )N)Úhandlers)rm   r   ÚpoolÚ_DEFAULT_POOL_SIZErw   ÚgrpcÚserver)Zservice_implementationsrr   rs   rt   Zthread_poolZthread_pool_sizeZgeneric_rpc_handlerZeffective_thread_poolr   r   r   rˆ   »  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   rl   ZGenericRpcHandlerrm   ÚServerrw   rˆ   r   r   r   r   Ú<module>   sF   )		5
þòh	&