Python contextlib2.contextmanager() Examples

The following are 3 code examples of contextlib2.contextmanager(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module contextlib2 , or try the search function .
Example #1
Source File: console_output.py    From openhtf with Apache License 2.0 5 votes vote down vote up
def fail(self):
    """Mark the action as having failed.

    Raises:
      ActionFailedError: This exception is always raised, by this function,
          but should be caught by the contextmanager.
    """
    self.success = False
    raise ActionFailedError() 
Example #2
Source File: local_span.py    From opentracing-python-instrumentation with MIT License 5 votes vote down vote up
def func_span(func, tags=None, require_active_trace=False):
    """
    Creates a new local span for execution of the given `func`.
    The returned span is best used as a context manager, e.g.

    .. code-block:: python

        with func_span('my_function'):
            return my_function(...)

    At this time the func should be a string name. In the future this code
    can be enhanced to accept a real function and derive its qualified name.

    :param func: name of the function or method
    :param tags: optional tags to add to the child span
    :param require_active_trace: controls what to do when there is no active
        trace. If require_active_trace=True, then no span is created.
        If require_active_trace=False, a new trace is started.
    :return: new child span, or a dummy context manager if there is no
        active/current parent span
    """
    current_span = get_current_span()

    if current_span is None and require_active_trace:
        @contextlib2.contextmanager
        def empty_ctx_mgr():
            yield None

        return empty_ctx_mgr()

    # TODO convert func to a proper name: module:class.func
    operation_name = str(func)
    return utils.start_child_span(
        operation_name=operation_name, parent=current_span, tags=tags) 
Example #3
Source File: _dbapi2.py    From opentracing-python-instrumentation with MIT License 4 votes vote down vote up
def db_span(sql_statement,
            module_name,
            sql_parameters=None,
            connect_params=None,
            cursor_params=None):
    span = current_span_func()

    @contextlib2.contextmanager
    def empty_ctx_mgr():
        yield None

    if span is None:
        return empty_ctx_mgr()

    if bytes is not str and isinstance(sql_statement, bytes):
        sql_statement = sql_statement.decode('utf-8', errors='ignore')

    statement = sql_statement.strip()
    add_sql_tag = True
    if sql_statement in _TRANS_TAGS:
        operation = sql_statement
        add_sql_tag = False
    else:
        space_idx = statement.find(' ')
        if space_idx == -1:
            operation = ''  # unrecognized format of the query
        else:
            operation = statement[0:space_idx]

    tags = {ext_tags.SPAN_KIND: ext_tags.SPAN_KIND_RPC_CLIENT}
    if add_sql_tag:
        tags['sql'] = statement
    if sql_parameters:
        tags['sql.params'] = sql_parameters
    if connect_params:
        tags['sql.conn'] = connect_params
    if cursor_params:
        tags['sql.cursor'] = cursor_params

    return utils.start_child_span(
        operation_name='%s:%s' % (module_name, operation),
        parent=span, tags=tags
    )