| 1 |
""" |
| 2 |
base_formatters.py |
| 3 |
=================== |
| 4 |
|
| 5 |
This file contains the formatters included with ``friendly_traceback``. |
| 6 |
Other formatters of potential interest, including a formatter designed |
| 7 |
for IDLE as well as some making use of Rich, are included with ``friendly``. |
| 8 |
|
| 9 |
A formatter is a function that takes two arguments: |
| 10 |
|
| 11 |
1. a dict (named ``info`` everywhere in friendly files) containing |
| 12 |
all the information that can be shown to the user, as well as some |
| 13 |
entries that are meant to be used only internally as the full |
| 14 |
friendly information is obtained. |
| 15 |
|
| 16 |
2. A second argument which is meant to convey what information should be shown. |
| 17 |
This second argument used to be a single integer ("verbosity level"). |
| 18 |
It is currently recently being replaced by a single string. However, |
| 19 |
this might change as we experiment with various options prior to |
| 20 |
version 1.0 |
| 21 |
|
| 22 |
A formatter returns a single string. By default, this string will be |
| 23 |
written to stderr; however this can be changed by the calling program. |
| 24 |
|
| 25 |
This module currently contains 2 formatters: |
| 26 |
|
| 27 |
* ``repl()``: This is used to print the information in a traditional console. |
| 28 |
The indentation of the traceback itself is chosen |
| 29 |
to reproduce that of a normal Python traceback. |
| 30 |
|
| 31 |
* ``docs()``: this produces output with leading spaces so that it can be |
| 32 |
embedded as a code-block in a file (such as .rst). It can also be used |
| 33 |
to print the information in a traditional console. |
| 34 |
""" |
| 35 |
|
| 36 |
from typing import Dict, List, Set |
| 37 |
|
| 38 |
from . import debug_helper |
| 39 |
from .ft_gettext import current_lang |
| 40 |
from .typing_info import InclusionChoice, Info |
| 41 |
|
| 42 |
_ = current_lang.translate |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
items_in_order = [ |
| 48 |
"header", |
| 49 |
"message", |
| 50 |
"original_python_traceback", |
| 51 |
"simulated_python_traceback", |
| 52 |
"shortened_traceback", |
| 53 |
"exception_notes_intro", |
| 54 |
"exception_notes", |
| 55 |
"suggest", |
| 56 |
"warning_message", |
| 57 |
"generic", |
| 58 |
"parsing_error", |
| 59 |
"parsing_error_source", |
| 60 |
"cause", |
| 61 |
"last_call_header", |
| 62 |
"last_call_source", |
| 63 |
"last_call_variables", |
| 64 |
"exception_raised_header", |
| 65 |
"exception_raised_source", |
| 66 |
"exception_raised_variables", |
| 67 |
"warning_location_header", |
| 68 |
"warning_source", |
| 69 |
"warning_variables", |
| 70 |
"additional_variable_warning", |
| 71 |
] |
| 72 |
|
| 73 |
|
| 74 |
repl_indentation = { |
| 75 |
"header": "single", |
| 76 |
"message": "single", |
| 77 |
"simulated_python_traceback": "none", |
| 78 |
"original_python_traceback": "none", |
| 79 |
"shortened_traceback": "none", |
| 80 |
"exception_notes_intro": "none", |
| 81 |
"exception_notes": "none", |
| 82 |
"suggest": "double", |
| 83 |
"warning_message": "single", |
| 84 |
"generic": "single", |
| 85 |
"parsing_error": "single", |
| 86 |
"parsing_error_source": "none", |
| 87 |
"cause": "single", |
| 88 |
"last_call_header": "single", |
| 89 |
"last_call_source": "none", |
| 90 |
"last_call_variables": "double", |
| 91 |
"exception_raised_header": "single", |
| 92 |
"exception_raised_source": "none", |
| 93 |
"exception_raised_variables": "double", |
| 94 |
"warning_location_header": "single", |
| 95 |
"warning_source": "none", |
| 96 |
"warning_variables": "double", |
| 97 |
"additional_variable_warning": "single", |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
def repl(info: Info, include: InclusionChoice = "friendly_tb") -> str: |
| 102 |
"""Default formatter, primarily for console usage. |
| 103 |
|
| 104 |
The only change made to the content of "info" is |
| 105 |
some added indentation. |
| 106 |
""" |
| 107 |
if include == "message": |
| 108 |
return info["message"] |
| 109 |
if include == "detailed_tb": |
| 110 |
return detailed_tb(info) if "detailed_tb" in info else "" |
| 111 |
items_to_show = select_items(include) |
| 112 |
spacing = {"single": " " * 4, "double": " " * 8, "none": ""} |
| 113 |
result = [""] |
| 114 |
for item in items_to_show: |
| 115 |
if item in info: |
| 116 |
indentation = spacing[repl_indentation[item]] |
| 117 |
if item == "exception_notes": |
| 118 |
lines = [] |
| 119 |
for note in info[item]: |
| 120 |
note_lines = note.split("\n") |
| 121 |
if "exception_notes_intro" in items_to_show: |
| 122 |
for index, line in enumerate(note_lines): |
| 123 |
if index == 0: |
| 124 |
note_lines[0] = f"* {note_lines[0]}" |
| 125 |
else: |
| 126 |
note_lines[index] = f" {note_lines[index]}" |
| 127 |
lines.extend(note_lines) |
| 128 |
lines.append("\n") |
| 129 |
else: |
| 130 |
lines = info[item].split("\n") |
| 131 |
result.extend(indentation + line for line in lines) |
| 132 |
if result == [""] or not result: |
| 133 |
return no_result(info, include) |
| 134 |
|
| 135 |
return "\n".join(result) |
| 136 |
|
| 137 |
|
| 138 |
def detailed_tb(info: Info) -> str: |
| 139 |
"""Unlike the normal information from 'where()', which focus on at |
| 140 |
most two frames, detailed_tb() gives information for all the frames. |
| 141 |
It is used mostly in IPython based environment - especially with |
| 142 |
the 'button-based' mode in Jupyter notebooks/lab.""" |
| 143 |
if "detailed_tb" not in info: |
| 144 |
return "" |
| 145 |
result = [""] |
| 146 |
spacing = {"location": " " * 4, "var_info": " " * 8} |
| 147 |
for location, source, var_info in info["detailed_tb"]: |
| 148 |
result.append(spacing["location"] + location) |
| 149 |
result.extend(iter(source.split("\n"))) |
| 150 |
result.extend(spacing["var_info"] + line for line in var_info.split("\n")) |
| 151 |
return "\n".join(result) |
| 152 |
|
| 153 |
|
| 154 |
def docs( |
| 155 |
info: Info, include: InclusionChoice = "friendly_tb" |
| 156 |
) -> str: |
| 157 |
"""Formatter that produces an output that is suitable for |
| 158 |
insertion in a RestructuredText (.rst) code block, |
| 159 |
with pre-formatted indentation. |
| 160 |
|
| 161 |
The only change made to the content of "info" is |
| 162 |
some added indentation. |
| 163 |
""" |
| 164 |
|
| 165 |
pre_items = dict(**repl_indentation) |
| 166 |
|
| 167 |
pre_items.update( |
| 168 |
**{ |
| 169 |
"simulated_python_traceback": "single", |
| 170 |
"original_python_traceback": "single", |
| 171 |
"shortened_traceback": "single", |
| 172 |
"exception_notes_intro": "single", |
| 173 |
"exception_notes": "single", |
| 174 |
} |
| 175 |
) |
| 176 |
if include == "message": |
| 177 |
return info["message"] |
| 178 |
if include == "detailed_tb": |
| 179 |
return detailed_tb(info) if "detailed_tb" in info else "" |
| 180 |
items_to_show = select_items(include) |
| 181 |
spacing = {"single": " " * 4, "double": " " * 8, "none": ""} |
| 182 |
result = [""] |
| 183 |
for item in items_to_show: |
| 184 |
if item in info and info[item].strip(): |
| 185 |
indentation = spacing[pre_items[item]] |
| 186 |
result.extend(indentation + line for line in info[item].split("\n")) |
| 187 |
return no_result(info, include) if result == [""] else "\n".join(result) |
| 188 |
|
| 189 |
|
| 190 |
def no_result(info: Info, include: InclusionChoice) -> str: |
| 191 |
"""Should normally only be called if no result is available |
| 192 |
from either hint() or why(). |
| 193 |
""" |
| 194 |
if include == "why": |
| 195 |
return _("I have no suggestion to offer.") |
| 196 |
|
| 197 |
if include == "hint": |
| 198 |
if "cause" in info: |
| 199 |
return _("I have no suggestion to offer; try `why()`.") |
| 200 |
|
| 201 |
return _("I have no suggestion to offer.") |
| 202 |
|
| 203 |
debug_helper.log( |
| 204 |
f"Internal error: include = {include} in base_formatters.no_result()" |
| 205 |
) |
| 206 |
return "" |
| 207 |
|
| 208 |
|
| 209 |
items_groups: Dict[InclusionChoice, Set[str]] = { |
| 210 |
"message": {"message"}, |
| 211 |
"message_plus": {"message", "suggest"}, |
| 212 |
"hint": {"suggest"}, |
| 213 |
"what": {"generic"}, |
| 214 |
"why": {"warnings", "cause"}, |
| 215 |
"where": { |
| 216 |
"parsing_error", |
| 217 |
"parsing_error_source", |
| 218 |
"last_call_header", |
| 219 |
"last_call_source", |
| 220 |
"last_call_variables", |
| 221 |
"exception_raised_header", |
| 222 |
"exception_raised_source", |
| 223 |
"exception_raised_variables", |
| 224 |
"warning_location_header", |
| 225 |
"warning_source", |
| 226 |
"warning_variables", |
| 227 |
"additional_variable_warning", |
| 228 |
}, |
| 229 |
"friendly_tb": { |
| 230 |
"shortened_traceback", |
| 231 |
"suggest", |
| 232 |
"warnings", |
| 233 |
"warning_message", |
| 234 |
"exception_notes_intro", |
| 235 |
"exception_notes", |
| 236 |
}, |
| 237 |
"python_tb": {"simulated_python_traceback", "exception_notes"}, |
| 238 |
"debug_tb": {"original_python_traceback", "exception_notes"}, |
| 239 |
"detailed_tb": {"detailed_tb"}, |
| 240 |
} |
| 241 |
items_groups["explain"] = ( |
| 242 |
items_groups["friendly_tb"] |
| 243 |
.union(items_groups["what"]) |
| 244 |
.union(items_groups["why"]) |
| 245 |
.union(items_groups["where"]) |
| 246 |
) |
| 247 |
|
| 248 |
items_groups["no_tb"] = set(items_groups["explain"]) |
| 249 |
for item_ in items_groups["friendly_tb"]: |
| 250 |
items_groups["no_tb"].discard(item_) |
| 251 |
|
| 252 |
|
| 253 |
def select_items(group_name: InclusionChoice) -> List[str]: |
| 254 |
items = items_groups[group_name] |
| 255 |
return [item for item in items_in_order if item in items] |