rich-struct 0.2.0__tar.gz → 0.3.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,7 +1,7 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: rich-struct
3
- Version: 0.2.0
4
- Summary: rich.print with 25 themes (Midnight Slate, Tokyo Night, Dracula, Nord, ...) and print_md/json/yaml/xml/object/table helpers
3
+ Version: 0.3.0
4
+ Summary: rich.print with 25 themes (Midnight Slate, Tokyo Night, Dracula, Nord, ...) and print_md/json/yaml/xml/obj/table helpers
5
5
  Project-URL: Homepage, https://github.com/wiertmir/rich-struct
6
6
  Project-URL: Issues, https://github.com/wiertmir/rich-struct/issues
7
7
  Author: Mirek Wiertel
@@ -28,7 +28,7 @@ pip install rich-struct # add [yaml] to dump objects with print_yaml
28
28
  ```python
29
29
  from rich.markdown import Markdown
30
30
  from rich.theme import Theme
31
- from rich_struct import ThemeName, print, print_md, print_json, print_yaml, print_xml, print_object, print_syntax, print_table
31
+ from rich_struct import ThemeName, print, print_md, print_json, print_yaml, print_xml, print_obj, print_syntax, print_table
32
32
 
33
33
  print("[bold]hello[/bold]", {"a": 1}) # drop-in rich.print, Midnight Slate by default
34
34
  print(Markdown("## Printing structs"), theme=ThemeName.DRACULA)
@@ -37,7 +37,7 @@ print_md("# Title\n\n```python\nprint('hi')\n```")
37
37
  print_json({"a": [1, 2, None]}, theme="Nord") # enum value as a string works too
38
38
  print_yaml("a: 1\nb: [x, y]")
39
39
  print_xml("<a><b c='d'/></a>")
40
- print_object(my_dataclass, theme=ThemeName.GITHUB_LIGHT)
40
+ print_obj(my_dataclass, theme=ThemeName.GITHUB_LIGHT)
41
41
  print_syntax("SELECT 1", "sql") # any pygments lexer
42
42
  print_table(df, title="Sales") # DataFrame, Series, numpy array, list...
43
43
  print(df) # arrays and frames become tables in print too
@@ -70,7 +70,7 @@ One per kind of struct, each in a different theme. The [demo notebook](https://g
70
70
 
71
71
  ![XML in the GitHub Dark theme](https://raw.githubusercontent.com/wiertmir/rich-struct/main/screenshots/xml.png)
72
72
 
73
- **Python objects** — `print_object`, Catppuccin Mocha
73
+ **Python objects** — `print_obj`, Catppuccin Mocha
74
74
 
75
75
  ![Python objects in the Catppuccin Mocha theme](https://raw.githubusercontent.com/wiertmir/rich-struct/main/screenshots/object.png)
76
76
 
@@ -127,4 +127,4 @@ Themes are built from each theme's colour palette, not ported token-by-token fro
127
127
 
128
128
  Output is painted on the theme's background across the full terminal width. Pass `background=False` to keep the terminal's own background; nothing is painted or padded when the output has no colour (e.g. piped to a file). A custom `Theme` gets a background by defining a `background` style, e.g. `"background": "#c0caf5 on #1a1b26"`.
129
129
 
130
- Code colours come from `syntax.*` styles (`text`, `comment`, `keyword`, `string`, `number`, `function`, `type`, `tag`, `attribute`, `operator`); a custom `Theme` may override any of them, and may set `syntax.block` (e.g. `"on #111c2e"`) to shade code blocks inside Markdown.
130
+ Code colours come from `syntax.*` styles (`text`, `comment`, `keyword`, `string`, `number`, `function`, `type`, `tag`, `attribute`, `operator`); a custom `Theme` may override any of them, and may set `syntax.block` (e.g. `"on #111c2e"`) to shade code blocks inside Markdown, and `repr.background` to give `print_obj` output its own background.
@@ -9,7 +9,7 @@ pip install rich-struct # add [yaml] to dump objects with print_yaml
9
9
  ```python
10
10
  from rich.markdown import Markdown
11
11
  from rich.theme import Theme
12
- from rich_struct import ThemeName, print, print_md, print_json, print_yaml, print_xml, print_object, print_syntax, print_table
12
+ from rich_struct import ThemeName, print, print_md, print_json, print_yaml, print_xml, print_obj, print_syntax, print_table
13
13
 
14
14
  print("[bold]hello[/bold]", {"a": 1}) # drop-in rich.print, Midnight Slate by default
15
15
  print(Markdown("## Printing structs"), theme=ThemeName.DRACULA)
@@ -18,7 +18,7 @@ print_md("# Title\n\n```python\nprint('hi')\n```")
18
18
  print_json({"a": [1, 2, None]}, theme="Nord") # enum value as a string works too
19
19
  print_yaml("a: 1\nb: [x, y]")
20
20
  print_xml("<a><b c='d'/></a>")
21
- print_object(my_dataclass, theme=ThemeName.GITHUB_LIGHT)
21
+ print_obj(my_dataclass, theme=ThemeName.GITHUB_LIGHT)
22
22
  print_syntax("SELECT 1", "sql") # any pygments lexer
23
23
  print_table(df, title="Sales") # DataFrame, Series, numpy array, list...
24
24
  print(df) # arrays and frames become tables in print too
@@ -51,7 +51,7 @@ One per kind of struct, each in a different theme. The [demo notebook](https://g
51
51
 
52
52
  ![XML in the GitHub Dark theme](https://raw.githubusercontent.com/wiertmir/rich-struct/main/screenshots/xml.png)
53
53
 
54
- **Python objects** — `print_object`, Catppuccin Mocha
54
+ **Python objects** — `print_obj`, Catppuccin Mocha
55
55
 
56
56
  ![Python objects in the Catppuccin Mocha theme](https://raw.githubusercontent.com/wiertmir/rich-struct/main/screenshots/object.png)
57
57
 
@@ -108,4 +108,4 @@ Themes are built from each theme's colour palette, not ported token-by-token fro
108
108
 
109
109
  Output is painted on the theme's background across the full terminal width. Pass `background=False` to keep the terminal's own background; nothing is painted or padded when the output has no colour (e.g. piped to a file). A custom `Theme` gets a background by defining a `background` style, e.g. `"background": "#c0caf5 on #1a1b26"`.
110
110
 
111
- Code colours come from `syntax.*` styles (`text`, `comment`, `keyword`, `string`, `number`, `function`, `type`, `tag`, `attribute`, `operator`); a custom `Theme` may override any of them, and may set `syntax.block` (e.g. `"on #111c2e"`) to shade code blocks inside Markdown.
111
+ Code colours come from `syntax.*` styles (`text`, `comment`, `keyword`, `string`, `number`, `function`, `type`, `tag`, `attribute`, `operator`); a custom `Theme` may override any of them, and may set `syntax.block` (e.g. `"on #111c2e"`) to shade code blocks inside Markdown, and `repr.background` to give `print_obj` output its own background.
@@ -4,8 +4,8 @@ build-backend = "hatchling.build"
4
4
 
5
5
  [project]
6
6
  name = "rich-struct"
7
- version = "0.2.0"
8
- description = "rich.print with 25 themes (Midnight Slate, Tokyo Night, Dracula, Nord, ...) and print_md/json/yaml/xml/object/table helpers"
7
+ version = "0.3.0"
8
+ description = "rich.print with 25 themes (Midnight Slate, Tokyo Night, Dracula, Nord, ...) and print_md/json/yaml/xml/obj/table helpers"
9
9
  readme = "README.md"
10
10
  requires-python = ">=3.9"
11
11
  license = "MIT"
@@ -23,7 +23,7 @@ __all__ = [
23
23
  "print",
24
24
  "print_json",
25
25
  "print_md",
26
- "print_object",
26
+ "print_obj",
27
27
  "print_syntax",
28
28
  "print_table",
29
29
  "print_xml",
@@ -84,6 +84,9 @@ def print(
84
84
  console = Console(theme=rich_theme, file=file)
85
85
  base = rich_theme.styles.get("background")
86
86
  if background and base and console.color_system:
87
+ if objects and all(isinstance(obj, Pretty) for obj in objects):
88
+ # a theme may give objects their own background
89
+ base += rich_theme.styles.get("repr.background", base)
87
90
  # fill whole lines: Padding expands renderables, justify pads plain text
88
91
  objects = tuple(
89
92
  Padding(obj, 0) if is_renderable(obj) and not isinstance(obj, str) else obj
@@ -142,7 +145,7 @@ def print_xml(
142
145
  print_syntax(data.rstrip("\n"), "xml", theme=theme, **kwargs)
143
146
 
144
147
 
145
- def print_object(obj: Any, *, theme: Optional[ThemeType] = None, **kwargs: Any) -> None:
148
+ def print_obj(obj: Any, *, theme: Optional[ThemeType] = None, **kwargs: Any) -> None:
146
149
  """Pretty-print any Python object (dataclasses, dicts, nested structures...)."""
147
150
  if obj is None:
148
151
  return print(None, theme=theme, **kwargs)
@@ -149,6 +149,22 @@ _OVERRIDES = {
149
149
  "json.bool_true": "bold #22d3ee",
150
150
  "json.bool_false": "bold #22d3ee",
151
151
  "json.null": "italic #a78bfa",
152
+ # objects (print_obj)
153
+ "repr.background": "on #0f172a",
154
+ "repr.attrib_name": "#cbd5e1",
155
+ "repr.call": "bold #22d3ee",
156
+ "repr.str": "#a3e635",
157
+ "repr.number": "#fbbf24",
158
+ "repr.number_complex": "#fbbf24",
159
+ "repr.none": "italic #c4b5fd",
160
+ "repr.bool_true": "italic #86efac",
161
+ "repr.bool_false": "italic #86efac",
162
+ "repr.tag_name": "#86efac", # enum members print as <Role.ADMIN: 1>
163
+ "repr.attrib_equal": "#f8fafc",
164
+ "repr.brace": "#94a3b8",
165
+ "repr.comma": "#94a3b8",
166
+ "repr.tag_start": "#94a3b8",
167
+ "repr.tag_end": "#94a3b8",
152
168
  },
153
169
  T.PAPER_FROST: {
154
170
  "syntax.tag": "#0369a1",
@@ -175,6 +191,22 @@ _OVERRIDES = {
175
191
  "json.bool_true": "bold #0e7490",
176
192
  "json.bool_false": "bold #0e7490",
177
193
  "json.null": "italic #7c3aed",
194
+ # objects (print_obj)
195
+ "repr.background": "on #ffffff",
196
+ "repr.attrib_name": "#1f2937",
197
+ "repr.call": "bold #2563eb",
198
+ "repr.str": "#16853a",
199
+ "repr.number": "#b45309",
200
+ "repr.number_complex": "#b45309",
201
+ "repr.none": "italic #7c3aed",
202
+ "repr.bool_true": "italic #059669",
203
+ "repr.bool_false": "italic #059669",
204
+ "repr.tag_name": "#059669", # enum members print as <Role.ADMIN: 1>
205
+ "repr.attrib_equal": "#111111",
206
+ "repr.brace": "#475569",
207
+ "repr.comma": "#475569",
208
+ "repr.tag_start": "#475569",
209
+ "repr.tag_end": "#475569",
178
210
  },
179
211
  }
180
212
  del T
@@ -61,12 +61,12 @@ def test_every_printer_with_every_theme_kind(theme):
61
61
  assert "a: 1" in render(rt.print_yaml, "a: 1\n", theme=theme)
62
62
  assert "<a>" in render(rt.print_xml, "<a><b/></a>", theme=theme)
63
63
  assert "<b />" in render(rt.print_xml, ET.fromstring("<a><b/></a>"), theme=theme)
64
- assert "Point(" in render(rt.print_object, Point(1, "y"), theme=theme)
64
+ assert "Point(" in render(rt.print_obj, Point(1, "y"), theme=theme)
65
65
  assert "def" in render(rt.print_syntax, "def f(): pass", "python", theme=theme)
66
66
 
67
67
 
68
68
  def test_every_printer_accepts_none(monkeypatch):
69
- helpers = [rt.print, rt.print_md, rt.print_yaml, rt.print_xml, rt.print_object, rt.print_table]
69
+ helpers = [rt.print, rt.print_md, rt.print_yaml, rt.print_xml, rt.print_obj, rt.print_table]
70
70
  for fn in helpers:
71
71
  assert render(fn, None) == "None\n", fn.__name__
72
72
  assert render(rt.print_syntax, None, "python") == "None\n"
@@ -74,15 +74,16 @@ def test_every_printer_accepts_none(monkeypatch):
74
74
  assert render(rt.print_json, None) == "null\n"
75
75
  monkeypatch.setenv("FORCE_COLOR", "1")
76
76
  monkeypatch.setenv("COLORTERM", "truecolor")
77
- for fn in [*helpers, rt.print_json]:
78
- assert "3;38;2;167;139;250" in render(fn, None), fn.__name__ # italic Midnight Slate violet
77
+ for fn in helpers:
78
+ assert "3;38;2;196;181;253" in render(fn, None), fn.__name__ # italic #c4b5fd
79
+ assert "3;38;2;167;139;250" in render(rt.print_json, None) # JSON null, italic #a78bfa
79
80
 
80
81
 
81
82
  def test_set_default_theme_changes_every_printer(monkeypatch):
82
83
  monkeypatch.setenv("FORCE_COLOR", "1")
83
84
  monkeypatch.setenv("COLORTERM", "truecolor")
84
85
  default_bg, nord_bg, dracula_bg = "48;2;11;19;32", "48;2;46;52;64", "48;2;40;42;54"
85
- printers = [rt.print, rt.print_md, rt.print_json, rt.print_yaml, rt.print_xml, rt.print_object, rt.print_table]
86
+ printers = [rt.print, rt.print_md, rt.print_json, rt.print_yaml, rt.print_xml, rt.print_obj, rt.print_table]
86
87
  try:
87
88
  rt.set_default_theme("Nord")
88
89
  for fn in printers:
@@ -119,6 +120,17 @@ def test_theme_specific_styles_and_markdown_code_block_background(monkeypatch):
119
120
  assert "48;2;1;2;3" in render(rt.print_md, "```python\nx = 1\n```", theme=mine)
120
121
 
121
122
 
123
+ def test_objects_get_their_own_colours_and_background_in_midnight_slate(monkeypatch):
124
+ monkeypatch.setenv("FORCE_COLOR", "1")
125
+ monkeypatch.setenv("COLORTERM", "truecolor")
126
+ page_bg, object_bg = "48;2;11;19;32", "48;2;15;23;42" # #0b1320, #0f172a
127
+ out = render(rt.print_obj, Point(1, "y"))
128
+ assert object_bg in out and page_bg not in out
129
+ assert "38;2;203;213;225" in out and "38;2;34;211;238" in out # property #cbd5e1, class #22d3ee
130
+ assert object_bg not in render(rt.print_md, "text") and object_bg not in render(rt.print_obj, 1, background=False)
131
+ assert "48;2;46;52;64" in render(rt.print_obj, 1, theme="Nord") # other themes: the page background
132
+
133
+
122
134
  def test_plain_print_matches_builtin_semantics():
123
135
  assert render(rt.print, "a", "b", sep="-", end="!") == "a-b!"
124
136
 
File without changes
File without changes