scope.py 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. from collections.abc import Mapping
  2. from typing import TYPE_CHECKING, Any, Optional, Tuple
  3. from .highlighter import ReprHighlighter
  4. from .panel import Panel
  5. from .pretty import Pretty
  6. from .table import Table
  7. from .text import Text, TextType
  8. if TYPE_CHECKING:
  9. from .console import ConsoleRenderable, OverflowMethod
  10. def render_scope(
  11. scope: "Mapping[str, Any]",
  12. *,
  13. title: Optional[TextType] = None,
  14. sort_keys: bool = True,
  15. indent_guides: bool = False,
  16. max_length: Optional[int] = None,
  17. max_string: Optional[int] = None,
  18. max_depth: Optional[int] = None,
  19. overflow: Optional["OverflowMethod"] = None,
  20. ) -> "ConsoleRenderable":
  21. """Render python variables in a given scope.
  22. Args:
  23. scope (Mapping): A mapping containing variable names and values.
  24. title (str, optional): Optional title. Defaults to None.
  25. sort_keys (bool, optional): Enable sorting of items. Defaults to True.
  26. indent_guides (bool, optional): Enable indentation guides. Defaults to False.
  27. max_length (int, optional): Maximum length of containers before abbreviating, or None for no abbreviation.
  28. Defaults to None.
  29. max_string (int, optional): Maximum length of string before truncating, or None to disable. Defaults to None.
  30. max_depth (int, optional): Maximum depths of locals before truncating, or None to disable. Defaults to None.
  31. overflow (OverflowMethod, optional): How to handle overflowing locals, or None to disable. Defaults to None.
  32. Returns:
  33. ConsoleRenderable: A renderable object.
  34. """
  35. highlighter = ReprHighlighter()
  36. items_table = Table.grid(padding=(0, 1), expand=False)
  37. items_table.add_column(justify="right")
  38. def sort_items(item: Tuple[str, Any]) -> Tuple[bool, str]:
  39. """Sort special variables first, then alphabetically."""
  40. key, _ = item
  41. return (not key.startswith("__"), key.lower())
  42. items = sorted(scope.items(), key=sort_items) if sort_keys else scope.items()
  43. for key, value in items:
  44. key_text = Text.assemble(
  45. (key, "scope.key.special" if key.startswith("__") else "scope.key"),
  46. (" =", "scope.equals"),
  47. )
  48. items_table.add_row(
  49. key_text,
  50. Pretty(
  51. value,
  52. highlighter=highlighter,
  53. indent_guides=indent_guides,
  54. max_length=max_length,
  55. max_string=max_string,
  56. max_depth=max_depth,
  57. overflow=overflow,
  58. ),
  59. )
  60. return Panel.fit(
  61. items_table,
  62. title=title,
  63. border_style="scope.border",
  64. padding=(0, 1),
  65. )
  66. if __name__ == "__main__": # pragma: no cover
  67. from rich import print
  68. print()
  69. def test(foo: float, bar: float) -> None:
  70. list_of_things = [1, 2, 3, None, 4, True, False, "Hello World"]
  71. dict_of_things = {
  72. "version": "1.1",
  73. "method": "confirmFruitPurchase",
  74. "params": [["apple", "orange", "mangoes", "pomelo"], 1.123],
  75. "id": "194521489",
  76. }
  77. print(render_scope(locals(), title="[i]locals", sort_keys=False))
  78. test(20.3423, 3.1427)
  79. print()