comparison env/lib/python3.7/site-packages/click/globals.py @ 5:9b1c78e6ba9c draft default tip

"planemo upload commit 6c0a8142489327ece472c84e558c47da711a9142"
author shellac
date Mon, 01 Jun 2020 08:59:25 -0400
parents 79f47841a781
children
comparison
equal deleted inserted replaced
4:79f47841a781 5:9b1c78e6ba9c
1 from threading import local
2
3 _local = local()
4
5
6 def get_current_context(silent=False):
7 """Returns the current click context. This can be used as a way to
8 access the current context object from anywhere. This is a more implicit
9 alternative to the :func:`pass_context` decorator. This function is
10 primarily useful for helpers such as :func:`echo` which might be
11 interested in changing its behavior based on the current context.
12
13 To push the current context, :meth:`Context.scope` can be used.
14
15 .. versionadded:: 5.0
16
17 :param silent: if set to `True` the return value is `None` if no context
18 is available. The default behavior is to raise a
19 :exc:`RuntimeError`.
20 """
21 try:
22 return _local.stack[-1]
23 except (AttributeError, IndexError):
24 if not silent:
25 raise RuntimeError("There is no active click context.")
26
27
28 def push_context(ctx):
29 """Pushes a new context to the current stack."""
30 _local.__dict__.setdefault("stack", []).append(ctx)
31
32
33 def pop_context():
34 """Removes the top level from the stack."""
35 _local.stack.pop()
36
37
38 def resolve_color_default(color=None):
39 """"Internal helper to get the default value of the color flag. If a
40 value is passed it's returned unchanged, otherwise it's looked up from
41 the current context.
42 """
43 if color is not None:
44 return color
45 ctx = get_current_context(silent=True)
46 if ctx is not None:
47 return ctx.color