~ubuntu-branches/ubuntu/wily/grpc/wily

« back to all changes in this revision

Viewing changes to src/python/src/grpc/framework/foundation/callable_util.py

  • Committer: Package Import Robot
  • Author(s): Andrew Pollock
  • Date: 2015-05-07 13:28:11 UTC
  • Revision ID: package-import@ubuntu.com-20150507132811-ybm4hfq73tnvvd2e
Tags: upstream-0.10.0
ImportĀ upstreamĀ versionĀ 0.10.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright 2015, Google Inc.
 
2
# All rights reserved.
 
3
#
 
4
# Redistribution and use in source and binary forms, with or without
 
5
# modification, are permitted provided that the following conditions are
 
6
# met:
 
7
#
 
8
#     * Redistributions of source code must retain the above copyright
 
9
# notice, this list of conditions and the following disclaimer.
 
10
#     * Redistributions in binary form must reproduce the above
 
11
# copyright notice, this list of conditions and the following disclaimer
 
12
# in the documentation and/or other materials provided with the
 
13
# distribution.
 
14
#     * Neither the name of Google Inc. nor the names of its
 
15
# contributors may be used to endorse or promote products derived from
 
16
# this software without specific prior written permission.
 
17
#
 
18
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 
19
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 
20
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 
21
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 
22
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 
23
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 
24
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 
25
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 
26
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 
27
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 
28
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
29
 
 
30
"""Utilities for working with callables."""
 
31
 
 
32
import abc
 
33
import collections
 
34
import enum
 
35
import functools
 
36
import logging
 
37
 
 
38
 
 
39
class Outcome(object):
 
40
  """A sum type describing the outcome of some call.
 
41
 
 
42
  Attributes:
 
43
    kind: One of Kind.RETURNED or Kind.RAISED respectively indicating that the
 
44
      call returned a value or raised an exception.
 
45
    return_value: The value returned by the call. Must be present if kind is
 
46
      Kind.RETURNED.
 
47
    exception: The exception raised by the call. Must be present if kind is
 
48
      Kind.RAISED.
 
49
  """
 
50
  __metaclass__ = abc.ABCMeta
 
51
 
 
52
  @enum.unique
 
53
  class Kind(enum.Enum):
 
54
    """Identifies the general kind of the outcome of some call."""
 
55
 
 
56
    RETURNED = object()
 
57
    RAISED = object()
 
58
 
 
59
 
 
60
class _EasyOutcome(
 
61
    collections.namedtuple(
 
62
        '_EasyOutcome', ['kind', 'return_value', 'exception']),
 
63
    Outcome):
 
64
  """A trivial implementation of Outcome."""
 
65
 
 
66
 
 
67
def _call_logging_exceptions(behavior, message, *args, **kwargs):
 
68
  try:
 
69
    return _EasyOutcome(Outcome.Kind.RETURNED, behavior(*args, **kwargs), None)
 
70
  except Exception as e:  # pylint: disable=broad-except
 
71
    logging.exception(message)
 
72
    return _EasyOutcome(Outcome.Kind.RAISED, None, e)
 
73
 
 
74
 
 
75
def with_exceptions_logged(behavior, message):
 
76
  """Wraps a callable in a try-except that logs any exceptions it raises.
 
77
 
 
78
  Args:
 
79
    behavior: Any callable.
 
80
    message: A string to log if the behavior raises an exception.
 
81
 
 
82
  Returns:
 
83
    A callable that when executed invokes the given behavior. The returned
 
84
      callable takes the same arguments as the given behavior but returns a
 
85
      future.Outcome describing whether the given behavior returned a value or
 
86
      raised an exception.
 
87
  """
 
88
  @functools.wraps(behavior)
 
89
  def wrapped_behavior(*args, **kwargs):
 
90
    return _call_logging_exceptions(behavior, message, *args, **kwargs)
 
91
  return wrapped_behavior
 
92
 
 
93
 
 
94
def call_logging_exceptions(behavior, message, *args, **kwargs):
 
95
  """Calls a behavior in a try-except that logs any exceptions it raises.
 
96
 
 
97
  Args:
 
98
    behavior: Any callable.
 
99
    message: A string to log if the behavior raises an exception.
 
100
    *args: Positional arguments to pass to the given behavior.
 
101
    **kwargs: Keyword arguments to pass to the given behavior.
 
102
 
 
103
  Returns:
 
104
    An Outcome describing whether the given behavior returned a value or raised
 
105
      an exception.
 
106
  """
 
107
  return _call_logging_exceptions(behavior, message, *args, **kwargs)