comparison venv/lib/python2.7/site-packages/click/globals.py @ 0:d67268158946 draft

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