o
    aeä:  ã                   @   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   ó
   || _ d S ©N©Ú_servicer_context©ÚselfÚservicer_context© r   úMD:\Projects\ConvertPro\env\Lib\site-packages\grpc/beta/_server_adaptations.pyÚ__init__$   ó   
z_ServerProtocolContext.__init__c                 C   ó
   | j  ¡ S r   )r   Úpeer©r   r   r   r   r   '   r   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   r   r   r   r   r   r   r   r   /   r   z_FaceServicerContext.__init__c                 C   r   r   )r   Ú	is_activer   r   r   r   r    2   r   z_FaceServicerContext.is_activec                 C   r   r   )r   Útime_remainingr   r   r   r   r!   5   r   z#_FaceServicerContext.time_remainingc                 C   s   t dƒ‚)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   ó   | j  ¡  d S r   )r   Úcancelr   r   r   r   r%   =   ó   z_FaceServicerContext.cancelc                 C   s
   t | jƒS r   )r   r   r   r   r   r   Úprotocol_context@   r   z%_FaceServicerContext.protocol_contextc                 C   s   t  | j ¡ ¡S r   )r   Úbetar   Úinvocation_metadatar   r   r   r   r)   C   ó   z(_FaceServicerContext.invocation_metadatac                 C   ó   | j  t |¡¡ d S r   )r   Zsend_initial_metadatar   Úunbeta)r   Úinitial_metadatar   r   r   r-   F   ó   ÿz%_FaceServicerContext.initial_metadatac                 C   r+   r   )r   Zset_terminal_metadatar   r,   )r   Úterminal_metadatar   r   r   r/   K   r.   z&_FaceServicerContext.terminal_metadatac                 C   ó   | j  |¡ d S r   )r   Zset_code)r   Úcoder   r   r   r1   P   r*   z_FaceServicerContext.codec                 C   r0   r   )r   Zset_details)r   Údetailsr   r   r   r2   S   r*   z_FaceServicerContext.detailsN)r   r   r   r   r    r!   r#   r%   r'   r)   r-   r/   r1   r2   r   r   r   r   r   .   s    r   c                    ó   ‡ fdd„}|S )Nc                    ó   ˆ | t |ƒƒS r   ©r   )Úrequestr   ©Úunary_request_inliner   r   Ú
adaptationX   ó   ÿz/_adapt_unary_request_inline.<locals>.adaptationr   )r8   r9   r   r7   r   Ú_adapt_unary_request_inlineW   ó   r;   c                    r3   )Nc                    r4   r   r5   )Úrequest_iteratorr   ©Ústream_request_inliner   r   r9   a   r:   z0_adapt_stream_request_inline.<locals>.adaptationr   )r?   r9   r   r>   r   Ú_adapt_stream_request_inline`   r<   r@   c                   @   óD   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   sB   | j � | j |¡ | j  ¡  W d   ƒ d S 1 sw   Y  d S r   )rF   rG   ÚappendÚ
notify_all©r   Úvaluer   r   r   Úconsumep   s   "þz_Callback.consumec                 C   ó<   | j � d| _| j  ¡  W d   ƒ d S 1 sw   Y  d S ©NT)rF   rH   rK   r   r   r   r   Ú	terminateu   ó   "þz_Callback.terminatec                 C   sH   | j � | j |¡ d| _| j  ¡  W d   ƒ d S 1 sw   Y  d S rP   )rF   rG   rJ   rH   rK   rL   r   r   r   Úconsume_and_terminatez   s
   "ýz_Callback.consume_and_terminatec                 C   rO   rP   )rF   rI   rK   r   r   r   r   r%   €   rR   z_Callback.cancelc                 C   sp   | j �+ 	 | jrt ¡ ‚| jr| j d¡W  d   ƒ S | jr(	 W d   ƒ d S | j  ¡  q1 s1w   Y  d S )NTr   )rF   rI   r   Ú	AbandonedrG   ÚpoprH   Úwaitr   r   r   r   Údraw_one_value…   s   
ûù
	øÿz_Callback.draw_one_valuec                 C   s`   | j �# 	 | jrt ¡ ‚| jr t| jƒ}d | _|W  d   ƒ S | j  ¡  q1 s)w   Y  d S r   )rF   rI   r   rT   rH   ÚtuplerG   rV   )r   Z
all_valuesr   r   r   Údraw_all_values‘   s   
ù
	øÿz_Callback.draw_all_valuesN)
r   r   r   r   rN   rQ   rS   r%   rW   rY   r   r   r   r   rB   i   s    rB   c                    s8   t  ¡ ‰‡ ‡‡‡fdd„}t j|d�}d|_| ¡  d S )Nc                     sL   ˆD ]} ˆ  ¡ rˆ ¡ r d S ˆ  | ¡ ˆ  ¡ rˆ ¡ r d S qˆ  ¡  d S r   )r    Úis_setrN   rQ   )r6   ©Úrequest_consumerr=   r   Zthread_joinedr   r   Úpipe_requests£   s   
ÿz/_run_request_pipe_thread.<locals>.pipe_requests)ÚtargetT)rD   ÚEventÚThreadÚdaemonÚstart)r=   r\   r   r]   Zrequest_pipe_threadr   r[   r   Ú_run_request_pipe_threadž   s
   	rc   c                    r3   )Nc                    s8   t ƒ }| |j¡st ¡ ‚ˆ | |jt|ƒƒ | ¡ d S ©Nr   )rB   Úadd_callbackr%   r   rT   rS   r   rY   )r6   r   Úcallback©Úunary_unary_eventr   r   r9   ²   s   ýz,_adapt_unary_unary_event.<locals>.adaptationr   )rh   r9   r   rg   r   Ú_adapt_unary_unary_event±   s   ri   c                    r3   )Nc                 3   sJ   � t ƒ }| |j¡st ¡ ‚ˆ | |t|ƒƒ 	 | ¡ }|d u r!d S |V  qr   )rB   re   r%   r   rT   r   rW   )r6   r   rf   Úresponse©Úunary_stream_eventr   r   r9   Á   s   €
ÿûz-_adapt_unary_stream_event.<locals>.adaptationr   )rl   r9   r   rk   r   Ú_adapt_unary_stream_eventÀ   s   rm   c                    r3   )Nc                    sB   t ƒ }| |j¡st ¡ ‚ˆ |jt|ƒƒ}t| ||ƒ | ¡ d S rd   )	rB   re   r%   r   rT   rS   r   rc   rY   )r=   r   rf   r\   ©Ústream_unary_eventr   r   r9   Ó   s   þÿz-_adapt_stream_unary_event.<locals>.adaptationr   )ro   r9   r   rn   r   Ú_adapt_stream_unary_eventÒ   s   rp   c                    r3   )Nc                 3   sT   � t ƒ }| |j¡st ¡ ‚ˆ |t|ƒƒ}t| ||ƒ 	 | ¡ }|d u r&d S |V  qr   )rB   re   r%   r   rT   r   rc   rW   )r=   r   rf   r\   rj   ©Ústream_stream_eventr   r   r9   ä   s    €ÿÿûz._adapt_stream_stream_event.<locals>.adaptationr   )rr   r9   r   rq   r   Ú_adapt_stream_stream_eventã   s   rs   c                   @   s   e Zd ZdS )Ú_SimpleMethodHandlerN)r   r   r   r   r   r   r   rt   ø   s    rt   Z_MethodHandler)Zrequest_streamingZresponse_streamingÚrequest_deserializerÚresponse_serializerZunary_unaryZunary_streamZstream_unaryZstream_streamc              
   C   sx  | j t jju r^| jtjju rtdd||t| jƒd d d ƒS | jtjj	u r1tdd||d t| j
ƒd d ƒS | jtjju rFtdd||d d t| jƒd ƒS | jtjju r[tdd||d d d t| jƒƒS tƒ ‚| j t jju r¹| jtjju rzt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 r¤tdd||d d t| jƒd ƒS | jtjju r¹tdd||d d d t| jƒƒS tƒ ‚)NFT)r   ZServiceZINLINEr   ZCardinalityZUNARY_UNARYrt   r;   Zunary_unary_inlineZUNARY_STREAMZunary_stream_inlineZSTREAM_UNARYr@   Zstream_unary_inlineZSTREAM_STREAMZstream_stream_inlineZEVENTri   rh   rm   rl   rp   ro   rs   rr   Ú
ValueError)Úimplementationru   rv   r   r   r   Ú_simple_method_handler  sÀ   ø
ø
ÿöÿø;Ñø
ø
øø
ry   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   r|   c                   @   s   e Zd Zdd„ Zdd„ ZdS )Ú_GenericRpcHandlerc                 C   s(   t |ƒ| _t |ƒ| _t |ƒ| _|| _d S r   )r|   Ú_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   sb   | j  |j¡}|d urt|| j |j¡| j |j¡ƒS | jd u r"d S zW d S  tjy0   Y d S w r   )	r~   Úgetr{   ry   r   r€   r�   r
   ZNoSuchMethodError)r   Zhandler_call_detailsZmethod_implementationr   r   r   Úservice�  s    ÿý
ÿz_GenericRpcHandler.serviceN)r   r   r   r   r†   r   r   r   r   r}   |  s    r}   c                   @   rA   )Ú_Serverc                 C   r   r   )Ú_grpc_server)r   Zgrpc_serverr   r   r   r   £  r   z_Server.__init__c                 C   ó   | j  |¡S r   )rˆ   Úadd_insecure_port)r   Úaddressr   r   r   rŠ   ¦  ó   z_Server.add_insecure_portc                 C   s   | j  ||¡S r   )rˆ   Úadd_secure_port)r   r‹   Zserver_credentialsr   r   r   r�   ©  r&   z_Server.add_secure_portc                 C   r$   r   ©rˆ   rb   r   r   r   r   rb   ¬  r&   z_Server.startc                 C   r‰   r   ©rˆ   Ústop)r   Zgracer   r   r   r�   ¯  rŒ   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 rC   r�   )r   Úexc_typeÚexc_valÚexc_tbr   r   r   Ú__exit__¶  s   z_Server.__exit__N)
r   r   r   r   rŠ   r�   rb   r�   r‘   r•   r   r   r   r   r‡   ¢  s    r‡   c                 C   sF   t | |||ƒ}|d u rt |d u rtn|¡}n|}ttj||fd�ƒS )N)Úhandlers)r}   r   ÚpoolÚ_DEFAULT_POOL_SIZEr‡   ÚgrpcÚserver)Zservice_implementationsr‚   rƒ   r„   Zthread_poolZthread_pool_sizeZgeneric_rpc_handlerZeffective_thread_poolr   r   r   rš   »  s   üÿÿrš   ))Ú__doc__ÚcollectionsrD   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;   r@   ZConsumerrB   rc   ri   rm   rp   rs   Ú
namedtupleZRpcMethodHandlerrt   ry   r|   ZGenericRpcHandlerr}   ÚServerr‡   rš   r   r   r   r   Ú<module>   sH   )		5
þòh	&