diff --git a/src/uproot_browser/__main__.py b/src/uproot_browser/__main__.py index 1433f7b..87ecf8d 100644 --- a/src/uproot_browser/__main__.py +++ b/src/uproot_browser/__main__.py @@ -7,7 +7,6 @@ import functools import os import typing -from pathlib import Path from typing import Any, Callable import click @@ -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: @@ -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. @@ -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)." ) @@ -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) @@ -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() diff --git a/src/uproot_browser/dirs.py b/src/uproot_browser/dirs.py deleted file mode 100644 index 7b2559e..0000000 --- a/src/uproot_browser/dirs.py +++ /dev/null @@ -1,31 +0,0 @@ -""" -This holds common functions for accessing a file, ROOT directories, and trees -split by colons. -""" - -from __future__ import annotations - -from typing import Any, Iterable - - -def filename(select: str) -> str: - """ - Return the filename portion of a colon-separated selection. - """ - return select.split(":")[0] - - -def selections(select: str) -> tuple[str, ...]: - """ - Return the directory/tree portion of a colon-separated selection. - """ - return tuple(select.split(":")[1:]) - - -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 diff --git a/src/uproot_browser/tui/browser.py b/src/uproot_browser/tui/browser.py index 42d4bb5..89c57ce 100644 --- a/src/uproot_browser/tui/browser.py +++ b/src/uproot_browser/tui/browser.py @@ -5,7 +5,6 @@ import contextlib import sys -from pathlib import Path from typing import Any, ClassVar import plotext as plt @@ -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) @@ -161,10 +160,6 @@ def on_uproot_selected(self, message: UprootSelected) -> None: if __name__ in {"", "__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() diff --git a/src/uproot_browser/tui/left_panel.py b/src/uproot_browser/tui/left_panel.py index 5cd734d..ac791be 100644 --- a/src/uproot_browser/tui/left_panel.py +++ b/src/uproot_browser/tui/left_panel.py @@ -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, diff --git a/src/uproot_browser/tui/right_panel.py b/src/uproot_browser/tui/right_panel.py index 76461fa..ade4102 100644 --- a/src/uproot_browser/tui/right_panel.py +++ b/src/uproot_browser/tui/right_panel.py @@ -1,6 +1,7 @@ from __future__ import annotations import dataclasses +from collections.abc import Iterable from types import TracebackType from typing import Any @@ -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) @@ -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() diff --git a/tests/test_dirs.py b/tests/test_dirs.py deleted file mode 100644 index 4073132..0000000 --- a/tests/test_dirs.py +++ /dev/null @@ -1,16 +0,0 @@ -from __future__ import annotations - -from uproot_browser.dirs import filename, selections - - -def test_filename(): - r"Should handle C:\ too" - assert filename("/file.root") == "/file.root" - assert filename("/file.root:dir") == "/file.root" - assert filename("/file.root:dir:tree") == "/file.root" - - -def test_selection(): - assert selections("/file.root") == () - assert selections("/file.root:dir") == ("dir",) - assert selections("/file.root:dir:tree") == ("dir", "tree") diff --git a/tests/test_tui.py b/tests/test_tui.py index b10d21e..03b936d 100644 --- a/tests/test_tui.py +++ b/tests/test_tui.py @@ -1,5 +1,3 @@ -from pathlib import Path - import skhep_testdata from uproot_browser.tui.browser import Browser @@ -7,14 +5,14 @@ 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" @@ -22,7 +20,7 @@ async def test_browse_plot() -> None: 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" @@ -30,7 +28,7 @@ async def test_browse_empty() -> None: 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" @@ -38,7 +36,7 @@ async def test_browse_empty_vim() -> None: 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]