codekingpro/portable-devtools
114k
1import typing as t2from threading import local3 4if t.TYPE_CHECKING:5 import typing_extensions as te6 from .core import Context7 8_local = local()9 10 11@t.overload12def get_current_context(silent: "te.Literal[False]" = False) -> "Context":13 ...14 15 16@t.overload17def get_current_context(silent: bool = ...) -> t.Optional["Context"]:18 ...19 20 21def get_current_context(silent: bool = False) -> t.Optional["Context"]:22 """Returns the current click context. This can be used as a way to23 access the current context object from anywhere. This is a more implicit24 alternative to the :func:`pass_context` decorator. This function is25 primarily useful for helpers such as :func:`echo` which might be26 interested in changing its behavior based on the current context.27 28 To push the current context, :meth:`Context.scope` can be used.29 30 .. versionadded:: 5.031 32 :param silent: if set to `True` the return value is `None` if no context33 is available. The default behavior is to raise a34 :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 e41 42 return None43 44 45def push_context(ctx: "Context") -> None:46 """Pushes a new context to the current stack."""47 _local.__dict__.setdefault("stack", []).append(ctx)48 49 50def pop_context() -> None:51 """Removes the top level from the stack."""52 _local.stack.pop()53 54 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 a57 value is passed it's returned unchanged, otherwise it's looked up from58 the current context.59 """60 if color is not None:61 return color62 63 ctx = get_current_context(silent=True)64 65 if ctx is not None:66 return ctx.color67 68 return None69 