Skip to content

Commit

Permalink
Merge pull request #4 from tlambert03/use-scyjava
Browse files Browse the repository at this point in the history
use scyjava, remove all the stuff
  • Loading branch information
tlambert03 authored May 23, 2022
2 parents 00a4083 + aa2f07f commit c63d7dc
Show file tree
Hide file tree
Showing 87 changed files with 36 additions and 46,052 deletions.
9 changes: 4 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,12 @@ on:

jobs:
test:
name: ${{ matrix.platform }} (py${{ matrix.python-version }}, j${{ matrix.java }})
name: ${{ matrix.platform }} (py${{ matrix.python-version }})
runs-on: ${{ matrix.platform }}
strategy:
fail-fast: false
matrix:
java: [8, 11]
python-version: [3.7, 3.8, 3.9]
python-version: [3.7, 3.8, 3.9, '3.10']
platform: [ubuntu-latest, macos-latest, windows-latest]

steps:
Expand All @@ -31,7 +30,7 @@ jobs:
- uses: actions/setup-java@v2
with:
distribution: "temurin"
java-version: ${{ matrix.java }}
java-version: "11"

- name: Install dependencies
run: |
Expand All @@ -43,7 +42,7 @@ jobs:

- name: Coverage
if: runner.os == 'Linux'
uses: codecov/codecov-action@v1
uses: codecov/codecov-action@v2

deploy:
name: Deploy
Expand Down
12 changes: 6 additions & 6 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.0.1
rev: v4.2.0
hooks:
- id: check-docstring-first
- id: end-of-file-fixer
- id: trailing-whitespace
- repo: https://github.com/asottile/setup-cfg-fmt
rev: v1.17.0
rev: v1.20.1
hooks:
- id: setup-cfg-fmt
- repo: https://github.com/PyCQA/flake8
rev: 3.9.2
rev: 4.0.1
hooks:
- id: flake8
additional_dependencies: [flake8-typing-imports==1.7.0]
Expand All @@ -21,17 +21,17 @@ repos:
- id: autoflake
args: ["--in-place", "--remove-all-unused-imports"]
- repo: https://github.com/PyCQA/isort
rev: 5.8.0
rev: 5.10.1
hooks:
- id: isort
exclude: bioformats_jar/_loci
- repo: https://github.com/psf/black
rev: 21.5b2
rev: 22.3.0
hooks:
- id: black
exclude: bioformats_jar/_loci
- repo: https://github.com/asottile/pyupgrade
rev: v2.19.0
rev: v2.32.1
hooks:
- id: pyupgrade
args: [--py37-plus]
54 changes: 14 additions & 40 deletions bioformats_jar/__init__.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import os
from functools import lru_cache
from typing import TYPE_CHECKING, Any, List, Union
from typing import TYPE_CHECKING, Any

import jpype
import scyjava

if TYPE_CHECKING:
from . import _loci, _ome

__all__ = [
"start_jvm",
"get_loci",
"set_loci_log_level",
"get_ome",
Expand All @@ -18,43 +18,17 @@
]


_BFJAR = os.path.join(os.path.dirname(__file__), "bioformats_package.jar")
BIOFORMATS_VERSION = os.getenv("BIOFORMATS_VERSION", "LATEST")
BIOFORMATS_LICENSE = os.getenv("BIOFORMATS_LICENSE", "gpl")
assert BIOFORMATS_LICENSE in {
"gpl",
"bsd",
}, "BIOFORMATS_LICENSE env var must be either 'gpl' or 'bsd'"
LOG_LEVEL = os.getenv("BIOFORMATS_LOG_LEVEL", "ERROR")
ATTACH_THREAD = os.getenv("BIOFORMATS_ATTACH_THREAD") in ("1", "True", "true")
JAVA_MEM = os.getenv("BIOFORMATS_MEMORY", "512m")
JAR = os.getenv("BIOFORMATS_JAR", _BFJAR)


@lru_cache()
def start_jvm(
classpath: Union[str, List[str]] = JAR,
attach_thread=ATTACH_THREAD,
memory=JAVA_MEM,
**kwargs,
) -> None:
"""Start the Java virtual machine with `jpype.startJVM`.
Parameters
----------
classpath : str or List[str], optional
jar path or list of jar paths, by default the bioformats_package.jar
Can also be set with the "BIOFORMATS_JAR" environment variable
attach_thread : [type], optional
If `True`, attaches the current thread to the JVM as a user thread. by default
True. Can also be set with the "BIOFORMATS_ATTACH_THREAD" environment variable
memory : str, optional
Java memory to use, by default "512m".
Can also be set with the "BIOFORMATS_MEMORY" environment variable
"""
if jpype.isJVMStarted():
return

jpype.startJVM(f"-Xmx{memory}", classpath=classpath, **kwargs)

if attach_thread:
java = jpype.JPackage("java")
if not java.lang.Thread.isAttached():
java.lang.Thread.attach()
scyjava.config.endpoints.append(
f"ome:formats-{BIOFORMATS_LICENSE}:{BIOFORMATS_VERSION}"
)


@lru_cache()
Expand All @@ -72,10 +46,10 @@ def get_loci(log_level: str = LOG_LEVEL) -> "_loci.__module_protocol__":
loci
the loci module from bioformats_package.jar
"""
start_jvm()
FormatTools = scyjava.jimport("loci.formats.FormatTools")
loci = jpype.JPackage("loci")
loci.common.DebugTools.setRootLevel(log_level)
loci.__version__ = loci.formats.FormatTools.VERSION
loci.__version__ = FormatTools.VERSION
return loci


Expand All @@ -87,7 +61,7 @@ def set_loci_log_level(level):
@lru_cache()
def get_ome() -> "_ome.__module_protocol__":
"""Start JVM (if necessary) and get the `ome` module"""
start_jvm()
scyjava.start_jvm()
return jpype.JPackage("ome")


Expand Down
15 changes: 0 additions & 15 deletions bioformats_jar/_loci/__init__.pyi

This file was deleted.

Loading

0 comments on commit c63d7dc

Please sign in to comment.