Skip to content
Myr

Added Context.meta

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 421e46a · Browse files · 3 files+51 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+51 added−0 removed

  • tests:+19 added−0 removed
  • other:+32 added−0 removed

3 files+51 added−0 removed

modified:click/core.py+30 added
189 unmodified lines
190 if obj is None and parent is not None:191 obj = parent.obj192 #: the user object stored.193 self.obj = obj194 self._meta = getattr(parent, 'meta', {})195196 #: A dictionary (-like object) with defaults for parameters.197 if default_map is None \198 and parent is not None \199 and parent.default_map is not None:
103 unmodified lines
303 self._depth -= 1304 if self._depth == 0:305 self.close()306307 @property308 def meta(self):309 """This is a dictionary which is shared with all the contexts310 that are nested. It exists so that click utiltiies can store some311 state here if they need to. It is however the responsibility of312 that code to manage this dictionary well.313314 The keys are supposed to be unique dotted strings. For instance315 module paths are a good choice for it. What is stored in there is316 irrelevant for the operation of click. However what is important is317 that code that places data here adheres to the general semantics of318 the system.319320 Example usage::321322 LANG_KEY = __name__ + '.lang'323324 def set_language(value):325 ctx = get_current_context()326 ctx.meta[LANG_KEY] = value327328 def get_language():329 return get_current_context().meta.get(LANG_KEY, 'en_US')330331 .. versionadded:: 5.0332 """333 return self._meta334335 def make_formatter(self):336 """Creates the formatter for the help and usage output."""337 return HelpFormatter(width=self.terminal_width,338 max_width=self.max_content_width)
1317 unmodified lines
modified:tests/test_context.py+19 added
121 unmodified lines
122123 assert click.get_current_context(silent=True) is None124 runner.invoke(cli, [], catch_exceptions=False)125 assert click.get_current_context(silent=True) is None126127128def test_context_meta(runner):129 LANG_KEY = __name__ + '.lang'130131 def set_language(value):132 click.get_current_context().meta[LANG_KEY] = value133134 def get_language():135 return click.get_current_context().meta.get(LANG_KEY, 'en_US')136137 @click.command()138 @click.pass_context139 def cli(ctx):140 assert get_language() == 'en_US'141 set_language('de_DE')142 assert get_language() == 'de_DE'143144 runner.invoke(cli, [], catch_exceptions=False)
modified:CHANGES+2 added
13 unmodified lines
14 their arguments onto the next line, indented by 4 spaces.15- Fix a bug where return code and error messages were incorrect when16 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.2021Version 4.122-----------23
221 unmodified lines