def __init__( self, pygments_style=style_with_executing_node("monokai", "bg:#005080"), ): self.pygments_formatter = HtmlFormatter( style=pygments_style, nowrap=True, )
def __init__( self, *, options=Options(), pygmented=False, show_executing_node=True, pygments_formatter_cls=None, pygments_formatter_kwargs=None, pygments_style="monokai", executing_node_modifier="bg:#005080", executing_node_underline="^", current_line_indicator="-->", line_gap_string="(...)", show_variables=False, use_code_qualname=True, show_linenos=True, strip_leading_indent=True, html=False, chain=True, collapse_repeated_frames=True ): if pygmented and not options.pygments_formatter: if show_executing_node: pygments_style = style_with_executing_node( pygments_style, executing_node_modifier ) if pygments_formatter_cls is None: from pygments.formatters.terminal256 import Terminal256Formatter \ as pygments_formatter_cls options.pygments_formatter = pygments_formatter_cls( style=pygments_style, **pygments_formatter_kwargs or {}, ) self.pygmented = pygmented self.show_executing_node = show_executing_node assert_( len(executing_node_underline) == 1, ValueError("executing_node_underline must be a single character"), ) self.executing_node_underline = executing_node_underline self.current_line_indicator = current_line_indicator or "" self.line_gap_string = line_gap_string self.show_variables = show_variables self.show_linenos = show_linenos self.use_code_qualname = use_code_qualname self.strip_leading_indent = strip_leading_indent self.html = html self.chain = chain self.options = options self.collapse_repeated_frames = collapse_repeated_frames
def test_executing_style_defs(): style = style_with_executing_node("native", "bg:#ffff00") formatter = HtmlFormatter(style=style) style_defs = formatter.get_style_defs() expected = """ .c { color: #999999; font-style: italic } .err { color: #a61717; background-color: #e3d2d2 } .c-ExecutingNode { color: #999999; font-style: italic; background-color: #ffff00 } .err-ExecutingNode { color: #a61717; background-color: #ffff00 } """.strip().splitlines() for line in expected: assert line.strip() in style_defs
def get_records(self, etb, number_of_lines_of_context, tb_offset): context = number_of_lines_of_context - 1 after = context // 2 before = context - after if self.has_colors: style = get_style_by_name('default') style = stack_data.style_with_executing_node(style, 'bg:#00005f') formatter = Terminal256Formatter(style=style) else: formatter = None options = stack_data.Options( before=before, after=after, pygments_formatter=formatter, ) return list(stack_data.FrameInfo.stack_data(etb, options=options))[tb_offset:]
def get_records(self, etb: TracebackType, number_of_lines_of_context: int, tb_offset: int): context = number_of_lines_of_context - 1 after = context // 2 before = context - after if self.has_colors: style = get_style_by_name("default") style = stack_data.style_with_executing_node( style, "bg:ansiyellow") formatter = Terminal256Formatter(style=style) else: formatter = None options = stack_data.Options( before=before, after=after, pygments_formatter=formatter, ) assert etb is not None return list(stack_data.FrameInfo.stack_data( etb, options=options))[tb_offset:]
def print_stack(): result = "" for formatter_cls in [ Terminal256Formatter, TerminalFormatter, TerminalTrueColorFormatter, HtmlFormatter, ]: for style in [ "native", style_with_executing_node("native", "bg:#444400") ]: result += "{formatter_cls.__name__} {style}:\n\n".format( **locals()) formatter = formatter_cls(style=style) options = Options(pygments_formatter=formatter) frame = inspect.currentframe().f_back for frame_info in list(FrameInfo.stack_data(frame, options))[-2:]: for line in frame_info.lines: result += '{:4} | {}\n'.format(line.lineno, line.render(pygmented=True)) result += "-----\n" result += "\n====================\n\n" return result
import pygments from cheap_repr import cheap_repr from friendly.core import FriendlyTraceback from pygments.formatters.html import HtmlFormatter from stack_data import ( style_with_executing_node, Options, Line, FrameInfo, Variable, RepeatedFrames, ) from core.utils import is_valid_syntax, lexer, get_suggestions_for_exception pygments_style = style_with_executing_node("monokai", "bg:#005080") pygments_formatter = HtmlFormatter( style=pygments_style, nowrap=True, ) log = logging.getLogger(__name__) def friendly_message(e, double_newline: bool): try: fr = FriendlyTraceback(type(e), e, e.__traceback__) fr.assign_generic() fr.assign_cause() return fr.info["generic"] + "\n" + double_newline * "\n" + fr.info.get("cause", "")