Format an object in YAML-style for clean logging. Args: obj: The object to format (dict, list, or primitive) indent: Current indentation level colorize: Whether to apply colors Returns: Formatted string representation
(obj: Any, indent: int = 0, colorize: bool = True)
| 420 | |
| 421 | |
| 422 | def format_object(obj: Any, indent: int = 0, colorize: bool = True) -> str: |
| 423 | """ |
| 424 | Format an object in YAML-style for clean logging. |
| 425 | |
| 426 | Args: |
| 427 | obj: The object to format (dict, list, or primitive) |
| 428 | indent: Current indentation level |
| 429 | colorize: Whether to apply colors |
| 430 | |
| 431 | Returns: |
| 432 | Formatted string representation |
| 433 | """ |
| 434 | lines: List[str] = [] |
| 435 | prefix = " " * indent |
| 436 | |
| 437 | if isinstance(obj, dict): |
| 438 | for key, value in obj.items(): # pyright: ignore[reportUnknownVariableType] |
| 439 | key_str = ( |
| 440 | f"{Colors.KEY}{key}{Colors.RESET}" if colorize else str(key) # pyright: ignore[reportUnknownArgumentType] |
| 441 | ) |
| 442 | if isinstance(value, (dict, list)) and value: |
| 443 | lines.append(f"{prefix}{key_str}:") |
| 444 | lines.append(format_object(value, indent + 1, colorize)) |
| 445 | else: |
| 446 | formatted_value = _format_value(value, colorize) |
| 447 | lines.append(f"{prefix}{key_str}: {formatted_value}") |
| 448 | elif isinstance(obj, list): |
| 449 | for item in obj: # pyright: ignore[reportUnknownVariableType] |
| 450 | if isinstance(item, (dict, list)) and item: |
| 451 | lines.append(f"{prefix}-") |
| 452 | lines.append(format_object(item, indent + 1, colorize)) |
| 453 | else: |
| 454 | formatted_value = _format_value(item, colorize) |
| 455 | lines.append(f"{prefix}- {formatted_value}") |
| 456 | else: |
| 457 | lines.append(f"{prefix}{_format_value(obj, colorize)}") |
| 458 | |
| 459 | return "\n".join(lines) |
| 460 | |
| 461 | |
| 462 | def _format_value(value: Any, colorize: bool = True) -> str: |