Skip to content

Commit 198509e

Browse files
committed
fix: split up meta for label
Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>
1 parent 6832861 commit 198509e

File tree

4 files changed

+70
-43
lines changed

4 files changed

+70
-43
lines changed

src/uproot_browser/tree.py

Lines changed: 59 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import dataclasses
88
import functools
99
from pathlib import Path
10-
from typing import Any, Dict
10+
from typing import Any, TypedDict
1111

1212
import uproot
1313
import uproot.reading
@@ -18,13 +18,29 @@
1818

1919
console = Console()
2020

21-
__all__ = ("make_tree", "process_item", "print_tree", "UprootEntry", "console")
21+
__all__ = (
22+
"make_tree",
23+
"process_item",
24+
"print_tree",
25+
"UprootEntry",
26+
"console",
27+
"MetaDict",
28+
)
2229

2330

2431
def __dir__() -> tuple[str, ...]:
2532
return __all__
2633

2734

35+
class MetaDictRequired(TypedDict, total=True):
36+
label_text: Text
37+
label_icon: str
38+
39+
40+
class MetaDict(MetaDictRequired, total=False):
41+
guide_style: str
42+
43+
2844
@dataclasses.dataclass
2945
class UprootEntry:
3046
path: str
@@ -38,11 +54,18 @@ def is_dir(self) -> bool:
3854
return len(self.item.branches) > 0
3955
return False
4056

41-
def meta(self) -> dict[str, Any]:
57+
def meta(self) -> MetaDict:
4258
return process_item(self.item)
4359

4460
def label(self) -> Text:
45-
return process_item(self.item)["label"] # type: ignore[no-any-return]
61+
meta = self.meta()
62+
return Text.assemble(meta["label_icon"], meta["label_text"])
63+
64+
def _tree_args(self) -> dict[str, Any]:
65+
d: dict[str, Text | str] = {"label": self.label()}
66+
if "guide_style" in self.meta():
67+
d["guide_style"] = self.meta()["guide_style"]
68+
return d
4669

4770
@property
4871
def children(self) -> list[UprootEntry]:
@@ -68,7 +91,7 @@ def make_tree(node: UprootEntry, *, tree: Tree | None = None) -> Tree:
6891
Given an object, build a rich.tree.Tree output.
6992
"""
7093

71-
tree = Tree(**node.meta()) if tree is None else tree.add(**node.meta())
94+
tree = Tree(**node._tree_args()) if tree is None else tree.add(**node._tree_args())
7295

7396
for child in node.children:
7497
make_tree(child, tree=tree)
@@ -77,24 +100,23 @@ def make_tree(node: UprootEntry, *, tree: Tree | None = None) -> Tree:
77100

78101

79102
@functools.singledispatch
80-
def process_item(uproot_object: Any) -> Dict[str, Any]:
103+
def process_item(uproot_object: Any) -> MetaDict:
81104
"""
82105
Given an unknown object, return a rich.tree.Tree output. Specialize for known objects.
83106
"""
84107
name = getattr(uproot_object, "name", "<unnamed>")
85108
classname = getattr(uproot_object, "classname", uproot_object.__class__.__name__)
86-
label = Text.assemble(
87-
"❓ ",
109+
label_text = Text.assemble(
88110
(f"{name} ", "bold"),
89111
(classname, "italic"),
90112
)
91-
return {"label": label}
113+
return MetaDict(label_icon="❓ ", label_text=label_text)
92114

93115

94116
@process_item.register
95117
def _process_item_tfile(
96118
uproot_object: uproot.reading.ReadOnlyDirectory,
97-
) -> Dict[str, Any]:
119+
) -> MetaDict:
98120
"""
99121
Given an TFile, return a rich.tree.Tree output.
100122
"""
@@ -109,33 +131,34 @@ def _process_item_tfile(
109131
path_name = escape(path.name)
110132
link_text = f"file://{path}"
111133

112-
label = Text.from_markup(f":file_folder: [link {link_text}]{path_name}")
134+
label_text = Text.from_markup(f"[link {link_text}]{path_name}")
113135

114-
return {
115-
"label": label,
116-
"guide_style": "bold bright_blue",
117-
}
136+
return MetaDict(
137+
label_icon="📁 ",
138+
label_text=label_text,
139+
guide_style="bold bright_blue",
140+
)
118141

119142

120143
@process_item.register
121-
def _process_item_ttree(uproot_object: uproot.TTree) -> Dict[str, Any]:
144+
def _process_item_ttree(uproot_object: uproot.TTree) -> MetaDict:
122145
"""
123146
Given an tree, return a rich.tree.Tree output.
124147
"""
125-
label = Text.assemble(
126-
"🌴 ",
148+
label_text = Text.assemble(
127149
(f"{uproot_object.name} ", "bold"),
128150
f"({uproot_object.num_entries:g})",
129151
)
130152

131-
return {
132-
"label": label,
133-
"guide_style": "bold bright_green",
134-
}
153+
return MetaDict(
154+
label_icon="🌴 ",
155+
label_text=label_text,
156+
guide_style="bold bright_green",
157+
)
135158

136159

137160
@process_item.register
138-
def _process_item_tbranch(uproot_object: uproot.TBranch) -> Dict[str, Any]:
161+
def _process_item_tbranch(uproot_object: uproot.TBranch) -> MetaDict:
139162
"""
140163
Given an branch, return a rich.tree.Tree output.
141164
"""
@@ -148,29 +171,35 @@ def _process_item_tbranch(uproot_object: uproot.TBranch) -> Dict[str, Any]:
148171
if len(uproot_object.branches):
149172
icon = "🌿 "
150173

151-
label = Text.assemble(
152-
icon,
174+
label_text = Text.assemble(
153175
(f"{uproot_object.name} ", "bold"),
154176
(f"{uproot_object.typename}", "italic"),
155177
)
156-
return {"label": label}
178+
179+
return MetaDict(
180+
label_icon=icon,
181+
label_text=label_text,
182+
guide_style="bold bright_green",
183+
)
157184

158185

159186
@process_item.register
160-
def _process_item_th(uproot_object: uproot.behaviors.TH1.Histogram) -> Dict[str, Any]:
187+
def _process_item_th(uproot_object: uproot.behaviors.TH1.Histogram) -> MetaDict:
161188
"""
162189
Given an histogram, return a rich.tree.Tree output.
163190
"""
164191
icon = "📊 " if uproot_object.kind == "COUNT" else "📈 "
165192
sizes = " × ".join(f"{len(ax)}" for ax in uproot_object.axes)
166193

167-
label = Text.assemble(
168-
icon,
194+
label_text = Text.assemble(
169195
(f"{uproot_object.name} ", "bold"),
170196
(f"{uproot_object.classname} ", "italic"),
171197
f"({sizes})",
172198
)
173-
return {"label": label}
199+
return MetaDict(
200+
label_icon=icon,
201+
label_text=label_text,
202+
)
174203

175204

176205
# pylint: disable-next=redefined-outer-name

src/uproot_browser/tui/browser.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ Browser Widget {
2323
}
2424

2525
Tree > .tree--cursor {
26-
background: $secondary-darken;
26+
background: $secondary-darken-1;
2727
color: $text;
2828
text-style: bold;
2929
}

src/uproot_browser/tui/browser.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from textual.reactive import var
1818

1919
with contextlib.suppress(AttributeError):
20-
light_background = 0xDF, 0xDF, 0xDF # $surface-darken-1
20+
light_background = 0xEF, 0xEF, 0xEF # $surface-darken-1
2121
# pylint: disable-next=protected-access
2222
plt._dict.themes["default"][0] = light_background
2323
# pylint: disable-next=protected-access
@@ -144,10 +144,11 @@ def on_uproot_selected(self, message: UprootSelected) -> None:
144144
content_switcher.current = "error"
145145

146146

147-
if __name__ == "<run_path>":
147+
if __name__ in {"<run_path>", "__main__"}:
148148
import uproot_browser.dirs
149149

150150
fname = uproot_browser.dirs.filename(
151151
"../scikit-hep-testdata/src/skhep_testdata/data/uproot-Event.root"
152152
)
153153
app = Browser(path=Path(fname))
154+
app.run()

src/uproot_browser/tui/left_panel.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,20 +42,17 @@ def __init__(self, path: Path, **args: Any) -> None:
4242
def render_label(
4343
self,
4444
node: textual.widgets.tree.TreeNode[UprootEntry],
45-
base_style: Style, # noqa: ARG002
45+
base_style: Style,
4646
style: Style, # ,
4747
) -> rich.text.Text:
48-
meta = {
49-
"@click": f"click_label({node.id})",
50-
"tree_node": node.id,
51-
}
5248
assert node.data
53-
icon_label = node.data.label()
54-
icon_label.apply_meta(meta)
49+
meta = node.data.meta()
50+
label_icon = rich.text.Text(meta["label_icon"])
51+
label_icon.stylize(base_style)
5552

56-
# label = icon_label.copy()
57-
icon_label.stylize(style)
58-
return icon_label
53+
label = rich.text.Text.assemble(label_icon, meta["label_text"])
54+
label.stylize(style)
55+
return label
5956

6057
def on_mount(self) -> None:
6158
self.load_directory(self.root)

0 commit comments

Comments
 (0)