Source code for jgdv.logging.format.stack_m

 1#!/usr/bin/env python3
 2"""
 3
 4"""
 5# Import:
 6from __future__ import annotations
 7
 8# ##-- stdlib imports
 9import datetime
10import enum
11import functools as ftz
12import itertools as itz
13import logging as logmod
14import pathlib as pl
15import re
16import time
17import weakref
18from uuid import UUID, uuid1
19# ##-- end stdlib imports
20
21import stackprinter
22
23# ##-- types
24# isort: off
25# General
26import abc
27import collections.abc
28import typing
29import types
30from typing import cast, assert_type, assert_never
31from typing import Generic, NewType, Never
32from typing import no_type_check, final, override, overload
33# Protocols and Interfaces:
34from typing import Protocol, runtime_checkable
35# isort: on
36# ##-- end types
37
38# ##-- type checking
39# isort: off
40if typing.TYPE_CHECKING:
41    from typing import Final, ClassVar, Any, Self
42    from typing import Literal, LiteralString
43    from typing import TypeGuard
44    from collections.abc import Iterable, Iterator, Callable, Generator
45    from collections.abc import Sequence, Mapping, MutableMapping, Hashable
46
47    from jgdv import Maybe, RxStr
48## isort: on
49# ##-- end type checking
50
51# Global Vars:
52
53# Body:
54
[docs] 55class StackFormatter_m: 56 """ A Mixin Error formatter, adapted from stackprinter's docs 57 Compactly Formats the error stack trace, without src. 58 59 """ 60 61 indent_str : ClassVar[str] = " | " 62 suppress : ClassVar[list[RxStr]] = [r".*pydantic.*", r"<frozen importlib._bootstrap>"] 63 source_height : ClassVar[int] = 10 64 source_lines : ClassVar[int|str] = 0 65 use_stackprinter : bool = True 66
[docs] 67 def formatException(self, exc_info:Maybe[tuple]) -> str: # noqa: N802 68 match exc_info: 69 case None | (None, None, None): 70 return "" 71 case _ if not self.use_stackprinter: 72 return cast("str", super().formatException(exc_info)) # type: ignore[misc] 73 74 msg : str = stackprinter.format(exc_info, 75 source_lines=self.source_lines, 76 suppressed_paths=self.suppress) 77 lines = [x for x in msg.splitlines() if bool(x)] 78 indented = [f"{self.indent_str}{line}\n" for line in lines[-self.source_height:]] 79 return "".join(indented)
80
[docs] 81 def formatStack(self, stack_info:str) -> str: # noqa: N802 82 return cast("str", super().formatStack(stack_info)) # type: ignore[misc]