U
    ڲg"`                     @   s6  d Z ddlZddl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mZmZmZ ddlZddlmZ edZedZeeeef  Zeeeeej eej f ZeeZdZeejkrej e!eje dZ"e#d	e" nej d
dZ"dZ$e$ejkr e%eje$ Z&e#de& ndZ&dZ'e'ejkrPe!eje' Z(e#de( ndZ(eeeeef  eej eej ej)dddZ*G dd dZ+edddddddde(ddfeeeee	ege,f  ee	e,gef  eeeef  eej e-eej. eej ee- ee! eeeeeee,f f   ee- edddZ/edddddddde(ddfeeeee	ege,f  ee	e,gef  eeeef  eej e-eej. eej ee- ee! eeeeeee,f f   ee- ee dddZ0edddddddde(ddfee eeee	ege,f  ee	e,gef  eeeef  eej e-eej. eej ee- ee! eeeeeee,f f   ee- eddd Z1edddddddde(ddfee eeee	ege,f  ee	e,gef  eeeef  eej e-eej. eej ee- ee! eeeeeee,f f   ee- ee dd!d"Z2dS )#z<Functions that obviate explicit stubs and explicit channels.    N)
AnyAnyStrCallableDictIteratorOptionalSequenceTupleTypeVarUnion)experimental_apiRequestTypeResponseTypeZ,GRPC_PYTHON_MANAGED_CHANNEL_EVICTION_SECONDS)secondsz-Setting managed channel eviction period to %s
   )minutesZ#GRPC_PYTHON_MANAGED_CHANNEL_MAXIMUMz&Setting maximum managed channels to %d   Z#GRPC_PYTHON_DEFAULT_TIMEOUT_SECONDSz%Setting default timeout seconds to %fg      N@)targetoptionschannel_credentialscompressionreturnc                 C   s6   t d| dd| d| d  tj| |||dS )Nz*Creating secure channel with credentials 'z', z	options 'z' and compression '')credentialsr   r   )_LOGGERdebuggrpcZsecure_channel)r   r   r   r    r   6/tmp/pip-unpacked-wheel-8poujhl6/grpc/_simple_stubs.py_create_channelL   s    
r   c                   @   s   e Zd ZU dZe Zejed< ejedZ	ejed< e
 Zej
ed< eeeejejf f ed< ejed< dd	 Zed
d ZedddZedd Zeeeeef  eej eeej eeeejee f dddZedddZ dS )ChannelCacheN_lock)lock
_condition_eviction_ready_mapping_eviction_threadc                 C   s*   t  | _tjtjdd| _| j  d S )NT)r   daemon)	collectionsOrderedDictr%   	threadingThreadr    _perform_evictionsr&   startselfr   r   r   __init__h   s    
 zChannelCache.__init__c                	   C   s4   t j t jd krt  t _W 5 Q R X t j  t jS N)r    r!   
_singletonr$   waitr   r   r   r   geto   s
    

zChannelCache.get)keyc                 C   s,   | j |\}}td|| |  ~d S )Nz*Evicting channel %s with configuration %s.)r%   popr   r   close)r/   r5   channel_r   r   r   _evict_lockedw   s      zChannelCache._evict_lockedc               	   C   s   t j t j  t jjs&t j  ntt jjt	krXt
tt jj } t j|  nbt
tt jj \} \}}tj }||krt j|  W 5 Q R  q n||  }t jj|d W 5 Q R X q d S )N)timeout)r    r!   r$   setr2   r%   r#   r3   len_MAXIMUM_CHANNELSnextiterkeysr:   itemsdatetimenowtotal_seconds)r5   r9   Zeviction_timerD   Ztime_to_evictionr   r   r   r,      s     

zChannelCache._perform_evictions)r   r   r   insecurer   method_registered_methodr   c              
   C   s6  |r|rt d|r tj }n|dkr:td t }||||f}| j | j	|d}	d}
|	dk	r|	d }|r~|
|}
| j| |tj t f| j|< ||
fW  5 Q R  S t||||}|r|
|}
|tj t f| j|< t| jdks
t| jtkr| j  ||
fW  5 Q R  S W 5 Q R X dS )a  Get a channel from cache or creates a new channel.

        This method also takes care of register method for channel,
          which means we'll register a new call handle if we're calling a
          non-registered method for an existing channel.

        Returns:
            A tuple with two items. The first item is the channel, second item is
              the call handle if the method is registered, None if it's not registered.
        zkThe insecure option is mutually exclusive with the channel_credentials option. Please use one or the other.Nz&Defaulting to SSL channel credentials.r      )
ValueErrorr   ZexperimentalZinsecure_channel_credentialsr   r   Zssl_channel_credentialsr!   r%   r4   Z_get_registered_call_handler6   rC   rD   _EVICTION_PERIODr   r=   r>   r#   notify)r/   r   r   r   rF   r   rG   rH   r5   Zchannel_dataZcall_handler8   r   r   r   get_channel   sP    


   


zChannelCache.get_channel)r   c              
   C   s(   | j  t| jW  5 Q R  S Q R X d S r1   )r!   r=   r%   r.   r   r   r   _test_only_channel_count   s    z%ChannelCache._test_only_channel_count)!__name__
__module____qualname__r2   r*   RLockr!   __annotations__	Conditionr#   Eventr$   r   CacheKeyr	   r   ChannelrC   r+   r0   staticmethodr4   r:   r,   strr   r   ChannelCredentialsboolCompressionintrM   rN   r   r   r   r   r    ^   s,   



Br    r   F)requestr   rG   request_serializerresponse_deserializerr   r   rF   call_credentialsr   wait_for_readyr;   metadatarH   r   c              	   C   sP   t  |||||	||\}}|||||}|
dk	r:|
nd}
|| ||
||dS )a
  Invokes a unary-unary RPC without an explicitly specified channel.

    THIS IS AN EXPERIMENTAL API.

    This is backed by a per-process cache of channels. Channels are evicted
    from the cache after a fixed period by a background. Channels will also be
    evicted if more than a configured maximum accumulate.

    The default eviction period is 10 minutes. One may set the environment
    variable "GRPC_PYTHON_MANAGED_CHANNEL_EVICTION_SECONDS" to configure this.

    The default maximum number of channels is 256. One may set the
    environment variable "GRPC_PYTHON_MANAGED_CHANNEL_MAXIMUM" to configure
    this.

    Args:
      request: An iterator that yields request values for the RPC.
      target: The server address.
      method: The name of the RPC method.
      request_serializer: Optional :term:`serializer` for serializing the request
        message. Request goes unserialized in case None is passed.
      response_deserializer: Optional :term:`deserializer` for deserializing the response
        message. Response goes undeserialized in case None is passed.
      options: An optional list of key-value pairs (:term:`channel_arguments` in gRPC Core
        runtime) to configure the channel.
      channel_credentials: A credential applied to the whole channel, e.g. the
        return value of grpc.ssl_channel_credentials() or
        grpc.insecure_channel_credentials().
      insecure: If True, specifies channel_credentials as
        :term:`grpc.insecure_channel_credentials()`. This option is mutually
        exclusive with the `channel_credentials` option.
      call_credentials: A call credential applied to each call individually,
        e.g. the output of grpc.metadata_call_credentials() or
        grpc.access_token_call_credentials().
      compression: An optional value indicating the compression method to be
        used over the lifetime of the channel, e.g. grpc.Compression.Gzip.
      wait_for_ready: An optional flag indicating whether the RPC should fail
        immediately if the connection is not ready at the time the RPC is
        invoked, or if it should wait until the connection to the server
        becomes ready. When using this option, the user will likely also want
        to set a timeout. Defaults to True.
      timeout: An optional duration of time in seconds to allow for the RPC,
        after which an exception will be raised. If timeout is unspecified,
        defaults to a timeout controlled by the
        GRPC_PYTHON_DEFAULT_TIMEOUT_SECONDS environment variable. If that is
        unset, defaults to 60 seconds. Supply a value of None to indicate that
        no timeout should be enforced.
      metadata: Optional metadata to send to the server.

    Returns:
      The response to the RPC.
    NTrc   rb   r   r;   )r    r4   rM   unary_unaryr^   r   rG   r_   r`   r   r   rF   ra   r   rb   r;   rc   rH   r8   method_handlemulticallabler   r   r   re      s.    F	   re   c              	   C   sP   t  |||||	||\}}|||||}|
dk	r:|
nd}
|| ||
||dS )a
  Invokes a unary-stream RPC without an explicitly specified channel.

    THIS IS AN EXPERIMENTAL API.

    This is backed by a per-process cache of channels. Channels are evicted
    from the cache after a fixed period by a background. Channels will also be
    evicted if more than a configured maximum accumulate.

    The default eviction period is 10 minutes. One may set the environment
    variable "GRPC_PYTHON_MANAGED_CHANNEL_EVICTION_SECONDS" to configure this.

    The default maximum number of channels is 256. One may set the
    environment variable "GRPC_PYTHON_MANAGED_CHANNEL_MAXIMUM" to configure
    this.

    Args:
      request: An iterator that yields request values for the RPC.
      target: The server address.
      method: The name of the RPC method.
      request_serializer: Optional :term:`serializer` for serializing the request
        message. Request goes unserialized in case None is passed.
      response_deserializer: Optional :term:`deserializer` for deserializing the response
        message. Response goes undeserialized in case None is passed.
      options: An optional list of key-value pairs (:term:`channel_arguments` in gRPC Core
        runtime) to configure the channel.
      channel_credentials: A credential applied to the whole channel, e.g. the
        return value of grpc.ssl_channel_credentials().
      insecure: If True, specifies channel_credentials as
        :term:`grpc.insecure_channel_credentials()`. This option is mutually
        exclusive with the `channel_credentials` option.
      call_credentials: A call credential applied to each call individually,
        e.g. the output of grpc.metadata_call_credentials() or
        grpc.access_token_call_credentials().
      compression: An optional value indicating the compression method to be
        used over the lifetime of the channel, e.g. grpc.Compression.Gzip.
      wait_for_ready: An optional flag indicating whether the RPC should fail
        immediately if the connection is not ready at the time the RPC is
        invoked, or if it should wait until the connection to the server
        becomes ready. When using this option, the user will likely also want
        to set a timeout. Defaults to True.
      timeout: An optional duration of time in seconds to allow for the RPC,
        after which an exception will be raised. If timeout is unspecified,
        defaults to a timeout controlled by the
        GRPC_PYTHON_DEFAULT_TIMEOUT_SECONDS environment variable. If that is
        unset, defaults to 60 seconds. Supply a value of None to indicate that
        no timeout should be enforced.
      metadata: Optional metadata to send to the server.

    Returns:
      An iterator of responses.
    NTrd   )r    r4   rM   unary_streamrf   r   r   r   ri   >  s.    E	   ri   )request_iteratorr   rG   r_   r`   r   r   rF   ra   r   rb   r;   rc   rH   r   c              	   C   sP   t  |||||	||\}}|||||}|
dk	r:|
nd}
|| ||
||dS )a
  Invokes a stream-unary RPC without an explicitly specified channel.

    THIS IS AN EXPERIMENTAL API.

    This is backed by a per-process cache of channels. Channels are evicted
    from the cache after a fixed period by a background. Channels will also be
    evicted if more than a configured maximum accumulate.

    The default eviction period is 10 minutes. One may set the environment
    variable "GRPC_PYTHON_MANAGED_CHANNEL_EVICTION_SECONDS" to configure this.

    The default maximum number of channels is 256. One may set the
    environment variable "GRPC_PYTHON_MANAGED_CHANNEL_MAXIMUM" to configure
    this.

    Args:
      request_iterator: An iterator that yields request values for the RPC.
      target: The server address.
      method: The name of the RPC method.
      request_serializer: Optional :term:`serializer` for serializing the request
        message. Request goes unserialized in case None is passed.
      response_deserializer: Optional :term:`deserializer` for deserializing the response
        message. Response goes undeserialized in case None is passed.
      options: An optional list of key-value pairs (:term:`channel_arguments` in gRPC Core
        runtime) to configure the channel.
      channel_credentials: A credential applied to the whole channel, e.g. the
        return value of grpc.ssl_channel_credentials().
      call_credentials: A call credential applied to each call individually,
        e.g. the output of grpc.metadata_call_credentials() or
        grpc.access_token_call_credentials().
      insecure: If True, specifies channel_credentials as
        :term:`grpc.insecure_channel_credentials()`. This option is mutually
        exclusive with the `channel_credentials` option.
      compression: An optional value indicating the compression method to be
        used over the lifetime of the channel, e.g. grpc.Compression.Gzip.
      wait_for_ready: An optional flag indicating whether the RPC should fail
        immediately if the connection is not ready at the time the RPC is
        invoked, or if it should wait until the connection to the server
        becomes ready. When using this option, the user will likely also want
        to set a timeout. Defaults to True.
      timeout: An optional duration of time in seconds to allow for the RPC,
        after which an exception will be raised. If timeout is unspecified,
        defaults to a timeout controlled by the
        GRPC_PYTHON_DEFAULT_TIMEOUT_SECONDS environment variable. If that is
        unset, defaults to 60 seconds. Supply a value of None to indicate that
        no timeout should be enforced.
      metadata: Optional metadata to send to the server.

    Returns:
      The response to the RPC.
    NTrd   )r    r4   rM   stream_unaryrj   r   rG   r_   r`   r   r   rF   ra   r   rb   r;   rc   rH   r8   rg   rh   r   r   r   rk     s.    E	   rk   c              	   C   sP   t  |||||	||\}}|||||}|
dk	r:|
nd}
|| ||
||dS )a
  Invokes a stream-stream RPC without an explicitly specified channel.

    THIS IS AN EXPERIMENTAL API.

    This is backed by a per-process cache of channels. Channels are evicted
    from the cache after a fixed period by a background. Channels will also be
    evicted if more than a configured maximum accumulate.

    The default eviction period is 10 minutes. One may set the environment
    variable "GRPC_PYTHON_MANAGED_CHANNEL_EVICTION_SECONDS" to configure this.

    The default maximum number of channels is 256. One may set the
    environment variable "GRPC_PYTHON_MANAGED_CHANNEL_MAXIMUM" to configure
    this.

    Args:
      request_iterator: An iterator that yields request values for the RPC.
      target: The server address.
      method: The name of the RPC method.
      request_serializer: Optional :term:`serializer` for serializing the request
        message. Request goes unserialized in case None is passed.
      response_deserializer: Optional :term:`deserializer` for deserializing the response
        message. Response goes undeserialized in case None is passed.
      options: An optional list of key-value pairs (:term:`channel_arguments` in gRPC Core
        runtime) to configure the channel.
      channel_credentials: A credential applied to the whole channel, e.g. the
        return value of grpc.ssl_channel_credentials().
      call_credentials: A call credential applied to each call individually,
        e.g. the output of grpc.metadata_call_credentials() or
        grpc.access_token_call_credentials().
      insecure: If True, specifies channel_credentials as
        :term:`grpc.insecure_channel_credentials()`. This option is mutually
        exclusive with the `channel_credentials` option.
      compression: An optional value indicating the compression method to be
        used over the lifetime of the channel, e.g. grpc.Compression.Gzip.
      wait_for_ready: An optional flag indicating whether the RPC should fail
        immediately if the connection is not ready at the time the RPC is
        invoked, or if it should wait until the connection to the server
        becomes ready. When using this option, the user will likely also want
        to set a timeout. Defaults to True.
      timeout: An optional duration of time in seconds to allow for the RPC,
        after which an exception will be raised. If timeout is unspecified,
        defaults to a timeout controlled by the
        GRPC_PYTHON_DEFAULT_TIMEOUT_SECONDS environment variable. If that is
        unset, defaults to 60 seconds. Supply a value of None to indicate that
        no timeout should be enforced.
      metadata: Optional metadata to send to the server.

    Returns:
      An iterator of responses.
    NTrd   )r    r4   rM   stream_streamrl   r   r   r   rm     s.    E	   rm   )3__doc__r(   rC   loggingosr*   typingr   r   r   r   r   r   r   r	   r
   r   r   Zgrpc.experimentalr   r   r   rY   ZOptionsTyperZ   r\   rV   	getLoggerrO   r   Z_EVICTION_PERIOD_KEYenviron	timedeltafloatrK   r   Z_MAXIMUM_CHANNELS_KEYr]   r>   Z_DEFAULT_TIMEOUT_KEYZ_DEFAULT_TIMEOUTrW   r   r    bytesr[   ZCallCredentialsre   ri   rk   rm   r   r   r   r   <module>   sF  0

  ZYY