rich-struct 0.3.0__tar.gz → 0.3.2__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,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: rich-struct
3
- Version: 0.3.0
3
+ Version: 0.3.2
4
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
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
4
4
 
5
5
  [project]
6
6
  name = "rich-struct"
7
- version = "0.3.0"
7
+ version = "0.3.2"
8
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"
@@ -4,6 +4,7 @@ import xml.etree.ElementTree as ET
4
4
  from typing import IO, Any, Optional, Union
5
5
 
6
6
  from rich.console import Console
7
+ from rich.highlighter import ReprHighlighter
7
8
  from rich.json import JSON
8
9
  from rich.markdown import Markdown
9
10
  from rich.padding import Padding
@@ -34,6 +35,13 @@ __all__ = [
34
35
 
35
36
  ThemeType = Union[ThemeName, Theme, ThemeLiteral]
36
37
 
38
+
39
+ class _ObjectHighlighter(ReprHighlighter):
40
+ """rich's repr highlighting, plus dict keys as ``repr.key`` (themes without it show them as strings)."""
41
+
42
+ # ponytail: any quoted string followed by ": " counts as a key; a real parser if that misfires
43
+ highlights = [*ReprHighlighter.highlights, r"""(?P<key>(?<!\w)(?:'[^'\n]*'|"[^"\n]*"))(?=: )"""]
44
+
37
45
  _default_theme: ThemeType = DEFAULT_THEME
38
46
 
39
47
 
@@ -149,7 +157,7 @@ def print_obj(obj: Any, *, theme: Optional[ThemeType] = None, **kwargs: Any) ->
149
157
  """Pretty-print any Python object (dataclasses, dicts, nested structures...)."""
150
158
  if obj is None:
151
159
  return print(None, theme=theme, **kwargs)
152
- print(Pretty(obj, expand_all=True), theme=theme, **kwargs)
160
+ print(Pretty(obj, expand_all=True, highlighter=_ObjectHighlighter()), theme=theme, **kwargs)
153
161
 
154
162
 
155
163
  def print_table(
@@ -133,25 +133,26 @@ _OVERRIDES = {
133
133
  "markdown.h4": "bold #a5b4fc",
134
134
  "markdown.h5": "#a5b4fc",
135
135
  "markdown.h6": "dim #a5b4fc",
136
- "markdown.strong": "bold #f8fafc",
136
+ "markdown.strong": "bold #ffffff",
137
137
  "markdown.em": "italic #c4b5fd",
138
138
  "markdown.emph": "italic #c4b5fd",
139
139
  "markdown.s": "strike #64748b",
140
140
  "markdown.code": "#fbbf24 on #111c2e",
141
141
  "markdown.block_quote": "italic #86efac",
142
- "markdown.item.bullet": "bold #fbbf24",
143
- "markdown.item.number": "bold #fbbf24",
142
+ "markdown.item.bullet": "bold #86efac",
143
+ "markdown.item.number": "bold #86efac",
144
144
  "markdown.link": "underline #38bdf8",
145
145
  "markdown.link_url": "underline #38bdf8", # rich styles link text with link_url
146
146
  "markdown.hr": "#475569",
147
147
  "json.brace": "#94a3b8",
148
- "json.key": "#7dd3fc",
148
+ "json.key": "#a3c2f2",
149
149
  "json.bool_true": "bold #22d3ee",
150
150
  "json.bool_false": "bold #22d3ee",
151
151
  "json.null": "italic #a78bfa",
152
152
  # objects (print_obj)
153
153
  "repr.background": "on #0f172a",
154
- "repr.attrib_name": "#cbd5e1",
154
+ "repr.attrib_name": "#a3c2f2",
155
+ "repr.key": "#a3c2f2",
155
156
  "repr.call": "bold #22d3ee",
156
157
  "repr.str": "#a3e635",
157
158
  "repr.number": "#fbbf24",
@@ -187,13 +188,14 @@ _OVERRIDES = {
187
188
  "markdown.link_url": "underline #2563eb", # rich styles link text with link_url
188
189
  "markdown.hr": "#cbd5e1",
189
190
  "json.brace": "#64748b",
190
- "json.key": "#0369a1",
191
+ "json.key": "#1e3f7a",
191
192
  "json.bool_true": "bold #0e7490",
192
193
  "json.bool_false": "bold #0e7490",
193
194
  "json.null": "italic #7c3aed",
194
195
  # objects (print_obj)
195
196
  "repr.background": "on #ffffff",
196
- "repr.attrib_name": "#1f2937",
197
+ "repr.attrib_name": "#1e3f7a",
198
+ "repr.key": "#1e3f7a",
197
199
  "repr.call": "bold #2563eb",
198
200
  "repr.str": "#16853a",
199
201
  "repr.number": "#b45309",
@@ -28,7 +28,7 @@ def test_theme_colours_reach_the_output(monkeypatch):
28
28
  assert keyword in render(rt.print_syntax, "def f(): pass", "python")
29
29
  assert keyword in render(rt.print_md, "```python\ndef f(): pass\n```")
30
30
  assert keyword not in render(rt.print_syntax, "def f(): pass", "python", theme="Nord")
31
- assert "38;2;125;211;252" in render(rt.print_json, {"a": 1}) # key, sky #7dd3fc
31
+ assert "38;2;163;194;242" in render(rt.print_json, {"a": 1}) # key, #a3c2f2
32
32
 
33
33
 
34
34
  @dataclass
@@ -126,9 +126,14 @@ def test_objects_get_their_own_colours_and_background_in_midnight_slate(monkeypa
126
126
  page_bg, object_bg = "48;2;11;19;32", "48;2;15;23;42" # #0b1320, #0f172a
127
127
  out = render(rt.print_obj, Point(1, "y"))
128
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
129
+ assert "38;2;163;194;242" in out and "38;2;34;211;238" in out # property #a3c2f2, class #22d3ee
130
130
  assert object_bg not in render(rt.print_md, "text") and object_bg not in render(rt.print_obj, 1, background=False)
131
131
  assert "48;2;46;52;64" in render(rt.print_obj, 1, theme="Nord") # other themes: the page background
132
+ # dict keys take the property colour here, and stay string-coloured in themes without repr.key
133
+ prop, slate_str, nord_str = "38;2;163;194;242", "38;2;163;230;53", "38;2;163;190;140"
134
+ out = render(rt.print_obj, {"key": "value"})
135
+ assert f"{prop};48;2;15;23;42m'key'" in out and f"{slate_str};48;2;15;23;42m'value'" in out
136
+ assert f"{nord_str};48;2;46;52;64m'key'" in render(rt.print_obj, {"key": "value"}, theme="Nord")
132
137
 
133
138
 
134
139
  def test_plain_print_matches_builtin_semantics():
File without changes
File without changes
File without changes