codekingpro/portable-devtools
114k
1from functools import partial2from typing import (3 Any,4 Callable,5 Iterable,6 List,7 Optional,8 Tuple,9 Type,10 TypeVar,11 Union,12 overload,13)14 15T = TypeVar("T")16 17 18Result = Iterable[Union[Any, Tuple[Any], Tuple[str, Any], Tuple[str, Any, Any]]]19RichReprResult = Result20 21 22class ReprError(Exception):23 """An error occurred when attempting to build a repr."""24 25 26@overload27def auto(cls: Optional[Type[T]]) -> Type[T]:28 ...29 30 31@overload32def auto(*, angular: bool = False) -> Callable[[Type[T]], Type[T]]:33 ...34 35 36def auto(37 cls: Optional[Type[T]] = None, *, angular: Optional[bool] = None38) -> Union[Type[T], Callable[[Type[T]], Type[T]]]:39 """Class decorator to create __repr__ from __rich_repr__"""40 41 def do_replace(cls: Type[T], angular: Optional[bool] = None) -> Type[T]:42 def auto_repr(self: T) -> str:43 """Create repr string from __rich_repr__"""44 repr_str: List[str] = []45 append = repr_str.append46 47 angular: bool = getattr(self.__rich_repr__, "angular", False) # type: ignore[attr-defined]48 for arg in self.__rich_repr__(): # type: ignore[attr-defined]49 if isinstance(arg, tuple):50 if len(arg) == 1:51 append(repr(arg[0]))52 else:53 key, value, *default = arg54 if key is None:55 append(repr(value))56 else:57 if default and default[0] == value:58 continue59 append(f"{key}={value!r}")60 else:61 append(repr(arg))62 if angular:63 return f"<{self.__class__.__name__} {' '.join(repr_str)}>"64 else:65 return f"{self.__class__.__name__}({', '.join(repr_str)})"66 67 def auto_rich_repr(self: Type[T]) -> Result:68 """Auto generate __rich_rep__ from signature of __init__"""69 try:70 import inspect71 72 signature = inspect.signature(self.__init__)73 for name, param in signature.parameters.items():74 if param.kind == param.POSITIONAL_ONLY:75 yield getattr(self, name)76 elif param.kind in (77 param.POSITIONAL_OR_KEYWORD,78 param.KEYWORD_ONLY,79 ):80 if param.default is param.empty:81 yield getattr(self, param.name)82 else:83 yield param.name, getattr(self, param.name), param.default84 except Exception as error:85 raise ReprError(86 f"Failed to auto generate __rich_repr__; {error}"87 ) from None88 89 if not hasattr(cls, "__rich_repr__"):90 auto_rich_repr.__doc__ = "Build a rich repr"91 cls.__rich_repr__ = auto_rich_repr # type: ignore[attr-defined]92 93 auto_repr.__doc__ = "Return repr(self)"94 cls.__repr__ = auto_repr # type: ignore[assignment]95 if angular is not None:96 cls.__rich_repr__.angular = angular # type: ignore[attr-defined]97 return cls98 99 if cls is None:100 return partial(do_replace, angular=angular)101 else:102 return do_replace(cls, angular=angular)103 104 105@overload106def rich_repr(cls: Optional[Type[T]]) -> Type[T]:107 ...108 109 110@overload111def rich_repr(*, angular: bool = False) -> Callable[[Type[T]], Type[T]]:112 ...113 114 115def rich_repr(116 cls: Optional[Type[T]] = None, *, angular: bool = False117) -> Union[Type[T], Callable[[Type[T]], Type[T]]]:118 if cls is None:119 return auto(angular=angular)120 else:121 return auto(cls)122 123 124if __name__ == "__main__":125 126 @auto127 class Foo:128 def __rich_repr__(self) -> Result:129 yield "foo"130 yield "bar", {"shopping": ["eggs", "ham", "pineapple"]}131 yield "buy", "hand sanitizer"132 133 foo = Foo()134 from rich.console import Console135 136 console = Console()137 138 console.rule("Standard repr")139 console.print(foo)140 141 console.print(foo, width=60)142 console.print(foo, width=30)143 144 console.rule("Angular repr")145 Foo.__rich_repr__.angular = True # type: ignore[attr-defined]146 147 console.print(foo)148 149 console.print(foo, width=60)150 console.print(foo, width=30)151 