Replaced IOError with custom glymur.jp2k.LibraryNotFoundError

To be used when an openjpeg library is not available to perform a certain task.
This commit is contained in:
John Evans 2013-07-16 12:58:34 -04:00
commit fc737ed00d
2 changed files with 31 additions and 13 deletions

View file

@ -65,6 +65,13 @@ _INFO_CALLBACK = _CMPFUNC(_default_info_handler)
_WARNING_CALLBACK = _CMPFUNC(_default_warning_handler)
class LibraryNotFoundError(IOError):
"""Raised if functionality is requested without the necessary library.
"""
def __init__(self, msg):
IOError.__init__(self, msg)
class Jp2k(Jp2kBox):
"""JPEG 2000 file.
@ -225,10 +232,16 @@ class Jp2k(Jp2kBox):
>>> tfile = NamedTemporaryFile(suffix='.jp2', delete=False)
>>> j = Jp2k(tfile.name, mode='wb')
>>> j.write(data.astype(np.uint8))
Raises
------
glymur.LibraryNotFoundError
If glymur is unable to load the openjp2 library.
"""
if _opj2.OPENJP2 is None:
raise IOError("You must have the development version of OpenJP2 "
"installed before using this functionality.")
raise LibraryNotFoundError("You must have the development version "
"of OpenJP2 installed before using "
"this functionality.")
cparams = _opj2.set_default_encoder_parameters()
@ -577,7 +590,7 @@ class Jp2k(Jp2kBox):
rlevel : int, optional
Factor by which to rlevel output resolution. Use -1 to get the
lowest resolution thumbnail. This is the only keyword option
available to use when only OpenJPEG version 1.5.1 is present.
available to use when only the OpenJPEG version 1.5.1 is present.
layer : int, optional
Number of quality layer to decode.
area : tuple, optional
@ -595,6 +608,9 @@ class Jp2k(Jp2kBox):
Raises
------
glymur.LibraryNotFoundError
If glymur is unable to load either the openjpeg or openjp2
libraries.
IOError
If the image has differing subsample factors.
@ -618,9 +634,10 @@ class Jp2k(Jp2kBox):
elif _opj.OPENJPEG is not None:
img = self._read_openjpeg(**kwargs)
else:
raise IOError("You must have either a recent version of OpenJPEG "
"or the development version of OpenJP2 installed "
"before using this functionality.")
raise LibraryNotFoundError("You must have either a recent version "
"of OpenJPEG or the development "
"version of OpenJP2 installed before "
"using this functionality.")
return img
def _read_openjpeg(self, rlevel=0, verbose=False):
@ -964,12 +981,13 @@ class Jp2k(Jp2kBox):
Raises
------
NotImplementedError
If the openjp2 library is not available.
glymur.LibraryNotFoundError
If glymur is unable to load the openjp2 library.
"""
if _opj2.OPENJP2 is None:
raise IOError("You must have the development version of OpenJP2 "
"installed before using this functionality.")
raise LibraryNotFoundError("You must have the development version "
"of OpenJP2 installed before using "
"this functionality.")
lst = self._read_common(rlevel=rlevel,
layer=layer,

View file

@ -61,7 +61,7 @@ class TestConfig(unittest.TestCase):
"""
with patch('glymur.lib.openjp2.OPENJP2', new=None):
with patch('glymur.lib.openjpeg.OPENJPEG', new=None):
with self.assertRaises((IOError, OSError)):
with self.assertRaises(glymur.jp2k.LibraryNotFoundError):
d = glymur.Jp2k(self.jp2file).read()
def test_read_bands_without_library_backing_us_up(self):
@ -69,7 +69,7 @@ class TestConfig(unittest.TestCase):
"""
with patch('glymur.lib.openjp2.OPENJP2', new=None):
with patch('glymur.lib.openjpeg.OPENJPEG', new=None):
with self.assertRaises((IOError, OSError)):
with self.assertRaises(glymur.jp2k.LibraryNotFoundError):
d = glymur.Jp2k(self.jp2file).read_bands()
@unittest.skipIf(os.name == "nt", "NamedTemporaryFile issue on windows")
@ -79,7 +79,7 @@ class TestConfig(unittest.TestCase):
data = glymur.Jp2k(self.j2kfile).read()
with patch('glymur.lib.openjp2.OPENJP2', new=None):
with patch('glymur.lib.openjpeg.OPENJPEG', new=None):
with self.assertRaises((IOError, OSError)):
with self.assertRaises(glymur.jp2k.LibraryNotFoundError):
with tempfile.NamedTemporaryFile(suffix='.jp2') as tfile:
ofile = Jp2k(tfile.name, 'wb')
ofile.write(data)