OpenQuizz
Une application de gestion des contenus pédagogiques
Context Class Reference
Inheritance diagram for Context:
Collaboration diagram for Context:

Public Member Functions

def __init__ (self, command, parent=None, info_name=None, obj=None, auto_envvar_prefix=None, default_map=None, terminal_width=None, max_content_width=None, resilient_parsing=False, allow_extra_args=None, allow_interspersed_args=None, ignore_unknown_options=None, help_option_names=None, token_normalize_func=None, color=None, show_default=None)
 
def __enter__ (self)
 
def __exit__ (self, exc_type, exc_value, tb)
 
def scope (self, cleanup=True)
 
def meta (self)
 
def make_formatter (self)
 
def call_on_close (self, f)
 
def close (self)
 
def command_path (self)
 
def find_root (self)
 
def find_object (self, object_type)
 
def ensure_object (self, object_type)
 
def lookup_default (self, name)
 
def fail (self, message)
 
def abort (self)
 
def exit (self, code=0)
 
def get_usage (self)
 
def get_help (self)
 
def invoke (*args, **kwargs)
 
def forward (*args, **kwargs)
 

Data Fields

 parent
 
 command
 
 info_name
 
 params
 
 args
 
 protected_args
 
 obj
 
 default_map
 
 invoked_subcommand
 
 terminal_width
 
 max_content_width
 
 allow_extra_args
 
 allow_interspersed_args
 
 ignore_unknown_options
 
 help_option_names
 
 token_normalize_func
 
 resilient_parsing
 
 auto_envvar_prefix
 
 color
 
 show_default
 

Detailed Description

The context is a special internal object that holds state relevant
for the script execution at every single level.  It's normally invisible
to commands unless they opt-in to getting access to it.

The context is useful as it can pass internal objects around and can
control special execution features such as reading data from
environment variables.

A context can be used as context manager in which case it will call
:meth:`close` on teardown.

.. versionadded:: 2.0
   Added the `resilient_parsing`, `help_option_names`,
   `token_normalize_func` parameters.

.. versionadded:: 3.0
   Added the `allow_extra_args` and `allow_interspersed_args`
   parameters.

.. versionadded:: 4.0
   Added the `color`, `ignore_unknown_options`, and
   `max_content_width` parameters.

.. versionadded:: 7.1
   Added the `show_default` parameter.

:param command: the command class for this context.
:param parent: the parent context.
:param info_name: the info name for this invocation.  Generally this
                  is the most descriptive name for the script or
                  command.  For the toplevel script it is usually
                  the name of the script, for commands below it it's
                  the name of the script.
:param obj: an arbitrary object of user data.
:param auto_envvar_prefix: the prefix to use for automatic environment
                           variables.  If this is `None` then reading
                           from environment variables is disabled.  This
                           does not affect manually set environment
                           variables which are always read.
:param default_map: a dictionary (like object) with default values
                    for parameters.
:param terminal_width: the width of the terminal.  The default is
                       inherit from parent context.  If no context
                       defines the terminal width then auto
                       detection will be applied.
:param max_content_width: the maximum width for content rendered by
                          Click (this currently only affects help
                          pages).  This defaults to 80 characters if
                          not overridden.  In other words: even if the
                          terminal is larger than that, Click will not
                          format things wider than 80 characters by
                          default.  In addition to that, formatters might
                          add some safety mapping on the right.
:param resilient_parsing: if this flag is enabled then Click will
                          parse without any interactivity or callback
                          invocation.  Default values will also be
                          ignored.  This is useful for implementing
                          things such as completion support.
:param allow_extra_args: if this is set to `True` then extra arguments
                         at the end will not raise an error and will be
                         kept on the context.  The default is to inherit
                         from the command.
:param allow_interspersed_args: if this is set to `False` then options
                                and arguments cannot be mixed.  The
                                default is to inherit from the command.
:param ignore_unknown_options: instructs click to ignore options it does
                               not know and keeps them for later
                               processing.
:param help_option_names: optionally a list of strings that define how
                          the default help parameter is named.  The
                          default is ``['--help']``.
:param token_normalize_func: an optional function that is used to
                             normalize tokens (options, choices,
                             etc.).  This for instance can be used to
                             implement case insensitive behavior.
:param color: controls if the terminal supports ANSI colors or not.  The
              default is autodetection.  This is only needed if ANSI
              codes are used in texts that Click prints which is by
              default not the case.  This for instance would affect
              help output.
:param show_default: if True, shows defaults for all options.
                Even if an option is later created with show_default=False,
                this command-level setting overrides it.

Constructor & Destructor Documentation

◆ __init__()

def __init__ (   self,
  command,
  parent = None,
  info_name = None,
  obj = None,
  auto_envvar_prefix = None,
  default_map = None,
  terminal_width = None,
  max_content_width = None,
  resilient_parsing = False,
  allow_extra_args = None,
  allow_interspersed_args = None,
  ignore_unknown_options = None,
  help_option_names = None,
  token_normalize_func = None,
  color = None,
  show_default = None 
)

Member Function Documentation

◆ __enter__()

def __enter__ (   self)

◆ __exit__()

def __exit__ (   self,
  exc_type,
  exc_value,
  tb 
)

◆ abort()

def abort (   self)
Aborts the script.

◆ call_on_close()

def call_on_close (   self,
  f 
)
This decorator remembers a function as callback that should be
executed when the context tears down.  This is most useful to bind
resource handling to the script execution.  For instance, file objects
opened by the :class:`File` type will register their close callbacks
here.

:param f: the function to execute on teardown.

◆ close()

def close (   self)
Invokes all close callbacks.

◆ command_path()

def command_path (   self)
The computed command path.  This is used for the ``usage``
information on the help page.  It's automatically created by
combining the info names of the chain of contexts to the root.

◆ ensure_object()

def ensure_object (   self,
  object_type 
)
Like :meth:`find_object` but sets the innermost object to a
new instance of `object_type` if it does not exist.

◆ exit()

def exit (   self,
  code = 0 
)
Exits the application with a given exit code.

◆ fail()

def fail (   self,
  message 
)
Aborts the execution of the program with a specific error
message.

:param message: the error message to fail with.

◆ find_object()

def find_object (   self,
  object_type 
)
Finds the closest object of a given type.

◆ find_root()

def find_root (   self)
Finds the outermost context.

◆ forward()

def forward ( args,
**  kwargs 
)
Similar to :meth:`invoke` but fills in default keyword
arguments from the current context if the other command expects
it.  This cannot invoke callbacks directly, only other commands.

◆ get_help()

def get_help (   self)
Helper method to get formatted help page for the current
context and command.

◆ get_usage()

def get_usage (   self)
Helper method to get formatted usage string for the current
context and command.

◆ invoke()

def invoke ( args,
**  kwargs 
)
Invokes a command callback in exactly the way it expects.  There
are two ways to invoke this method:

1.  the first argument can be a callback and all other arguments and
    keyword arguments are forwarded directly to the function.
2.  the first argument is a click command object.  In that case all
    arguments are forwarded as well but proper click parameters
    (options and click arguments) must be keyword arguments and Click
    will fill in defaults.

Note that before Click 3.2 keyword arguments were not properly filled
in against the intention of this code and no context was created.  For
more information about this change and why it was done in a bugfix
release see :ref:`upgrade-to-3.2`.

◆ lookup_default()

def lookup_default (   self,
  name 
)
Looks up the default for a parameter name.  This by default
looks into the :attr:`default_map` if available.

◆ make_formatter()

def make_formatter (   self)
Creates the formatter for the help and usage output.

◆ meta()

def meta (   self)
This is a dictionary which is shared with all the contexts
that are nested.  It exists so that click utilities can store some
state here if they need to.  It is however the responsibility of
that code to manage this dictionary well.

The keys are supposed to be unique dotted strings.  For instance
module paths are a good choice for it.  What is stored in there is
irrelevant for the operation of click.  However what is important is
that code that places data here adheres to the general semantics of
the system.

Example usage::

    LANG_KEY = f'{__name__}.lang'

    def set_language(value):
ctx = get_current_context()
ctx.meta[LANG_KEY] = value

    def get_language():
return get_current_context().meta.get(LANG_KEY, 'en_US')

.. versionadded:: 5.0

◆ scope()

def scope (   self,
  cleanup = True 
)
This helper method can be used with the context object to promote
it to the current thread local (see :func:`get_current_context`).
The default behavior of this is to invoke the cleanup functions which
can be disabled by setting `cleanup` to `False`.  The cleanup
functions are typically used for things such as closing file handles.

If the cleanup is intended the context object can also be directly
used as a context manager.

Example usage::

    with ctx.scope():
assert get_current_context() is ctx

This is equivalent::

    with ctx:
assert get_current_context() is ctx

.. versionadded:: 5.0

:param cleanup: controls if the cleanup functions should be run or
        not.  The default is to run these functions.  In
        some situations the context only wants to be
        temporarily pushed in which case this can be disabled.
        Nested pushes automatically defer the cleanup.

Field Documentation

◆ allow_extra_args

allow_extra_args

◆ allow_interspersed_args

allow_interspersed_args

◆ args

args

◆ auto_envvar_prefix

auto_envvar_prefix

◆ color

color

◆ command

command

◆ default_map

default_map

◆ help_option_names

help_option_names

◆ ignore_unknown_options

ignore_unknown_options

◆ info_name

info_name

◆ invoked_subcommand

invoked_subcommand

◆ max_content_width

max_content_width

◆ obj

obj

◆ params

params

◆ parent

parent

◆ protected_args

protected_args

◆ resilient_parsing

resilient_parsing

◆ show_default

show_default

◆ terminal_width

terminal_width

◆ token_normalize_func

token_normalize_func

The documentation for this class was generated from the following file: