Skip to content

Commit

Permalink
Add recommended_versions
Browse files Browse the repository at this point in the history
This enables us to suggest users upgrade their versions without forcing
them to do so.
  • Loading branch information
gselzer committed Jun 6, 2023
1 parent ab7c7d4 commit 982fe62
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 4 deletions.
28 changes: 26 additions & 2 deletions src/napari_imagej/java.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
from jpype import JClass
from scyjava import config, get_version, is_version_at_least, jimport, jvm_started

from napari_imagej import settings
from napari_imagej.utilities.logging import log_debug
from napari_imagej import __version__, settings
from napari_imagej.utilities.logging import log_debug, warn

# -- Constants --

Expand All @@ -34,6 +34,8 @@
"sc.fiji:TrackMate": "7.11.0",
}

recommended_versions = {}

# -- ImageJ API -- #

_ij = None
Expand Down Expand Up @@ -152,6 +154,28 @@ def _validate_imagej():
)
raise RuntimeError(failure_str)

# Find versions below recommended
violations = []
for component, cls in component_requirements.items():
if component not in recommended_versions:
continue
recommended_version = recommended_versions[component]
component_version = get_version(cls)
if not is_version_at_least(component_version, recommended_version):
violations.append(
f"{component} : {recommended_version} (Installed: {component_version})"
)

# If there are older versions, warn the user
if violations:
failure_str = (
f"napari-imagej v{__version__} recommends using "
"the following component versions:"
)
violations.insert(0, failure_str)
failure_str = "\n\t".join(violations)
warn(failure_str)


def _optional_requirements():
optionals = {}
Expand Down
35 changes: 33 additions & 2 deletions tests/test_java.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

from scyjava import get_version, is_version_at_least, jimport

from napari_imagej import settings
from napari_imagej.java import minimum_versions
from napari_imagej import __version__, settings
from napari_imagej.java import _validate_imagej, minimum_versions
from napari_imagej.utilities.logging import logger

version_checks = {
"io.scif:scifio": "io.scif.SCIFIO",
Expand Down Expand Up @@ -57,3 +58,33 @@ def test_endpoint(ij):
version = gav[2]
exp_version = get_version(jimport(version_checks[ga]))
assert is_version_at_least(version, exp_version)


def test_recommended_version(ij):
# Save old recommended versions
import napari_imagej.java

existing = napari_imagej.java.recommended_versions
napari_imagej.java.recommended_versions = {"org.scijava:scijava-common": "999.0.0"}

# Setup log handler to capture warning
import io
import logging

log_capture_string = io.StringIO()
ch = logging.StreamHandler(log_capture_string)
ch.setLevel(logging.WARN)
logger().addHandler(ch)
# Validate ImageJ - capture lower-than-recommended version
_validate_imagej()
log_contents = log_capture_string.getvalue()
log_capture_string.close()
# Assert warning given
assert log_contents == (
f"napari-imagej: napari-imagej v{__version__} recommends using the "
"following component versions:\n\torg.scijava:scijava-common : "
"999.0.0 (Installed: 2.94.1)\n"
)

# restore recommended versions
napari_imagej.java.recommended_versions = existing

0 comments on commit 982fe62

Please sign in to comment.