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