Skip to content

Commit beb827a

Browse files
author
Lee Kamentsky
committed
issue #6 - check for presence of OMERO classes before trying to use them
1 parent db370d9 commit beb827a

4 files changed

Lines changed: 52 additions & 8 deletions

File tree

bioformats/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@
7979
# Omero
8080

8181
from .formatreader import use_omero_credentials, set_omero_credentials, get_omero_credentials
82-
from .formatreader import set_omero_login_hook, omero_logout
82+
from .formatreader import set_omero_login_hook, omero_logout, has_omero_packages
8383
from .formatreader import K_OMERO_SERVER, K_OMERO_PORT, K_OMERO_USER, K_OMERO_SESSION_ID,\
8484
K_OMERO_PASSWORD, K_OMERO_CONFIG_FILE
8585

bioformats/formatreader.py

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,16 @@
2626

2727
import logging
2828
logger = logging.getLogger(__name__)
29+
import errno
30+
import exceptions
2931
import numpy as np
3032
import os
3133
import sys
3234
import urllib
3335
import urllib2
3436
import shutil
3537
import tempfile
38+
import traceback
3639

3740
import javabridge as jutil
3841
import bioformats
@@ -340,6 +343,32 @@ def __init__(self, rdr):
340343
'Set the name of the data file')
341344
return ReaderWrapper
342345

346+
__has_omero_jars = None
347+
def has_omero_packages():
348+
'''Return True if we can find the packages needed for OMERO
349+
350+
In order to run OMERO, you'll need the OMERO client and ICE
351+
on your class path (not supplied with python-bioformats and
352+
specific to your server's version)
353+
'''
354+
global __has_omero_jars
355+
if __has_omero_jars is None:
356+
class_loader = jutil.static_call(
357+
"java/lang/ClassLoader", "getSystemClassLoader",
358+
"()Ljava/lang/ClassLoader;")
359+
for klass in ("Glacier2.PermissionDeniedException",
360+
"loci.ome.io.OmeroReader", "omero.client"):
361+
try:
362+
jutil.call(
363+
class_loader, "loadClass",
364+
"(Ljava/lang/String;)Ljava/lang/Class;", klass)
365+
except:
366+
__has_omero_jars = False
367+
break
368+
else:
369+
__has_omero_jars = True
370+
return __has_omero_jars
371+
343372
__omero_server = None
344373
__omero_username = None
345374
__omero_session_id = None
@@ -542,9 +571,6 @@ def __init__(self, path=None, url=None, perform_init=True):
542571
omero_logout()
543572
omero_login()
544573
else:
545-
import errno
546-
import exceptions
547-
import traceback
548574
logger.warn(e.message)
549575
for line in traceback.format_exc().split("\n"):
550576
logger.warn(line)
@@ -581,6 +607,12 @@ def __init__(self, path=None, url=None, perform_init=True):
581607
self.path = self.path.replace("/", os.path.sep)
582608
filename = os.path.split(path)[1]
583609

610+
if not os.path.isfile(self.path):
611+
raise exceptions.IOError(
612+
errno.ENOENT,
613+
"The file, \"%s\", does not exist." % path,
614+
path)
615+
584616
self.stream = jutil.make_instance('loci/common/RandomAccessInputStream',
585617
'(Ljava/lang/String;)V',
586618
self.path)
@@ -664,14 +696,11 @@ def init_reader(self):
664696
try:
665697
self.rdr.setId(self.path)
666698
except jutil.JavaException, e:
667-
import errno
668-
import exceptions
669-
import traceback
670699
logger.warn(e.message)
671700
for line in traceback.format_exc().split("\n"):
672701
logger.warn(line)
673702
je = e.throwable
674-
if jutil.is_instance_of(
703+
if has_omero_packages() and jutil.is_instance_of(
675704
je, "Glacier2/PermissionDeniedException"):
676705
# Handle at a higher level
677706
raise

bioformats/tests/test_load_using_bioformats.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
# Copyright (c) 2009-2014 Broad Institute
66
# All rights reserved.
77

8+
import exceptions
89
import os
910
import unittest
1011

@@ -30,4 +31,11 @@ def test_load_using_bioformats(self):
3031
image, scale = load_image(path, rescale=False,
3132
wants_max_intensity=True)
3233
print image.shape
34+
35+
def test_file_not_found(self):
36+
# Regression test of issue #6
37+
path = os.path.join(os.path.dirname(__file__), 'Channel5-01-A-01.tif')
38+
self.assertRaises(exceptions.IOError,
39+
lambda :load_image(path))
40+
3341

docs/index.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,13 @@ Writing images
121121
OMERO
122122
=====
123123

124+
Python-bioformats can load images from OMERO URLs. To do this, you'll need
125+
to put the JAR files for your OMERO server version onto your Java classpath
126+
when you start. For OMERO server 5.0.0, these are blitz.jar, common.jar,
127+
ice.jar, ice-glacier2.jar, ice-storm.jar and ice-grid.jar. You'll also need
128+
to use the matching version of the Bio-formats library for the OMERO release
129+
instead of the one included with python-bioformats.
130+
124131
.. autofunction:: bioformats.use_omero_credentials
125132
.. autofunction:: bioformats.set_omero_credentials
126133
.. autofunction:: bioformats.get_omero_credentials

0 commit comments

Comments
 (0)