updating and refactoring codebase for improved performance and maintainability
This commit is contained in:
+30
-21
@@ -13,42 +13,49 @@ import contextlib
|
||||
import sys
|
||||
from typing import Any, Iterator, TextIO, List, Dict, Optional, Tuple, cast
|
||||
|
||||
from rich.console import Console
|
||||
from rich.panel import Panel
|
||||
from rich.text import Text
|
||||
from pathlib import Path
|
||||
from SYS.utils import expand_path
|
||||
|
||||
# Configure Rich pretty-printing to avoid truncating long strings (hashes/paths).
|
||||
# This is version-safe: older Rich versions may not support the max_* arguments.
|
||||
try:
|
||||
from rich.pretty import install as _pretty_install
|
||||
# rich imports are deferred to first Console use to avoid ~100ms startup cost.
|
||||
# They are loaded the first time any Console function is called.
|
||||
|
||||
try:
|
||||
_pretty_install(max_string=100_000, max_length=100_000)
|
||||
except TypeError:
|
||||
_pretty_install()
|
||||
except Exception:
|
||||
from SYS.logger import logger
|
||||
logger.exception("Failed to configure rich pretty-printing")
|
||||
|
||||
_STDOUT_CONSOLE = Console(file=sys.stdout)
|
||||
_STDERR_CONSOLE = Console(file=sys.stderr)
|
||||
_STDOUT_CONSOLE: Any = None
|
||||
_STDERR_CONSOLE: Any = None
|
||||
|
||||
|
||||
def stdout_console() -> Console:
|
||||
def _ensure_consoles() -> None:
|
||||
global _STDOUT_CONSOLE, _STDERR_CONSOLE
|
||||
if _STDOUT_CONSOLE is None:
|
||||
from rich.console import Console
|
||||
from rich.pretty import install as _pretty_install
|
||||
try:
|
||||
_pretty_install(max_string=100_000, max_length=100_000)
|
||||
except TypeError:
|
||||
_pretty_install()
|
||||
except Exception:
|
||||
pass
|
||||
_STDOUT_CONSOLE = Console(file=sys.stdout)
|
||||
_STDERR_CONSOLE = Console(file=sys.stderr)
|
||||
|
||||
|
||||
def stdout_console() -> Any:
|
||||
_ensure_consoles()
|
||||
return _STDOUT_CONSOLE
|
||||
|
||||
|
||||
def stderr_console() -> Console:
|
||||
def stderr_console() -> Any:
|
||||
_ensure_consoles()
|
||||
return _STDERR_CONSOLE
|
||||
|
||||
|
||||
def console_for(file: TextIO | None) -> Console:
|
||||
def console_for(file: Any) -> Any:
|
||||
if file is None or file is sys.stdout:
|
||||
_ensure_consoles()
|
||||
return _STDOUT_CONSOLE
|
||||
if file is sys.stderr:
|
||||
_ensure_consoles()
|
||||
return _STDERR_CONSOLE
|
||||
from rich.console import Console
|
||||
return Console(file=file)
|
||||
|
||||
|
||||
@@ -57,7 +64,7 @@ def rprint(renderable: Any = "", *, file: TextIO | None = None) -> None:
|
||||
|
||||
|
||||
@contextlib.contextmanager
|
||||
def capture_rich_output(*, stdout: TextIO, stderr: TextIO) -> Iterator[None]:
|
||||
def capture_rich_output(*, stdout: Any, stderr: Any) -> Iterator[None]:
|
||||
"""Temporarily redirect Rich output helpers to provided streams.
|
||||
|
||||
Note: `stdout_console()` / `stderr_console()` use global Console instances,
|
||||
@@ -65,9 +72,11 @@ def capture_rich_output(*, stdout: TextIO, stderr: TextIO) -> Iterator[None]:
|
||||
"""
|
||||
|
||||
global _STDOUT_CONSOLE, _STDERR_CONSOLE
|
||||
_ensure_consoles()
|
||||
|
||||
previous_stdout = _STDOUT_CONSOLE
|
||||
previous_stderr = _STDERR_CONSOLE
|
||||
from rich.console import Console
|
||||
try:
|
||||
_STDOUT_CONSOLE = Console(file=stdout)
|
||||
_STDERR_CONSOLE = Console(file=stderr)
|
||||
|
||||
Reference in New Issue
Block a user