Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/click/globals.py: 40%
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1import typing as t
2from threading import local
4if t.TYPE_CHECKING:
5 import typing_extensions as te
6 from .core import Context
8_local = local()
11@t.overload
12def get_current_context(silent: "te.Literal[False]" = False) -> "Context":
13 ...
16@t.overload
17def get_current_context(silent: bool = ...) -> t.Optional["Context"]:
18 ...
21def get_current_context(silent: bool = False) -> t.Optional["Context"]:
22 """Returns the current click context. This can be used as a way to
23 access the current context object from anywhere. This is a more implicit
24 alternative to the :func:`pass_context` decorator. This function is
25 primarily useful for helpers such as :func:`echo` which might be
26 interested in changing its behavior based on the current context.
28 To push the current context, :meth:`Context.scope` can be used.
30 .. versionadded:: 5.0
32 :param silent: if set to `True` the return value is `None` if no context
33 is available. The default behavior is to raise a
34 :exc:`RuntimeError`.
35 """
36 try:
37 return t.cast("Context", _local.stack[-1])
38 except (AttributeError, IndexError) as e:
39 if not silent:
40 raise RuntimeError("There is no active click context.") from e
42 return None
45def push_context(ctx: "Context") -> None:
46 """Pushes a new context to the current stack."""
47 _local.__dict__.setdefault("stack", []).append(ctx)
50def pop_context() -> None:
51 """Removes the top level from the stack."""
52 _local.stack.pop()
55def resolve_color_default(color: t.Optional[bool] = None) -> t.Optional[bool]:
56 """Internal helper to get the default value of the color flag. If a
57 value is passed it's returned unchanged, otherwise it's looked up from
58 the current context.
59 """
60 if color is not None:
61 return color
63 ctx = get_current_context(silent=True)
65 if ctx is not None:
66 return ctx.color
68 return None