U
    Uºc0  ã                	   @   s†   d Z ddlmZ ddlZddlZddlZddlZe e¡Z	G dd„ deƒZ
G dd„ de ddd	d
g¡e
ƒZdd„ Zdd„ Zdd„ ZdS )z%Utilities for working with callables.é    )ÚABCNc                   @   s(   e Zd ZdZejG dd„ dejƒƒZdS )ÚOutcomea…  A sum type describing the outcome of some call.

  Attributes:
    kind: One of Kind.RETURNED or Kind.RAISED respectively indicating that the
      call returned a value or raised an exception.
    return_value: The value returned by the call. Must be present if kind is
      Kind.RETURNED.
    exception: The exception raised by the call. Must be present if kind is
      Kind.RAISED.
  c                   @   s   e Zd ZdZeƒ Zeƒ ZdS )zOutcome.Kindz8Identifies the general kind of the outcome of some call.N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚobjectÚRETURNEDÚRAISED© r   r   úK/tmp/pip-unpacked-wheel-may1sbrn/grpc/framework/foundation/callable_util.pyÚKind%   s   r   N)r   r   r   r   ÚenumÚuniqueÚEnumr   r   r   r   r   r      s   r   c                   @   s   e Zd ZdZdS )Ú_EasyOutcomez$A trivial implementation of Outcome.N)r   r   r   r   r   r   r   r   r   -   s   r   ÚkindZreturn_valueÚ	exceptionc              
   O   s^   zt tjj| ||Žd ƒW S  tk
rX } z"t |¡ t tjjd |ƒ W Y ¢S d }~X Y nX d S ©N)r   r   r   r	   Ú	ExceptionÚ_LOGGERr   r
   )ÚbehaviorÚmessageÚargsÚkwargsÚer   r   r   Ú_call_logging_exceptions3   s    ÿ
r   c                    s   t  ˆ ¡‡ ‡fdd„ƒ}|S )a»  Wraps a callable in a try-except that logs any exceptions it raises.

  Args:
    behavior: Any callable.
    message: A string to log if the behavior raises an exception.

  Returns:
    A callable that when executed invokes the given behavior. The returned
      callable takes the same arguments as the given behavior but returns a
      future.Outcome describing whether the given behavior returned a value or
      raised an exception.
  c                     s   t ˆ ˆf| ž|ŽS r   ©r   )r   r   ©r   r   r   r   Úwrapped_behaviorJ   s    z0with_exceptions_logged.<locals>.wrapped_behavior)Ú	functoolsÚwraps)r   r   r   r   r   r   Úwith_exceptions_logged<   s    r"   c                 O   s   t | |f|ž|ŽS )aœ  Calls a behavior in a try-except that logs any exceptions it raises.

  Args:
    behavior: Any callable.
    message: A string to log if the behavior raises an exception.
    *args: Positional arguments to pass to the given behavior.
    **kwargs: Keyword arguments to pass to the given behavior.

  Returns:
    An Outcome describing whether the given behavior returned a value or raised
      an exception.
  r   )r   r   r   r   r   r   r   Úcall_logging_exceptionsQ   s    r#   )r   Úabcr   Úcollectionsr   r    ÚloggingÚ	getLoggerr   r   r   Ú
namedtupler   r   r"   r#   r   r   r   r   Ú<module>   s    

ÿþ	