|
26 | 26 |
|
27 | 27 | import logging |
28 | 28 | logger = logging.getLogger(__name__) |
| 29 | +import errno |
| 30 | +import exceptions |
29 | 31 | import numpy as np |
30 | 32 | import os |
31 | 33 | import sys |
32 | 34 | import urllib |
33 | 35 | import urllib2 |
34 | 36 | import shutil |
35 | 37 | import tempfile |
| 38 | +import traceback |
36 | 39 |
|
37 | 40 | import javabridge as jutil |
38 | 41 | import bioformats |
@@ -340,6 +343,32 @@ def __init__(self, rdr): |
340 | 343 | 'Set the name of the data file') |
341 | 344 | return ReaderWrapper |
342 | 345 |
|
| 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 | + |
343 | 372 | __omero_server = None |
344 | 373 | __omero_username = None |
345 | 374 | __omero_session_id = None |
@@ -542,9 +571,6 @@ def __init__(self, path=None, url=None, perform_init=True): |
542 | 571 | omero_logout() |
543 | 572 | omero_login() |
544 | 573 | else: |
545 | | - import errno |
546 | | - import exceptions |
547 | | - import traceback |
548 | 574 | logger.warn(e.message) |
549 | 575 | for line in traceback.format_exc().split("\n"): |
550 | 576 | logger.warn(line) |
@@ -581,6 +607,12 @@ def __init__(self, path=None, url=None, perform_init=True): |
581 | 607 | self.path = self.path.replace("/", os.path.sep) |
582 | 608 | filename = os.path.split(path)[1] |
583 | 609 |
|
| 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 | + |
584 | 616 | self.stream = jutil.make_instance('loci/common/RandomAccessInputStream', |
585 | 617 | '(Ljava/lang/String;)V', |
586 | 618 | self.path) |
@@ -664,14 +696,11 @@ def init_reader(self): |
664 | 696 | try: |
665 | 697 | self.rdr.setId(self.path) |
666 | 698 | except jutil.JavaException, e: |
667 | | - import errno |
668 | | - import exceptions |
669 | | - import traceback |
670 | 699 | logger.warn(e.message) |
671 | 700 | for line in traceback.format_exc().split("\n"): |
672 | 701 | logger.warn(line) |
673 | 702 | je = e.throwable |
674 | | - if jutil.is_instance_of( |
| 703 | + if has_omero_packages() and jutil.is_instance_of( |
675 | 704 | je, "Glacier2/PermissionDeniedException"): |
676 | 705 | # Handle at a higher level |
677 | 706 | raise |
|
0 commit comments