Ejemplo n.º 1
0
def get_toplevel_context(raise_context_error=True):
    """Get the current top level context.

    This function should be called within a context established by
    :meth:`google.cloud.ndb.client.Client.context`.

    The toplevel context is the context created by the call to
    :meth:`google.cloud.ndb.client.Client.context`. At times, this context will
    be superceded by subcontexts, which are used, for example, during
    transactions. This function will always return the top level context
    regardless of whether one of these subcontexts is the current one.

    Args:
        raise_context_error (bool): If set to :data:`True`, will raise an
            exception if called outside of a context. Set this to :data:`False`
            in order to have it just return :data:`None` if called outside of a
            context. Default: :data:`True`

    Returns:
        Context: The current context.

    Raises:
        .ContextError: If called outside of a context
            established by :meth:`google.cloud.ndb.client.Client.context` and
            ``raise_context_error`` is :data:`True`.
    """
    context = _state.toplevel_context
    if context:
        return context

    if raise_context_error:
        raise exceptions.ContextError()
Ejemplo n.º 2
0
def get_context(raise_context_error=True):
    """Get the current context.

    This function should be called within a context established by
    :meth:`google.cloud.ndb.client.Client.context`.

    Args:
        raise_context_error (bool): If set to :data:`True`, will raise an
            exception if called outside of a context. Set this to :data:`False`
            in order to have it just return :data:`None` if called outside of a
            context. Default: :data:`True`

    Returns:
        Context: The current context.

    Raises:
        .ContextError: If called outside of a context
            established by :meth:`google.cloud.ndb.client.Client.context` and
            ``raise_context_error`` is :data:`True`.
    """
    context = _state.context
    if context:
        return context

    if raise_context_error:
        raise exceptions.ContextError()
Ejemplo n.º 3
0
def current():
    """Get the current context state.

    This function should be called within a context established by
    :meth:`google.cloud.ndb.client.Client.context`.

    Returns:
        State: The state for the current context.

    Raises:
        .ContextError: If called outside of a context
            established by :meth:`google.cloud.ndb.client.Client.context`.
    """
    state = states.current()
    if state:
        return state

    raise exceptions.ContextError()
Ejemplo n.º 4
0
def get_context():
    """Get the current context.

    This function should be called within a context established by
    :meth:`google.cloud.ndb.client.Client.context`.

    Returns:
        Context: The current context.

    Raises:
        .ContextError: If called outside of a context
            established by :meth:`google.cloud.ndb.client.Client.context`.
    """
    context = _state.context
    if context:
        return context

    raise exceptions.ContextError()