You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
bazarr/libs/click/globals.py

70 lines
1.9 KiB

import typing
import typing as t
6 years ago
from threading import local
if t.TYPE_CHECKING:
import typing_extensions as te
from .core import Context
6 years ago
_local = local()
@typing.overload
def get_current_context(silent: "te.Literal[False]" = False) -> "Context":
...
@typing.overload
def get_current_context(silent: bool = ...) -> t.Optional["Context"]:
...
def get_current_context(silent: bool = False) -> t.Optional["Context"]:
6 years ago
"""Returns the current click context. This can be used as a way to
access the current context object from anywhere. This is a more implicit
alternative to the :func:`pass_context` decorator. This function is
primarily useful for helpers such as :func:`echo` which might be
5 years ago
interested in changing its behavior based on the current context.
6 years ago
To push the current context, :meth:`Context.scope` can be used.
.. versionadded:: 5.0
:param silent: if set to `True` the return value is `None` if no context
6 years ago
is available. The default behavior is to raise a
:exc:`RuntimeError`.
"""
try:
return t.cast("Context", _local.stack[-1])
except (AttributeError, IndexError) as e:
6 years ago
if not silent:
raise RuntimeError("There is no active click context.") from e
6 years ago
return None
6 years ago
def push_context(ctx: "Context") -> None:
6 years ago
"""Pushes a new context to the current stack."""
_local.__dict__.setdefault("stack", []).append(ctx)
6 years ago
def pop_context() -> None:
6 years ago
"""Removes the top level from the stack."""
_local.stack.pop()
def resolve_color_default(color: t.Optional[bool] = None) -> t.Optional[bool]:
"""Internal helper to get the default value of the color flag. If a
6 years ago
value is passed it's returned unchanged, otherwise it's looked up from
the current context.
"""
if color is not None:
return color
6 years ago
ctx = get_current_context(silent=True)
6 years ago
if ctx is not None:
return ctx.color
return None