From 5de1b8b240a95fbddb31495af4dddc9a2116ed34 Mon Sep 17 00:00:00 2001 From: jevans Date: Sun, 19 Oct 2014 11:24:23 -0400 Subject: [PATCH] remove LibraryNotFoundError, use RuntimeError instead, closes #274, #278 LibraryNotFoundError isn't needed because we already error out during the import of glymur if neither openjpeg nor openjp2 can be found. When read_bands is used and the library version is not at least 2.0.0, use RuntimeError instead of LibraryNotFoundError. --- glymur/jp2k.py | 31 +++---------------------------- glymur/test/test_config.py | 33 --------------------------------- glymur/test/test_jp2k.py | 5 +++++ 3 files changed, 8 insertions(+), 61 deletions(-) diff --git a/glymur/jp2k.py b/glymur/jp2k.py index b7b699c..0bda856 100644 --- a/glymur/jp2k.py +++ b/glymur/jp2k.py @@ -406,19 +406,11 @@ class Jp2k(Jp2kBox): >>> tfile = NamedTemporaryFile(suffix='.jp2', delete=False) >>> j = Jp2k(tfile.name, mode='wb') >>> j.write(data.astype(np.uint8)) - - Raises - ------ - glymur.jp2k.LibraryNotFoundError - if glymur is unable to load an openjpeg library suitable for writing """ if re.match("1.[0-4]", version.openjpeg_version) is not None: raise RuntimeError("You must have at least version 1.5 of OpenJPEG " "in order to write images.") - if opj2.OPENJP2 is None and opj.OPENJPEG is None: - raise LibraryNotFoundError("Cannot load the OpenJPEG library.") - self._determine_colorspace(img_array, **kwargs) cparams = self._populate_cparams(img_array, **kwargs) @@ -962,8 +954,6 @@ class Jp2k(Jp2kBox): Raises ------ - glymur.jp2k.LibraryNotFoundError - if glymur is unable to load an openjpeg library suitable for reading IOError If the image has differing subsample factors. @@ -982,9 +972,6 @@ class Jp2k(Jp2kBox): >>> thumbnail.shape (728, 1296, 3) """ - if opj2.OPENJP2 is None and opj.OPENJPEG is None: - raise LibraryNotFoundError("Cannot load the OpenJPEG library.") - if opj2.OPENJP2 is not None: img = self._read_openjp2(**kwargs) else: @@ -1275,16 +1262,11 @@ class Jp2k(Jp2kBox): >>> jfile = glymur.data.nemo() >>> jp = glymur.Jp2k(jfile) >>> components_lst = jp.read_bands(rlevel=1) - - Raises - ------ - glymur.jp2k.LibraryNotFoundError - if glymur is unable to load an openjpeg library suitable for reading """ if version.openjpeg_version_tuple[0] < 2: - raise LibraryNotFoundError("You must have at least version 2.0.0 " - "of OpenJPEG installed before using " - "this functionality.") + raise RuntimeError("You must have at least version 2.0.0 of " + "OpenJPEG installed before using this " + "functionality.") dparam = self._populate_dparam(rlevel, ignore_pclr_cmap_cdef, layer=layer, tile=tile, area=area) @@ -1835,10 +1817,3 @@ def _default_warning_handler(library_msg, _): _ERROR_CALLBACK = _CMPFUNC(_default_error_handler) _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) diff --git a/glymur/test/test_config.py b/glymur/test/test_config.py index 477e2e3..37db88d 100644 --- a/glymur/test/test_config.py +++ b/glymur/test/test_config.py @@ -108,36 +108,3 @@ class TestConfig(unittest.TestCase): def tearDown(self): pass - def test_read_without_library(self): - """Don't have either openjp2 or openjpeg libraries? Must error out. - """ - with patch('glymur.lib.openjp2.OPENJP2', new=None): - with patch('glymur.lib.openjpeg.OPENJPEG', new=None): - with self.assertRaises(glymur.jp2k.LibraryNotFoundError): - glymur.Jp2k(self.jp2file).read() - - def test_read_bands_without_library(self): - """Don't have openjp2 library? Must error out. - """ - with patch('glymur.lib.openjp2.OPENJP2', new=None): - with patch('glymur.lib.openjpeg.OPENJPEG', new=None): - with patch('glymur.version.openjpeg_version_tuple', - new=(0, 0, 0)): - with self.assertRaises(glymur.jp2k.LibraryNotFoundError): - glymur.Jp2k(self.jp2file).read_bands() - - @unittest.skipIf(os.name == "nt", WINDOWS_TMP_FILE_MSG) - def test_write_without_library(self): - """Don't have openjpeg libraries? Must error out. - """ - 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(glymur.jp2k.LibraryNotFoundError): - with tempfile.NamedTemporaryFile(suffix='.jp2') as tfile: - ofile = Jp2k(tfile.name, 'wb') - ofile.write(data) - - -if __name__ == "__main__": - unittest.main() diff --git a/glymur/test/test_jp2k.py b/glymur/test/test_jp2k.py index 48c0fdb..3800f44 100644 --- a/glymur/test/test_jp2k.py +++ b/glymur/test/test_jp2k.py @@ -824,6 +824,11 @@ class TestJp2k(unittest.TestCase): data = jpx.read() self.assertEqual(data.shape, (1024, 1024, 3)) + def test_read_bands_without_openjp2(self): + """Don't have openjp2 library? Must error out.""" + with patch('glymur.version.openjpeg_version_tuple', new=(1, 5, 0)): + with self.assertRaises(RuntimeError): + glymur.Jp2k(self.jp2file).read_bands() @unittest.skipIf(re.match('1.[0-4]', openjpeg_version) is not None, "Not supported with OpenJPEG {0}".format(openjpeg_version))