Added Context.scope
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Armin Ronacher · committed by Armin Ronacher · parent 86facdb · Browse files · 4 files·+65 added−1 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+65 added−1 removed
- tests:+24 added−0 removed
- other:+41 added−1 removed
Provenance
- Commit
8e31125- Tree
b6879fc- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
86facdb
4 files changed+65 added−1 removed
modified:click/core.py+38 added-1 removed
302 unmodified lines303 self._depth -= 1304 if self._depth == 0:305 self.close()306 307+ @contextmanager308+ def scope(self, cleanup=True):309+ """This helper method can be used with the context object to promote310+ it to the current thread local (see :func:`get_current_context`).311+ The default behavior of this is to invoke the cleanup functions which312+ can be disabled by setting `cleanup` to `False`. The cleanup313+ functions are typically used for things such as closing file handles.314+315+ If the cleanup is intended the context object can also be directly316+ used as a context manager.317+318+ Example usage::319+320+ with ctx.scope():321+ assert get_current_context() is ctx322+323+ This is equivalent::324+325+ with ctx:326+ assert get_current_context() is ctx327+328+ .. versionadded:: 5.0329+330+ :param cleanup: controls if the cleanup functions should be run or331+ not. The default is to run these functions. In332+ some situations the context only wants to be333+ temporarily pushed in which case this can be disabled.334+ Nested pushes automatically defer the cleanup.335+ """336+ if not cleanup:337+ self._depth += 1338+ with self as rv:339+ yield rv340+ if not cleanup:341+ self._depth -= 1342+343 @property344 def meta(self):345 """This is a dictionary which is shared with all the contexts346 that are nested. It exists so that click utiltiies can store some240 unmodified lines587 for key, value in iteritems(self.context_settings):588 if key not in extra:589 extra[key] = value590 ctx = Context(self, info_name=info_name, parent=parent, **extra)591+ with ctx.scope(cleanup=False):555- self.parse_args(ctx, args)592+ self.parse_args(ctx, args)593 return ctx594 595 def parse_args(self, ctx, args):596 """Given a context and a list of arguments this creates the parser1096 unmodified linesmodified:click/globals.py+2 added
9 unmodified lines10 alternative to the :func:`pass_context` decorator. This function is11 primarily useful for helpers such as :func:`echo` which might be12 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.017 18 :param silent: is set to `True` the return value is `None` if no context19 is available. The default behavior is to raise a29 unmodified linesmodified:tests/test_context.py+24 added
140 unmodified lines141 set_language('de_DE')142 assert get_language() == 'de_DE'143 144 runner.invoke(cli, [], catch_exceptions=False)145+146+147+def test_context_pushing():148+ rv = []149+150+ @click.command()151+ def cli():152+ pass153+154+ ctx = click.Context(cli)155+156+ @ctx.call_on_close157+ def test_callback():158+ rv.append(42)159+160+ with ctx.scope(cleanup=False):161+ pass162+163+ assert rv == []164+165+ with ctx.scope():166+ pass167+168+ assert rv == [42]modified:CHANGES+1 added
15 unmodified lines16 using ``CliRunner``.17 - added `get_current_context`.18 - added a `meta` dictionary to the context which is shared across the19 linked list of contexts to allow click utilities to place state there.20+- introduced `Context.scope`.21 22 Version 4.123 -----------24 221 unmodified lines