Class for safely making an HTML representation of a Python object.
import re
from reprlib import Repr
from typing import Any, cast
from pydoc_fork import inline_styles
from pydoc_fork.string_utils import cram, replace, stripid
Class for safely making an HTML representation of a Python object.
class HTMLRepr(Repr):
Some maximums
def __init__(self) -> None:
Repr.__init__(self)
self.maxlist = self.maxtuple = 20
self.maxdict = 10
self.maxstring = self.maxother = 100
pylint: disable=no-self-use Simple html escaping
def escape(self, text: str) -> str:
result = replace(text, "&", "&", "<", "<", ">", ">")
if “&” in result: print(“possible double escape”)
return result
Delegates to Repr.repr
def repr(self, x: Any) -> str: # noqa - unhiding could break code?
return Repr.repr(self, x)
Not sure, is dead code?
def repr1(self, x: Any, level: int) -> str:
if hasattr(type(x), "__name__"):
methodname = "repr_" + "_".join(type(x).__name__.split())
if hasattr(self, methodname):
return cast(str, getattr(self, methodname)(x, level))
return self.escape(cram(stripid(repr(x)), self.maxother))
Repr, but squash it into a window
def repr_string(self, x: str, _: int) -> str:
test = cram(x, self.maxstring)
test_repr = repr(test)
if "\\" in test and "\\" not in replace(test_repr, r"\\", ""):
Backslashes are only literal in the string and are never needed to make any special characters, so show a raw string.
return "r" + test_repr[0] + self.escape(test) + test_repr[0]
return re.sub(
r'((\\[\\abfnrtv\'"]|\\[0-9]..|\\x..|\\u....)+)',
f'<span style="color:{inline_styles.REPR_COLOR}">' + r"\1" + "</span>",
self.escape(test_repr),
)
repr_str = repr_string
Repr, but squash it into a window
def repr_instance(self, x: Any, level: int) -> str:
try:
return self.escape(cram(stripid(repr(x)), self.maxstring))
pylint: disable=broad-except
except BaseException:
return self.escape(f"<{x.__class__.__name__} instance>")
repr_unicode = repr_string