From a1d84e63ee76dca77a78ce3c3079dd808556955a Mon Sep 17 00:00:00 2001 From: Gabriel Selzer Date: Fri, 9 Sep 2022 12:27:49 -0500 Subject: [PATCH] Allow add_legacy configuration in settings --- src/napari_imagej/config_default.yaml | 4 ++++ src/napari_imagej/java.py | 23 +++++++++-------------- src/napari_imagej/widgets/menu.py | 12 ++++++------ tests/widgets/test_menu.py | 25 +++++++++---------------- 4 files changed, 28 insertions(+), 36 deletions(-) diff --git a/src/napari_imagej/config_default.yaml b/src/napari_imagej/config_default.yaml index b71f3903..9a2c439c 100644 --- a/src/napari_imagej/config_default.yaml +++ b/src/napari_imagej/config_default.yaml @@ -11,6 +11,10 @@ # The default (null) will use the latest version of ImageJ2, downloading it if needed. imagej_directory_or_endpoint: 'net.imagej:imagej' +# This can be used to include original ImageJ functionality. +# Iff True, original ImageJ functionality (ij.* packages) will be available. +include_imagej_legacy: false + # This can be used to identify whether transferred data between ImageJ2 and napari # should be selected via activation or by user selection via a dialog. # By default, the active layer/window is chosen for transfer between applications. diff --git a/src/napari_imagej/java.py b/src/napari_imagej/java.py index 19291388..c58642dd 100644 --- a/src/napari_imagej/java.py +++ b/src/napari_imagej/java.py @@ -6,10 +6,8 @@ - used to access the ImageJ instance * ensure_jvm_started() - used to block execution until the ImageJ instance is ready - * running_headless() + * jvm_is_headless() - reports whether the JVM is being run headlessly - * setting() - - used to obtain values of configuration settings * log_debug() - used for logging in a standardized way @@ -47,15 +45,15 @@ def ensure_jvm_started() -> None: ij_future.wait() -def get_mode() -> str: +def _get_mode() -> str: """ Returns the mode ImageJ will be run in """ return "headless" if sys.platform == "darwin" else "interactive" -def running_headless() -> bool: - return get_mode() == "headless" +def jvm_is_headless() -> bool: + return _get_mode() == "headless" def _imagej_init(): @@ -73,15 +71,12 @@ def _imagej_init(): config.endpoints.append("io.scif:scifio:0.43.1") log_debug("Completed JVM Configuration") - # Configure PyImageJ settings - ij_settings = { - "ij_dir_or_version_or_endpoint": settings["imagej_directory_or_endpoint"].get(), - "mode": get_mode(), - "add_legacy": False, - } - # Launch PyImageJ - _ij = imagej.init(**ij_settings) + _ij = imagej.init( + ij_dir_or_version_or_endpoint=settings["imagej_directory_or_endpoint"].get(str), + mode=_get_mode(), + add_legacy=settings["include_imagej_legacy"].get(bool), + ) log_debug(f"Initialized at version {_ij.getVersion()}") # Return the ImageJ gateway diff --git a/src/napari_imagej/widgets/menu.py b/src/napari_imagej/widgets/menu.py index 125b775b..1d159c75 100644 --- a/src/napari_imagej/widgets/menu.py +++ b/src/napari_imagej/widgets/menu.py @@ -15,7 +15,7 @@ from qtpy.QtWidgets import QHBoxLayout, QMessageBox, QPushButton, QWidget from napari_imagej import settings -from napari_imagej.java import ensure_jvm_started, ij, jc, log_debug, running_headless +from napari_imagej.java import ensure_jvm_started, ij, jc, jvm_is_headless, log_debug from napari_imagej.resources import resource_path from napari_imagej.utilities._module_utils import _get_layers_hack @@ -38,7 +38,7 @@ def __init__(self, viewer: Viewer): self.settings_button: SettingsButton = SettingsButton(viewer) self.layout().addWidget(self.settings_button) - if running_headless(): + if jvm_is_headless(): self.gui_button.clicked.connect(self.gui_button.disable_popup) else: self.gui_button.clicked.connect(self._showUI) @@ -241,12 +241,12 @@ def get_active_layer(self) -> None: class GUIButton(QPushButton): def __init__(self): super().__init__() - running_headful = not running_headless() self.setEnabled(False) - if running_headful: - self._setup_headful() - else: + + if jvm_is_headless(): self._setup_headless() + else: + self._setup_headful() def _set_icon(self, path: str): icon: QIcon = QIcon(QPixmap(path)) diff --git a/tests/widgets/test_menu.py b/tests/widgets/test_menu.py index 2f4976df..5f763ebb 100644 --- a/tests/widgets/test_menu.py +++ b/tests/widgets/test_menu.py @@ -14,7 +14,7 @@ from qtpy.QtWidgets import QApplication, QHBoxLayout, QMessageBox from napari_imagej import settings -from napari_imagej.java import running_headless +from napari_imagej.java import jvm_is_headless from napari_imagej.resources import resource_path from napari_imagej.widgets import menu from napari_imagej.widgets.menu import ( @@ -26,6 +26,9 @@ ) from tests.utils import jc +# Determine whether we are testing headlessly +TESTING_HEADLESS: bool = jvm_is_headless() + @pytest.fixture(autouse=True) def clean_settings(): @@ -132,9 +135,7 @@ def test_widget_layout(gui_widget: NapariImageJMenu): assert isinstance(subwidgets[4], SettingsButton) -@pytest.mark.skipif( - running_headless(), reason="Only applies when not running headlessly" -) +@pytest.mark.skipif(TESTING_HEADLESS, reason="Only applies when not running headlessly") def test_GUIButton_layout_headful(qtbot, asserter, ij, gui_widget: NapariImageJMenu): """Tests headful-specific settings of GUIButton""" button: GUIButton = gui_widget.gui_button @@ -155,9 +156,7 @@ def test_GUIButton_layout_headful(qtbot, asserter, ij, gui_widget: NapariImageJM asserter(ij.ui().isVisible) -@pytest.mark.skipif( - not running_headless(), reason="Only applies when running headlessly" -) +@pytest.mark.skipif(not TESTING_HEADLESS, reason="Only applies when running headlessly") def test_GUIButton_layout_headless(asserter, gui_widget: NapariImageJMenu): """Tests headless-specific settings of GUIButton""" # Wait until the JVM starts to test settings @@ -214,9 +213,7 @@ def passed(self) -> bool: assert runnable.passed() -@pytest.mark.skipif( - running_headless(), reason="Only applies when not running headlessly" -) +@pytest.mark.skipif(TESTING_HEADLESS, reason="Only applies when not running headlessly") def test_active_data_send(asserter, qtbot, ij, gui_widget: NapariImageJMenu): button: ToIJButton = gui_widget.to_ij assert not button.isEnabled() @@ -240,9 +237,7 @@ def test_active_data_send(asserter, qtbot, ij, gui_widget: NapariImageJMenu): assert "test_to" == active_display.getName() -@pytest.mark.skipif( - running_headless(), reason="Only applies when not running headlessly" -) +@pytest.mark.skipif(TESTING_HEADLESS, reason="Only applies when not running headlessly") def test_active_data_receive(asserter, qtbot, ij, gui_widget: NapariImageJMenu): button: FromIJButton = gui_widget.from_ij assert not button.isEnabled() @@ -267,9 +262,7 @@ def test_active_data_receive(asserter, qtbot, ij, gui_widget: NapariImageJMenu): assert (10, 10, 10) == layer.data.shape -@pytest.mark.skipif( - running_headless(), reason="Only applies when not running headlessly" -) +@pytest.mark.skipif(TESTING_HEADLESS, reason="Only applies when not running headlessly") def test_data_choosers(asserter, qtbot, ij, gui_widget_chooser): button_to: ToIJButton = gui_widget_chooser.to_ij button_from: FromIJButton = gui_widget_chooser.from_ij