U
    ڲg                  "   @   s  d Z ddlZddlZddl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 eeZejje	jjejje	jjejje	jjejje	jjejje	jjiZejj e	jj!ejj"e	jj#ejj$e	jj%ejj&e	jj'ejj(e	jj)ejj*e	jj+ejj,e	jj-ejj.e	jj/ejj0e	jj1ejj2e	jj3ejj4e	jj5ejj6e	jj7ejj8e	jj9ejj:e	jj;ejj<e	jj=ejj>e	jj?ejj@e	jjAiZBdd eBC D ZDd	ZEd
ZFeeGdddZHeeIdddZJeeeedf eIedddZKeee eGdddZLeGee edddZMeIeIeIdddZNedeOf ePeeg df  ddd ZQd'edeOf eg eOf eeP eeg df  eOd!d"d#ZReIeSeSd$d%d&ZTdS )(zShared implementation.    N)AnyAnyStrCallableOptionalUnion)cygrpc)DeserializingFunction)SerializingFunctionc                 C   s   i | ]\}}||qS  r
   ).0Zcygrpc_codeZ	grpc_coder
   r
   0/tmp/pip-unpacked-wheel-8poujhl6/grpc/_common.py
<dictcomp>6   s    r   g?zjFailed to bind to address %s; set GRPC_VERBOSITY=debug environment variable to see detailed error message.)sreturnc                 C   s   t | tr| S | dS d S )Nutf8)
isinstancebytesencode)r   r
   r
   r   r   C   s    
r   )br   c                 C   s   t | tr| ddS | S )Nzutf-8replace)r   r   decode)r   r
   r
   r   r   J   s    
r   )messagetransformerexception_messager   c                 C   s<   |d kr| S z
|| W S  t k
r6   t| Y d S X d S )N)	Exception_LOGGER	exception)r   r   r   r
   r
   r   
_transformP   s    

r   )r   
serializerr   c                 C   s   t | |dS )NzException serializing message!r   )r   r   r
   r
   r   	serialize_   s    r    )serialized_messagedeserializerr   c                 C   s   t | |dS )Nz Exception deserializing message!r   )r!   r"   r
   r
   r   deserializec   s
      r#   )groupmethodr   c                 C   s   d | |S )Nz/{}/{})format)r$   r%   r
   r
   r   fully_qualified_methodk   s    r'   .wait_fntimeoutspin_cbc                 C   s   | |d |d k	r|  d S )N)r*   r
   r(   r
   r
   r   
_wait_onceo   s    
r,   )r)   wait_complete_fnr*   r+   r   c                 C   s`   |dkr| s\t | t| qn>t | }| s\t|t  t}|dk rNdS t | || q*dS )ad  Blocks waiting for an event without blocking the thread indefinitely.

    See https://github.com/grpc/grpc/issues/19464 for full context. CPython's
    `threading.Event.wait` and `threading.Condition.wait` methods, if invoked
    without a timeout kwarg, may block the calling thread indefinitely. If the
    call is made from the main thread, this means that signal handlers may not
    run for an arbitrarily long period of time.

    This wrapper calls the supplied wait function with an arbitrary short
    timeout to ensure that no signal handler has to wait longer than
    MAXIMUM_WAIT_TIMEOUT before executing.

    Args:
      wait_fn: A callable acceptable a single float-valued kwarg named
        `timeout`. This function is expected to be one of `threading.Event.wait`
        or `threading.Condition.wait`.
      wait_complete_fn: A callable taking no arguments and returning a bool.
        When this function returns true, it indicates that waiting should cease.
      timeout: An optional float-valued number of seconds after which the wait
        should cease.
      spin_cb: An optional Callable taking no arguments and returning nothing.
        This callback will be called on each iteration of the spin. This may be
        used for, e.g. work related to forking.

    Returns:
      True if a timeout was supplied and it was reached. False otherwise.
    Nr   TF)r,   MAXIMUM_WAIT_TIMEOUTtimemin)r)   r-   r*   r+   end	remainingr
   r
   r   waity   s    !r3   )addressportr   c                 C   s   |dkrt t|  n|S dS )ak  Validates if the port binding succeed.

    If the port returned by Core is 0, the binding is failed. However, in that
    case, the Core API doesn't return a detailed failing reason. The best we
    can do is raising an exception to prevent further confusion.

    Args:
        address: The address string to be bound.
        port: An int returned by core
    r   N)RuntimeError"_ERROR_MESSAGE_PORT_BINDING_FAILED)r4   r5   r
   r
   r   validate_port_binding_result   s    r8   )NN)U__doc__loggingr/   typingr   r   r   r   r   ZgrpcZgrpc._cythonr   Zgrpc._typingr   r	   	getLogger__name__r   ZConnectivityStateZidleZChannelConnectivityZIDLEZ
connectingZ
CONNECTINGreadyZREADYZtransient_failureZTRANSIENT_FAILUREshutdownZSHUTDOWNZ1CYGRPC_CONNECTIVITY_STATE_TO_CHANNEL_CONNECTIVITYZ
StatusCodeokOKZ	cancelledZ	CANCELLEDunknownUNKNOWNZinvalid_argumentZINVALID_ARGUMENTZdeadline_exceededZDEADLINE_EXCEEDED	not_found	NOT_FOUNDZalready_existsZALREADY_EXISTSZpermission_deniedZPERMISSION_DENIEDZunauthenticatedZUNAUTHENTICATEDZresource_exhaustedZRESOURCE_EXHAUSTEDZfailed_preconditionZFAILED_PRECONDITIONZabortedZABORTEDZout_of_rangeZOUT_OF_RANGEZunimplementedZUNIMPLEMENTEDZinternalZINTERNALunavailableUNAVAILABLEZ	data_lossZ	DATA_LOSSZ!CYGRPC_STATUS_CODE_TO_STATUS_CODEitemsZ!STATUS_CODE_TO_CYGRPC_STATUS_CODEr.   r7   r   r   strr   r   r    r#   r'   boolfloatr,   r3   intr8   r
   r
   r
   r   <module>   s   
     	                  
  

.