File: console_helpers.py

package info (click to toggle)
python-friendly-traceback 0.7.62%2Bgit20240811.d7dbff6-1.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 9,264 kB
  • sloc: python: 21,500; makefile: 4
file content (519 lines) | stat: -rw-r--r-- 17,955 bytes parent folder | download
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
"""
This module includes functions that can be used in a friendly console
or in other **interactive** environments such as in a Jupyter notebook.

If you intend to use ``friendly_traceback`` in an interactive environment,
you should use ``friendly`` instead, as in::

    from friendly.ipython import ...
    # or
    from friendly.jupyter import ...
    # etc.

Since this documentation is generated automatically by Sphinx using
``friendly_traceback`` code, it will not contain all the relevant
functions specific to a given environment. To find out about what
functions are available in such environments, do the following::

    prompt>> from friendly.X import Friendly
    prompt>> Friendly
    # The output will include the required information
"""

# NOTE: __all__ is defined at the very bottom of this file
import inspect
import sys
import types
from typing import Any, Callable, Dict, List, Optional, Type, Union

import friendly_traceback
from friendly_traceback import __version__, base_formatters, debug_helper
from friendly_traceback.config import session
from friendly_traceback.ft_gettext import current_lang
from friendly_traceback.functions_help import add_help_attribute
from friendly_traceback.info_generic import get_generic_explanation
from friendly_traceback.path_info import show_paths
from friendly_traceback.syntax_errors.source_info import Statement
from friendly_traceback.tb_data import TracebackData
from friendly_traceback.typing_info import InclusionChoice, Site

_ = current_lang.translate


def _nothing_to_show():
    return _("Nothing to show: no exception recorded.")


def disable():
    """Disable friendly-traceback exception hook"""
    if not session.installed:
        info = {"suggest": _("Friendly-traceback is already disabled.") + "\n"}
        explanation = session.formatter(info, include="hint")
        session.write_err(explanation)
        return
    session.uninstall()


def enable():
    """Enable friendly-traceback exception hook"""
    if session.installed:
        info = {"suggest": _("Friendly-traceback is already enabled.") + "\n"}
        explanation = session.formatter(info, include="hint")
        session.write_err(explanation)
        return
    session.install()


def explain(index: int = -1, include: InclusionChoice = "explain") -> None:
    """Shows the previously recorded traceback info again,
    with the option to specify different items to include.
    For example, ``explain("why")`` is equivalent to ``why()``.
    """
    old_include = friendly_traceback.get_include()
    friendly_traceback.set_include(include)
    session.show_traceback_info_again(index)
    friendly_traceback.set_include(old_include)


def friendly_tb(index: int = -1) -> None:
    """Shows the simplified Python traceback,
    which includes the hint/suggestion if available.
    """
    explain(index, include="friendly_tb")


def hint(index: int = -1) -> None:
    """Shows hint/suggestion if available."""
    explain(index, include="hint")


class History:
    __name__ = "history"

    def __call__(self):
        """Prints a list of recorded tracebacks and warning messages"""
        if not session.recorded_tracebacks:
            info = {"suggest": _nothing_to_show() + "\n"}
            explanation = session.formatter(info, include="hint")
            session.write_err(explanation)
            return
        session.rich_add_vspace = False  # for friendly
        for index, tb in enumerate(session.recorded_tracebacks):
            if "message" in tb.info:
                info = {"message": f"{index}. {tb.info['message']}"}
                explanation = session.formatter(info, include="message")
                session.write_err(explanation)
        session.rich_add_vspace = True

    def __repr__(self):
        return (
            _("Shows a list of recorded traceback messages.")
            + "\n"
            + _("You can also use `history.clear()` and `history.pop()`.")
        )

    def clear(self):
        """Removes all recorded tracebacks and warnings"""
        session.recorded_tracebacks.clear()

    def pop(self, index=-1):
        """Used to delete a particular item, with the last item removed by default"""
        if not session.recorded_tracebacks:
            info = {"suggest": _("Nothing to delete: no exception recorded.") + "\n"}
            explanation = session.formatter(info, include="hint")
            session.write_err(explanation)
            return
        try:
            session.recorded_tracebacks.pop(index)
        except IndexError:
            info = {"suggest": _("There is no such history item.") + "\n"}
            explanation = session.formatter(info, include="hint")
            session.write_err(explanation)


history = History()


def python_tb(index: int = -1) -> None:
    """Shows the Python traceback, excluding files from friendly
    itself.
    """
    explain(index, include="python_tb")


def what(
    exception_or_index: Union[
        Type[BaseException], str, bytes, types.CodeType, int, None
    ] = None,
    pre: bool = False,
) -> None:
    """If known, shows the generic explanation about a given exception.

    If the ``pre`` argument is set to ``True``, the output is
    formatted in a way that is suitable for inclusion in the
    documentation.
    """
    if exception_or_index is None:
        explain(index=-1, include="what")
        return
    elif isinstance(exception_or_index, int):
        explain(index=exception_or_index, include="what")
        return

    if inspect.isclass(exception_or_index) and issubclass(
        exception_or_index, BaseException
    ):
        result = get_generic_explanation(exception_or_index)
    else:
        try:
            exc = eval(exception_or_index)  # skipcq PYL-W0123
            if inspect.isclass(exc) and issubclass(exc, BaseException):
                result = get_generic_explanation(exc)
            else:
                result = _("{exception} is not an exception.").format(
                    exception=f"`{exception_or_index}`"
                )
        except Exception:  # noqa
            result = _("{exception} is not an exception.").format(
                exception=f"`{exception_or_index}`"
            )

    if pre:  # for documentation # pragma: no cover
        lines = result.split("\n")
        for line in lines:
            session.write_err(f"    {line}\n")
        session.write_err("\n")
    else:
        info = {"generic": result}
        explanation = session.formatter(info, include="what")
        session.write_err(explanation)


def where(index: int = -1, more=False) -> None:
    """Shows the information about where the exception occurred"""
    if more:
        try:
            info = session.recorded_tracebacks[index].info
        except Exception:
            explain(index, "where")  # let the error bubble up
        else:
            if "detailed_tb" in info:
                explain(index, "detailed_tb")
            else:
                explain(index, "where")
    else:
        explain(index, "where")


def why(index: int = -1) -> None:
    """Shows the likely cause of the exception."""
    # If no cause is found, and the exception name is not accompanied by a
    # message, as in "StopIteration:", we use the same info for
    # the cause as we used for the generic information as per issue #66
    try:
        if index == -1:
            info = session.recorded_tracebacks[-1].info
        else:
            info = session.recorded_tracebacks[index + 1].info
    except IndexError:
        info = {}
    if (
        ("cause" not in info or not info["cause"])
        and "message" in info
        and len(info["message"].split(":")) > 1
        and not info["message"].split(":")[1].strip()  # empty message
    ):
        explain(index, "what")
    else:
        explain(index, "why")


def www(site_or_index: Union[Site, int] = -1) -> None:  # pragma: no cover
    """This uses the ``webbrowser`` module to open a tab (or window)
     in the default browser, linking to a specific url
     or opening the default email client.

    * If the argument ``site_or_index`` is not specified,
      and an exception or a warning has been raised,
      an internet search will be done using
      the exception message of the last exception or warning
      as the search string.

    * If the argument ``site_or_index`` is not specified,
      and NO exception has been raised,
      Friendly's documentation will open.

    * If the argument ``site_or_index`` == "friendly",
      Friendly's documentation will open.

    * If the argument ``site_or_index`` == "python", Python's documentation site
      will open with the currently used Python version.

    * If the argument ``site_or_index`` == "bug",
      the Issues page for Friendly on GitHub will open.

    * If the argument ``site_or_index`` == "email",
      the default email client should open with Friendly's
      developer's address already filled in.

    * If the argument ``site_or_index`` is an integer representing an item
      in the recorded history,  an internet search will be done using
      the exception message of the corresponding item as the search string.
    """
    import urllib.parse
    import webbrowser

    def open_browser(url):
        try:
            webbrowser.open_new_tab(url)
        except Exception:  # noqa
            session.write_err(
                _("The default web browser cannot be used for searching.")
            )
            return

    urls: Dict[Site, str] = {
        "friendly": "https://friendly-traceback.github.io/docs/index.html",
        "python": f"https://docs.python.org/3.{sys.version_info.minor}/",
        "bug": "https://github.com/friendly-traceback/friendly-traceback/issues/new",
        "email": "mailto:andre.roberge@gmail.com",
    }
    invalid_args = _(
        "Invalid argument for `www()`.\n"
        "Valid arguments are integer values or one of `{sites}`.\n"
    ).format(sites=repr(urls.keys()))

    if isinstance(site_or_index, int):
        index = site_or_index
    else:
        try:
            site = site_or_index.casefold()
        except Exception:  # noqa
            session.write_err(invalid_args)
            return
        else:
            if site not in urls:
                session.write_err(invalid_args)
                return
            return open_browser(urls[site])

    try:
        info = (
            session.recorded_tracebacks[index].info
            if session.recorded_tracebacks
            else {}
        )
    except IndexError:
        session.write_err(_("Invalid index value."))
        return

    url = urls["friendly"]
    if info:
        message = info["message"].replace("'", "")
        if " (" in message:
            message = message.split("(")[0]
        url = f"https://duckduckgo.com?q={urllib.parse.quote(message)}"

    return open_browser(url)


def set_debug(flag: bool = True) -> None:  # pragma: no cover
    """This sets the value of the debug flag for the current session."""
    debug_helper.DEBUG = flag
    debug_helper.SHOW_DEBUG_HELPER = flag


get_lang = friendly_traceback.get_lang
set_lang = friendly_traceback.set_lang
get_include = friendly_traceback.get_include
set_include = friendly_traceback.set_include
set_formatter = friendly_traceback.set_formatter

# ===== Debugging functions are not unit tested by choice =====


def _get_statement() -> Optional[Statement]:  # pragma: no cover
    """This returns a 'Statement' instance obtained for SyntaxErrors and
    subclasses.  Such a Statement instance contains essentially all
    the known information about the statement where the error occurred.

    This is not intended for end-users but is useful in development.
    """
    if not session.recorded_tracebacks:
        print(_nothing_to_show())
        return None
    current_tb = session.recorded_tracebacks[-1]
    if hasattr(current_tb, "tb_data"):
        if isinstance(
            session.recorded_tracebacks[-1].tb_data.exception_instance, SyntaxError
        ):
            return session.recorded_tracebacks[-1].tb_data.statement
    elif hasattr(current_tb, "problem_statement"):
        return current_tb.problem_statement
    print("No statement: likely not a SyntaxError nor a SyntaxWarning.")
    return None


def _get_tb_data() -> Optional[TracebackData]:  # pragma: no cover
    """This returns the TracebackData instance containing all the
    information we have obtained.

    This is not intended for end-users but is useful in development.
    """
    if not session.recorded_tracebacks:
        print(_nothing_to_show())
        return None
    current_tb = session.recorded_tracebacks[-1]
    return current_tb.tb_data if hasattr(current_tb, "tb_data") else current_tb


def _get_info() -> list:
    """Debugging tool: returns the content of a traceback info."""
    return session.recorded_tracebacks[-1].info if session.recorded_tracebacks else []


def _show_info() -> None:  # pragma: no cover
    """Debugging tool: shows the complete content of traceback info.

    Prints ``''`` for a given item if it is not present.
    """
    info = session.recorded_tracebacks[-1].info if session.recorded_tracebacks else []

    for item in base_formatters.items_in_order:
        if item in info and info[item].strip():
            print(f"{item}:")
            for line in info[item].strip().split("\n"):
                print("   ", line)
            print()
        else:
            print(f"{item}: ''")

    for item in info:
        if item not in base_formatters.items_in_order:
            print(f"{item}: {info[item]}")


helpers: Dict[str, Callable[..., None]] = {
    "why": why,
    "what": what,
    "where": where,
    "www": www,
    "explain": explain,
    "hint": hint,
    "enable": enable,
    "disable": disable,
    "history": history,
    "friendly_tb": friendly_tb,
    "python_tb": python_tb,
    "show_paths": show_paths,
    "get_include": get_include,
    "set_include": set_include,
    "get_lang": get_lang,
    "set_lang": set_lang,
    "set_formatter": set_formatter,
    "set_debug": set_debug,
}
add_help_attribute(helpers)

debug_helper_methods: Dict[str, Callable[..., Any]] = {
    "_get_info": _get_info,
    "_get_tb_data": _get_tb_data,
    "_get_statement": _get_statement,
}
add_help_attribute(debug_helper_methods)


class FriendlyHelpers:
    """Helper class which can be used in a console as an alternative
    to using the helper functions directly.
    This can be helpful if one of the helper functions gets redefined.

    It is usually instantiated using the name ``Friendly``.

    For example, we can write ``Friendly.why()`` as equivalent to ``why()``.
    """

    version = __version__

    def __init__(self) -> None:
        self.helpers = {}

    def add_helper(self, function: Callable) -> None:  # pragma: no cover
        """Adds a helper base on its name and the function it refers to."""
        self.helpers[function.__name__] = function
        setattr(self, function.__name__, function)

    def remove_helper(self, name: str) -> None:  # pragma: no cover
        """Removes a helper from the FriendlyHelpers object"""
        if name in self.helpers:
            del self.helpers[name]
            delattr(self, name)
        else:
            message = f"Cannot remove {name}; it is not a known helper."
            debug_helper.log(message)

    def __dir__(self) -> List[str]:  # pragma: no cover
        """Only include useful friendly methods."""
        return sorted(list(self.helpers))

    def __repr__(self) -> str:  # pragma: no cover
        """Shows a brief description in the default language of what
        each function/method does.

        Debugging helper functions are only included if the DEBUG flag is set.
        """
        basic_helpers = {}
        _debug_helpers = {}
        for name in self.helpers:
            if name.startswith("_"):
                _debug_helpers[name] = self.helpers[name]
            else:
                basic_helpers[name] = self.helpers[name]

        # sort alphabetically, then by length name for nicer display
        basic_helpers = sorted(list(basic_helpers))
        basic_helpers = sorted(basic_helpers, key=len)
        _debug_helpers = sorted(list(_debug_helpers))
        _debug_helpers = sorted(_debug_helpers, key=len)

        header = _(
            "The following methods of the Friendly object should also "
            "be available as functions."
        )
        parts = [self.true_repr() + "\n" + header + "\n\n"]
        for name in basic_helpers:
            parts.append(name + "(): ")
            fn = self.helpers[name]
            if hasattr(fn, "help"):
                parts.append(fn.help() + "\n")
            else:
                print("Warning:", name, "has no help() method.")

        if debug_helper.SHOW_DEBUG_HELPER:
            more_header = "Debugging methods (English only)."
            parts.append("\n" + more_header + "\n\n")
            for name in _debug_helpers:
                parts.append(name + "(): ")
                fn = self.helpers[name]
                if hasattr(fn, "help"):
                    parts.append(fn.help() + "\n")
                else:
                    print("Warning:", name, "has no help() method.")
        return "".join(parts)

    def true_repr(self) -> str:  # pragma: no cover
        """Method that can be called when a normal looking repr is needed."""
        return "<class FriendlyHelpers>"


Friendly = FriendlyHelpers()
for helper_name in helpers:
    Friendly.add_helper(helpers[helper_name])
for helper_name in debug_helper_methods:
    Friendly.add_helper(debug_helper_methods[helper_name])

helpers["Friendly"] = Friendly
# We don't include the debug helpers in __all__ so that they do
# not show when doing dir() at a console; we only make them
# available as methods of the Friendly object.
__all__ = list(helpers.keys())