Skip to content

Commit 35ca65d

Browse files
fix: support anything uproot supports (#179)
* fix: support anything uproot supports Signed-off-by: Henry Schreiner <[email protected]> * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Signed-off-by: Henry Schreiner <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent c0598c9 commit 35ca65d

File tree

7 files changed

+27
-90
lines changed

7 files changed

+27
-90
lines changed

src/uproot_browser/__main__.py

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import functools
88
import os
99
import typing
10-
from pathlib import Path
1110
from typing import Any, Callable
1211

1312
import click
@@ -25,15 +24,6 @@
2524
from click_default_group import DefaultGroup
2625

2726

28-
def _existing_path_before_colon(_ctx: object, _value: object, path: str) -> str:
29-
prefix, _, _ = path.partition(":")
30-
if not Path(prefix).is_file():
31-
msg = "{prefix!r} must be an exiting path"
32-
raise click.BadParameter(msg)
33-
34-
return path
35-
36-
3727
@click.group(context_settings=CONTEXT_SETTINGS, cls=DefaultGroup, default="browse")
3828
@click.version_option(version=VERSION)
3929
def main() -> None:
@@ -43,7 +33,7 @@ def main() -> None:
4333

4434

4535
@main.command()
46-
@click.argument("filename", callback=_existing_path_before_colon)
36+
@click.argument("filename")
4737
def tree(filename: str) -> None:
4838
"""
4939
Display a tree.
@@ -68,7 +58,7 @@ def new_func(*args: Any, **kwargs: Any) -> Any:
6858

6959

7060
@main.command()
71-
@click.argument("filename", callback=_existing_path_before_colon)
61+
@click.argument("filename")
7262
@click.option(
7363
"--iterm", is_flag=True, help="Display an iTerm plot (requires [iterm] extra)."
7464
)
@@ -85,12 +75,7 @@ def plot(filename: str, iterm: bool) -> None:
8575
else:
8676
import uproot_browser.plot # pylint: disable=import-outside-toplevel
8777

88-
import uproot_browser.dirs # pylint: disable=import-outside-toplevel
89-
90-
fname = uproot_browser.dirs.filename(filename)
91-
selections = uproot_browser.dirs.selections(filename)
92-
my_tree = uproot.open(fname)
93-
*_, item = uproot_browser.dirs.apply_selection(my_tree, selections)
78+
item = uproot.open(filename)
9479

9580
if iterm:
9681
uproot_browser.plot_mpl.plot(item)
@@ -109,18 +94,15 @@ def plot(filename: str, iterm: bool) -> None:
10994

11095

11196
@main.command()
112-
@click.argument("filename", callback=_existing_path_before_colon)
97+
@click.argument("filename")
11398
def browse(filename: str) -> None:
11499
"""
115100
Display a TUI.
116101
"""
117-
import uproot_browser.dirs # pylint: disable=import-outside-toplevel
118102
import uproot_browser.tui.browser # pylint: disable=import-outside-toplevel
119103

120-
fname = uproot_browser.dirs.filename(filename)
121-
122104
app = uproot_browser.tui.browser.Browser(
123-
path=Path(fname),
105+
path=filename,
124106
)
125107

126108
app.run()

src/uproot_browser/dirs.py

Lines changed: 0 additions & 31 deletions
This file was deleted.

src/uproot_browser/tui/browser.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
import contextlib
77
import sys
8-
from pathlib import Path
98
from typing import Any, ClassVar
109

1110
import plotext as plt
@@ -66,7 +65,7 @@ class Browser(textual.app.App[object]):
6665

6766
show_tree = var(True)
6867

69-
def __init__(self, path: Path, **kwargs: Any) -> None:
68+
def __init__(self, path: str, **kwargs: Any) -> None:
7069
self.path = path
7170
super().__init__(**kwargs)
7271

@@ -161,10 +160,6 @@ def on_uproot_selected(self, message: UprootSelected) -> None:
161160

162161

163162
if __name__ in {"<run_path>", "__main__"}:
164-
import uproot_browser.dirs
165-
166-
fname = uproot_browser.dirs.filename(
167-
"../scikit-hep-testdata/src/skhep_testdata/data/uproot-Event.root"
168-
)
169-
app = Browser(path=Path(fname))
163+
fname = "../scikit-hep-testdata/src/skhep_testdata/data/uproot-Event.root"
164+
app = Browser(path=fname)
170165
app.run()

src/uproot_browser/tui/left_panel.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,11 @@ class UprootTree(textual.widgets.Tree[UprootEntry]):
3737
textual.binding.Binding("l", "cursor_in", "Cursor in", show=False),
3838
]
3939

40-
def __init__(self, path: Path, **args: Any) -> None:
40+
def __init__(self, path: str, **args: Any) -> None:
4141
self.upfile = uproot.open(path)
42+
file_path = Path(self.upfile.file_path)
4243
data = UprootEntry("/", self.upfile)
43-
super().__init__(name=path.name, data=data, label=path.stem, **args)
44+
super().__init__(name=str(file_path), data=data, label=file_path.stem, **args)
4445

4546
def render_label(
4647
self,

src/uproot_browser/tui/right_panel.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

33
import dataclasses
4+
from collections.abc import Iterable
45
from types import TracebackType
56
from typing import Any
67

@@ -34,6 +35,15 @@
3435
placeholder = np.random.rand(1000)
3536

3637

38+
def apply_selection(tree: Any, selection: Iterable[str]) -> Iterable[Any]:
39+
"""
40+
Apply a colon-separated selection to an uproot tree. Slashes are handled by uproot.
41+
"""
42+
for sel in selection:
43+
tree = tree[sel]
44+
yield tree
45+
46+
3747
def make_plot(item: Any, theme: str, *size: int) -> Any:
3848
plt.clf()
3949
plt.plotsize(*size)
@@ -52,9 +62,7 @@ class Plotext:
5262
def __rich_console__(
5363
self, console: rich.console.Console, options: rich.console.ConsoleOptions
5464
) -> rich.console.RenderResult:
55-
*_, item = uproot_browser.dirs.apply_selection(
56-
self.upfile, self.selection.split(":")
57-
)
65+
*_, item = apply_selection(self.upfile, self.selection.split(":"))
5866

5967
if item is None:
6068
yield rich.text.Text()

tests/test_dirs.py

Lines changed: 0 additions & 16 deletions
This file was deleted.

tests/test_tui.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,42 @@
1-
from pathlib import Path
2-
31
import skhep_testdata
42

53
from uproot_browser.tui.browser import Browser
64

75

86
async def test_browse_logo() -> None:
97
async with Browser(
10-
Path(skhep_testdata.data_path("uproot-Event.root"))
8+
skhep_testdata.data_path("uproot-Event.root")
119
).run_test() as pilot:
1210
assert pilot.app.query_one("#main-view").current == "logo"
1311

1412

1513
async def test_browse_plot() -> None:
1614
async with Browser(
17-
Path(skhep_testdata.data_path("uproot-Event.root"))
15+
skhep_testdata.data_path("uproot-Event.root")
1816
).run_test() as pilot:
1917
await pilot.press("down", "down", "down", "enter")
2018
assert pilot.app.query_one("#main-view").current == "plot"
2119

2220

2321
async def test_browse_empty() -> None:
2422
async with Browser(
25-
Path(skhep_testdata.data_path("uproot-empty.root"))
23+
skhep_testdata.data_path("uproot-empty.root")
2624
).run_test() as pilot:
2725
await pilot.press("down", "space", "down", "enter")
2826
assert pilot.app.query_one("#main-view").current == "empty"
2927

3028

3129
async def test_browse_empty_vim() -> None:
3230
async with Browser(
33-
Path(skhep_testdata.data_path("uproot-empty.root"))
31+
skhep_testdata.data_path("uproot-empty.root")
3432
).run_test() as pilot:
3533
await pilot.press("j", "l", "j", "enter")
3634
assert pilot.app.query_one("#main-view").current == "empty"
3735

3836

3937
async def test_help_focus() -> None:
4038
async with Browser(
41-
Path(skhep_testdata.data_path("uproot-empty.root"))
39+
skhep_testdata.data_path("uproot-empty.root")
4240
).run_test() as pilot:
4341
await pilot.press("?")
4442
focus_chain = [widget.id for widget in pilot.app.screen.focus_chain]

0 commit comments

Comments
 (0)