1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
|
"""This module is intended to add help attributes to various functions."""
from typing import Any, Callable, Dict, Union
from friendly_traceback import debug_helper
from friendly_traceback.ft_gettext import current_lang
_ = current_lang.translate
short_description = {
"disable": lambda: _(
"Disable friendly-traceback and restore previous exception hook."
),
"enable": lambda: _("Enable friendly-traceback as an exception hook"),
"explain": lambda: _("Shows all the information about the last traceback."),
"history": lambda: _("Shows a list of recorded traceback messages.")
+ " "
+ _("You can also use `history.clear()` and `history.pop()`."),
"set_lang": lambda: _("Sets the language to be used."),
"show_paths": lambda: _("Shows the paths corresponding to synonyms used."),
"what": lambda: _("Shows the generic meaning of a given exception."),
"where": lambda: _("Shows where an exception was raised."),
"why": lambda: _("Shows the likely cause of the exception."),
"www": lambda: _("Opens a web browser at a useful location."),
"hint": lambda: _("Suggestion sometimes added to a friendly traceback."),
"python_tb": lambda: _("Shows a normal Python traceback."),
"friendly_tb": lambda: _("Shows a simplified Python traceback."),
"get_include": lambda: _(
"Returns the current value used for items to include by default."
),
"set_include": lambda: _(
"Sets the items to show by default when an exception is raised."
),
"get_lang": lambda: _("Returns the language currently used."),
"set_formatter": lambda: _("Sets the formatter to use for display."),
"set_debug": lambda: _("Use True (default) or False to enable debugging methods."),
# The following are not translated by choice.
"_get_info": lambda: "Returns the content of the traceback items",
"_get_statement": lambda: "Returns a special Statement object for SyntaxError.",
"_get_tb_data": lambda: "Return a special traceback object.",
}
def add_help_attribute(
functions: Dict[str, Callable[..., Any]], description: Union[Dict, None] = None
) -> None:
"""Given a dict whose content is of the form
{function_name_string: function_obj}
it adds customs `help` and `__rich__repr` attributes for all such
function objects.
"""
if description is None:
description = short_description
for name in functions:
if name not in description: # pragma: no cover
debug_helper.log(f"Missing description for {name}.")
continue
func = functions[name]
setattr(func, "help", description[name]) # noqa
setattr(
func, "__rich_repr__", lambda func=func: (func.help(),)
) # pragma: no cover
|