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