diff options
Diffstat (limited to 'libs/rich/__init__.py')
-rw-r--r-- | libs/rich/__init__.py | 68 |
1 files changed, 60 insertions, 8 deletions
diff --git a/libs/rich/__init__.py b/libs/rich/__init__.py index b0e4c8d94..ed11f5d7e 100644 --- a/libs/rich/__init__.py +++ b/libs/rich/__init__.py @@ -1,7 +1,9 @@ """Rich text and beautiful formatting in the terminal.""" import os -from typing import Any, IO, Optional, TYPE_CHECKING +from typing import Callable, IO, TYPE_CHECKING, Any, Optional + +from ._extension import load_ipython_extension __all__ = ["get_console", "reconfigure", "print", "inspect"] @@ -30,8 +32,8 @@ def get_console() -> "Console": return _console -def reconfigure(*args, **kwargs) -> None: - """Reconfigures the global console bu replacing it with another. +def reconfigure(*args: Any, **kwargs: Any) -> None: + """Reconfigures the global console by replacing it with another. Args: console (Console): Replacement console instance. @@ -39,10 +41,17 @@ def reconfigure(*args, **kwargs) -> None: from rich.console import Console new_console = Console(*args, **kwargs) + _console = get_console() _console.__dict__ = new_console.__dict__ -def print(*objects: Any, sep=" ", end="\n", file: IO[str] = None, flush: bool = False): +def print( + *objects: Any, + sep: str = " ", + end: str = "\n", + file: Optional[IO[str]] = None, + flush: bool = False, +) -> None: r"""Print object(s) supplied via positional arguments. This function has an identical signature to the built-in print. For more advanced features, see the :class:`~rich.console.Console` class. @@ -60,11 +69,54 @@ def print(*objects: Any, sep=" ", end="\n", file: IO[str] = None, flush: bool = return write_console.print(*objects, sep=sep, end=end) +def print_json( + json: Optional[str] = None, + *, + data: Any = None, + indent: int = 2, + highlight: bool = True, + skip_keys: bool = False, + ensure_ascii: bool = True, + check_circular: bool = True, + allow_nan: bool = True, + default: Optional[Callable[[Any], Any]] = None, + sort_keys: bool = False, +) -> None: + """Pretty prints JSON. Output will be valid JSON. + + Args: + json (str): A string containing JSON. + data (Any): If json is not supplied, then encode this data. + indent (int, optional): Number of spaces to indent. Defaults to 2. + highlight (bool, optional): Enable highlighting of output: Defaults to True. + skip_keys (bool, optional): Skip keys not of a basic type. Defaults to False. + ensure_ascii (bool, optional): Escape all non-ascii characters. Defaults to False. + check_circular (bool, optional): Check for circular references. Defaults to True. + allow_nan (bool, optional): Allow NaN and Infinity values. Defaults to True. + default (Callable, optional): A callable that converts values that can not be encoded + in to something that can be JSON encoded. Defaults to None. + sort_keys (bool, optional): Sort dictionary keys. Defaults to False. + """ + + get_console().print_json( + json, + data=data, + indent=indent, + highlight=highlight, + skip_keys=skip_keys, + ensure_ascii=ensure_ascii, + check_circular=check_circular, + allow_nan=allow_nan, + default=default, + sort_keys=sort_keys, + ) + + def inspect( obj: Any, *, - console: "Console" = None, - title: str = None, + console: Optional["Console"] = None, + title: Optional[str] = None, help: bool = False, methods: bool = False, docs: bool = True, @@ -72,8 +124,8 @@ def inspect( dunder: bool = False, sort: bool = True, all: bool = False, - value: bool = True -): + value: bool = True, +) -> None: """Inspect any Python object. * inspect(<OBJECT>) to see summarized info. |