Skip to content

Commit

Permalink
Allow add_legacy configuration in settings
Browse files Browse the repository at this point in the history
  • Loading branch information
gselzer committed Sep 9, 2022
1 parent 9547cae commit a1d84e6
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 36 deletions.
4 changes: 4 additions & 0 deletions src/napari_imagej/config_default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
23 changes: 9 additions & 14 deletions src/napari_imagej/java.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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():
Expand All @@ -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
Expand Down
12 changes: 6 additions & 6 deletions src/napari_imagej/widgets/menu.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)
Expand Down Expand Up @@ -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))
Expand Down
25 changes: 9 additions & 16 deletions tests/widgets/test_menu.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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():
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand All @@ -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
Expand Down

0 comments on commit a1d84e6

Please sign in to comment.