Skip to content

Commit

Permalink
fix: support anything uproot supports (#179)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
henryiii and pre-commit-ci[bot] authored Oct 18, 2024
1 parent c0598c9 commit 35ca65d
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 90 deletions.
28 changes: 5 additions & 23 deletions src/uproot_browser/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import functools
import os
import typing
from pathlib import Path
from typing import Any, Callable

import click
Expand All @@ -25,15 +24,6 @@
from click_default_group import DefaultGroup


def _existing_path_before_colon(_ctx: object, _value: object, path: str) -> str:
prefix, _, _ = path.partition(":")
if not Path(prefix).is_file():
msg = "{prefix!r} must be an exiting path"
raise click.BadParameter(msg)

return path


@click.group(context_settings=CONTEXT_SETTINGS, cls=DefaultGroup, default="browse")
@click.version_option(version=VERSION)
def main() -> None:
Expand All @@ -43,7 +33,7 @@ def main() -> None:


@main.command()
@click.argument("filename", callback=_existing_path_before_colon)
@click.argument("filename")
def tree(filename: str) -> None:
"""
Display a tree.
Expand All @@ -68,7 +58,7 @@ def new_func(*args: Any, **kwargs: Any) -> Any:


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

import uproot_browser.dirs # pylint: disable=import-outside-toplevel

fname = uproot_browser.dirs.filename(filename)
selections = uproot_browser.dirs.selections(filename)
my_tree = uproot.open(fname)
*_, item = uproot_browser.dirs.apply_selection(my_tree, selections)
item = uproot.open(filename)

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


@main.command()
@click.argument("filename", callback=_existing_path_before_colon)
@click.argument("filename")
def browse(filename: str) -> None:
"""
Display a TUI.
"""
import uproot_browser.dirs # pylint: disable=import-outside-toplevel
import uproot_browser.tui.browser # pylint: disable=import-outside-toplevel

fname = uproot_browser.dirs.filename(filename)

app = uproot_browser.tui.browser.Browser(
path=Path(fname),
path=filename,
)

app.run()
Expand Down
31 changes: 0 additions & 31 deletions src/uproot_browser/dirs.py

This file was deleted.

11 changes: 3 additions & 8 deletions src/uproot_browser/tui/browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import contextlib
import sys
from pathlib import Path
from typing import Any, ClassVar

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

show_tree = var(True)

def __init__(self, path: Path, **kwargs: Any) -> None:
def __init__(self, path: str, **kwargs: Any) -> None:
self.path = path
super().__init__(**kwargs)

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


if __name__ in {"<run_path>", "__main__"}:
import uproot_browser.dirs

fname = uproot_browser.dirs.filename(
"../scikit-hep-testdata/src/skhep_testdata/data/uproot-Event.root"
)
app = Browser(path=Path(fname))
fname = "../scikit-hep-testdata/src/skhep_testdata/data/uproot-Event.root"
app = Browser(path=fname)
app.run()
5 changes: 3 additions & 2 deletions src/uproot_browser/tui/left_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,11 @@ class UprootTree(textual.widgets.Tree[UprootEntry]):
textual.binding.Binding("l", "cursor_in", "Cursor in", show=False),
]

def __init__(self, path: Path, **args: Any) -> None:
def __init__(self, path: str, **args: Any) -> None:
self.upfile = uproot.open(path)
file_path = Path(self.upfile.file_path)
data = UprootEntry("/", self.upfile)
super().__init__(name=path.name, data=data, label=path.stem, **args)
super().__init__(name=str(file_path), data=data, label=file_path.stem, **args)

def render_label(
self,
Expand Down
14 changes: 11 additions & 3 deletions src/uproot_browser/tui/right_panel.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import dataclasses
from collections.abc import Iterable
from types import TracebackType
from typing import Any

Expand Down Expand Up @@ -34,6 +35,15 @@
placeholder = np.random.rand(1000)


def apply_selection(tree: Any, selection: Iterable[str]) -> Iterable[Any]:
"""
Apply a colon-separated selection to an uproot tree. Slashes are handled by uproot.
"""
for sel in selection:
tree = tree[sel]
yield tree


def make_plot(item: Any, theme: str, *size: int) -> Any:
plt.clf()
plt.plotsize(*size)
Expand All @@ -52,9 +62,7 @@ class Plotext:
def __rich_console__(
self, console: rich.console.Console, options: rich.console.ConsoleOptions
) -> rich.console.RenderResult:
*_, item = uproot_browser.dirs.apply_selection(
self.upfile, self.selection.split(":")
)
*_, item = apply_selection(self.upfile, self.selection.split(":"))

if item is None:
yield rich.text.Text()
Expand Down
16 changes: 0 additions & 16 deletions tests/test_dirs.py

This file was deleted.

12 changes: 5 additions & 7 deletions tests/test_tui.py
Original file line number Diff line number Diff line change
@@ -1,44 +1,42 @@
from pathlib import Path

import skhep_testdata

from uproot_browser.tui.browser import Browser


async def test_browse_logo() -> None:
async with Browser(
Path(skhep_testdata.data_path("uproot-Event.root"))
skhep_testdata.data_path("uproot-Event.root")
).run_test() as pilot:
assert pilot.app.query_one("#main-view").current == "logo"


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


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


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


async def test_help_focus() -> None:
async with Browser(
Path(skhep_testdata.data_path("uproot-empty.root"))
skhep_testdata.data_path("uproot-empty.root")
).run_test() as pilot:
await pilot.press("?")
focus_chain = [widget.id for widget in pilot.app.screen.focus_chain]
Expand Down

0 comments on commit 35ca65d

Please sign in to comment.