CoolFace
Apppublic

Aluode/PerceptionLabPortable

sourceHugging Faceupdated 9mo agoView on Hugging Face
0likes
protocol.py43 linesDownload Raw Back to rich
1from typing import Any, cast, Set, TYPE_CHECKING2from inspect import isclass3 4if TYPE_CHECKING:5    from pip._vendor.rich.console import RenderableType6 7_GIBBERISH = """aihwerij235234ljsdnp34ksodfipwoe234234jlskjdf"""8 9 10def is_renderable(check_object: Any) -> bool:11    """Check if an object may be rendered by Rich."""12    return (13        isinstance(check_object, str)14        or hasattr(check_object, "__rich__")15        or hasattr(check_object, "__rich_console__")16    )17 18 19def rich_cast(renderable: object) -> "RenderableType":20    """Cast an object to a renderable by calling __rich__ if present.21 22    Args:23        renderable (object): A potentially renderable object24 25    Returns:26        object: The result of recursively calling __rich__.27    """28    from pip._vendor.rich.console import RenderableType29 30    rich_visited_set: Set[type] = set()  # Prevent potential infinite loop31    while hasattr(renderable, "__rich__") and not isclass(renderable):32        # Detect object which claim to have all the attributes33        if hasattr(renderable, _GIBBERISH):34            return repr(renderable)35        cast_method = getattr(renderable, "__rich__")36        renderable = cast_method()37        renderable_type = type(renderable)38        if renderable_type in rich_visited_set:39            break40        rich_visited_set.add(renderable_type)41 42    return cast(RenderableType, renderable)43 
Aluode/PerceptionLabPortable · CoolFace