Coverage for /run/media/veganeco/water/status600.com/pypi/reptilian_climates/modules_series_2/ships/fields/gardens_pip/rich/scope.py: 43%

1 from collections.abc import Mapping

2 from typing import TYPE_CHECKING, Any, Optional, Tuple

3

4 from .highlighter import ReprHighlighter

5 from .panel import Panel

6 from .pretty import Pretty

7 from .table import Table

8 from .text import Text, TextType

9

10 if TYPE_CHECKING:

11 from .console import ConsoleRenderable

12

13

14 def 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.

24

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.

33

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")

40

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())

45

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 )

68

69

70 if __name__ == "__main__": # pragma: no cover

71 from rich import print

72

73 print()

74

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))

84

85 test(20.3423, 3.1427)

86 print()