From 29dfce792767624c839c7140ec31abf618a9c660 Mon Sep 17 00:00:00 2001 From: jevans Date: Mon, 17 Nov 2014 21:07:36 -0500 Subject: [PATCH 1/5] warn instead of raising IOError if openjpeg library cannot be found Improve error message when trying to read without openjpeg library. --- glymur/jp2k.py | 10 ++++------ glymur/lib/config.py | 2 +- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/glymur/jp2k.py b/glymur/jp2k.py index d460ea6..e4a1a01 100644 --- a/glymur/jp2k.py +++ b/glymur/jp2k.py @@ -937,10 +937,8 @@ class Jp2k(Jp2kBox): elif opj.OPENJPEG is not None: img = self._read_openjpeg(**kwargs) else: - raise LibraryNotFoundError("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 OpenJPEG installed " + "before reading a JPEG2000 image.") return img def _subsampling_sanity_check(self): @@ -1235,8 +1233,8 @@ class Jp2k(Jp2kBox): """ if version.openjpeg_version_tuple[0] < 2: raise LibraryNotFoundError("You must have at least version 2.0.0 " - "of OpenJP2 installed before using " - "this functionality.") + "of OpenJPEG installed before using " + "read_bands.") dparam = self._populate_dparam(rlevel, ignore_pclr_cmap_cdef, layer=layer, tile=tile, area=area) diff --git a/glymur/lib/config.py b/glymur/lib/config.py index 04aece2..dc8ab29 100644 --- a/glymur/lib/config.py +++ b/glymur/lib/config.py @@ -146,7 +146,7 @@ def glymur_config(): libopenjpeg_handle = load_openjpeg(libs['openjpeg']) if libopenjp2_handle is None and libopenjpeg_handle is None: msg = "Neither the openjp2 nor the openjpeg library could be loaded. " - raise IOError(msg) + warnings.warn(msg, UserWarning) return libopenjp2_handle, libopenjpeg_handle From 3fa3072041c4111ebe9dc611f8d826c46538936f Mon Sep 17 00:00:00 2001 From: jevans Date: Mon, 17 Nov 2014 21:07:36 -0500 Subject: [PATCH 2/5] warn instead of raising IOError if openjpeg library cannot be found, issue#300 Improve error message when trying to read without openjpeg library. --- glymur/jp2k.py | 10 ++++------ glymur/lib/config.py | 2 +- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/glymur/jp2k.py b/glymur/jp2k.py index d460ea6..e4a1a01 100644 --- a/glymur/jp2k.py +++ b/glymur/jp2k.py @@ -937,10 +937,8 @@ class Jp2k(Jp2kBox): elif opj.OPENJPEG is not None: img = self._read_openjpeg(**kwargs) else: - raise LibraryNotFoundError("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 OpenJPEG installed " + "before reading a JPEG2000 image.") return img def _subsampling_sanity_check(self): @@ -1235,8 +1233,8 @@ class Jp2k(Jp2kBox): """ if version.openjpeg_version_tuple[0] < 2: raise LibraryNotFoundError("You must have at least version 2.0.0 " - "of OpenJP2 installed before using " - "this functionality.") + "of OpenJPEG installed before using " + "read_bands.") dparam = self._populate_dparam(rlevel, ignore_pclr_cmap_cdef, layer=layer, tile=tile, area=area) diff --git a/glymur/lib/config.py b/glymur/lib/config.py index 04aece2..dc8ab29 100644 --- a/glymur/lib/config.py +++ b/glymur/lib/config.py @@ -146,7 +146,7 @@ def glymur_config(): libopenjpeg_handle = load_openjpeg(libs['openjpeg']) if libopenjp2_handle is None and libopenjpeg_handle is None: msg = "Neither the openjp2 nor the openjpeg library could be loaded. " - raise IOError(msg) + warnings.warn(msg, UserWarning) return libopenjp2_handle, libopenjpeg_handle From d45d74ff5d625b1bde663880485603d3ac232311 Mon Sep 17 00:00:00 2001 From: John Evans Date: Tue, 18 Nov 2014 07:07:14 -0500 Subject: [PATCH 3/5] add test fixture for determining if openjpeg is installed --- glymur/test/fixtures.py | 9 +++++++++ glymur/test/test_jp2k.py | 6 ++++++ 2 files changed, 15 insertions(+) diff --git a/glymur/test/fixtures.py b/glymur/test/fixtures.py index 838756a..417797f 100644 --- a/glymur/test/fixtures.py +++ b/glymur/test/fixtures.py @@ -13,6 +13,15 @@ import six import glymur +# If openjpeg is not installed, many tests cannot be run. +if glymur.version.openjpeg_version == "0.0.0": + OPENJPEG_NOT_AVAILABLE = True + OPENJPEG_NOT_AVAILABLE_MSG = "OpenJPEG library not installed" +else: + OPENJPEG_NOT_AVAILABLE = False + OPENJPEG_NOT_AVAILABLE_MSG = None + + # Some versions of "six" on python3 cause problems when verifying warnings. # Only use when the version is 1.7 or higher. # And moreover, we only test using the 3.x infrastructure, never on 2.x. diff --git a/glymur/test/test_jp2k.py b/glymur/test/test_jp2k.py index 042a681..6a4b5c1 100644 --- a/glymur/test/test_jp2k.py +++ b/glymur/test/test_jp2k.py @@ -54,6 +54,8 @@ def load_tests(loader, tests, ignore): return tests +@unittest.skipIf(fixtures.OPENJPEG_NOT_AVAILABLE, + fixtures.OPENJPEG_NOT_AVAILABLE_MSG) class SliceProtocolBase(unittest.TestCase): """ Test slice protocol, i.e. when using [ ] to read image data. @@ -460,6 +462,8 @@ class TestSliceProtocolOpjData(unittest.TestCase): expected = self.j2k_quarter_data[5:13, 38:50] np.testing.assert_array_equal(actual, expected) +@unittest.skipIf(fixtures.OPENJPEG_NOT_AVAILABLE, + fixtures.OPENJPEG_NOT_AVAILABLE_MSG) class TestJp2k(unittest.TestCase): """These tests should be run by just about all configuration.""" @@ -966,6 +970,8 @@ class TestJp2k_write(unittest.TestCase): self.assertEqual(codestream.segment[2].spcod[0], glymur.core.CPRL) +@unittest.skipIf(fixtures.OPENJPEG_NOT_AVAILABLE, + fixtures.OPENJPEG_NOT_AVAILABLE_MSG) @unittest.skipIf(glymur.version.openjpeg_version_tuple[0] >= 2, "Negative tests only for version 1.x") class TestJp2k_1_x(unittest.TestCase): From d03870cc7287f730d4f3f6c71e3223f48e80cf71 Mon Sep 17 00:00:00 2001 From: John Evans Date: Tue, 18 Nov 2014 07:26:21 -0500 Subject: [PATCH 4/5] filtered out tests that should not run if openjpeg not installed --- glymur/test/test_jp2k.py | 4 ++++ glymur/test/test_opj_suite.py | 7 +++++++ glymur/test/test_opj_suite_2p1.py | 5 +++++ 3 files changed, 16 insertions(+) diff --git a/glymur/test/test_jp2k.py b/glymur/test/test_jp2k.py index 6a4b5c1..7e168ab 100644 --- a/glymur/test/test_jp2k.py +++ b/glymur/test/test_jp2k.py @@ -311,6 +311,8 @@ class TestSliceProtocolRead(SliceProtocolBase): expected = self.j2k.read(area=(5, 27, 533, 423), rlevel=5) np.testing.assert_array_equal(actual, expected) +@unittest.skipIf(fixtures.OPENJPEG_NOT_AVAILABLE, + fixtures.OPENJPEG_NOT_AVAILABLE_MSG) @unittest.skipIf(OPJ_DATA_ROOT is None, "OPJ_DATA_ROOT environment variable not set") class TestSliceProtocolOpjData(unittest.TestCase): @@ -1296,6 +1298,8 @@ class TestJp2kOpjDataRoot(unittest.TestCase): rgb_from_idx[r, c] = palette[idx[r, c]] np.testing.assert_array_equal(rgb, rgb_from_idx) + @unittest.skipIf(fixtures.OPENJPEG_NOT_AVAILABLE, + fixtures.OPENJPEG_NOT_AVAILABLE_MSG) def test_read_differing_subsamples(self): """should error out with read used on differently subsampled images""" # Verify that we error out appropriately if we use the read method diff --git a/glymur/test/test_opj_suite.py b/glymur/test/test_opj_suite.py index a033aae..8eb5956 100644 --- a/glymur/test/test_opj_suite.py +++ b/glymur/test/test_opj_suite.py @@ -37,6 +37,7 @@ import glymur from glymur import Jp2k from glymur.jp2box import FileTypeBox, ImageHeaderBox, ColourSpecificationBox +from . import fixtures from .fixtures import ( OPJ_DATA_ROOT, MetadataBase, WARNING_INFRASTRUCTURE_ISSUE, WARNING_INFRASTRUCTURE_MSG, @@ -44,6 +45,8 @@ from .fixtures import ( ) +@unittest.skipIf(fixtures.OPENJPEG_NOT_AVAILABLE, + fixtures.OPENJPEG_NOT_AVAILABLE_MSG) @unittest.skipIf(OPJ_DATA_ROOT is None, "OPJ_DATA_ROOT environment variable not set") class TestSuite(unittest.TestCase): @@ -479,6 +482,8 @@ class TestSuiteWarns(MetadataBase): self.assertTrue(True) +@unittest.skipIf(fixtures.OPENJPEG_NOT_AVAILABLE, + fixtures.OPENJPEG_NOT_AVAILABLE_MSG) @unittest.skipIf(OPJ_DATA_ROOT is None, "OPJ_DATA_ROOT environment variable not set") @unittest.skipIf(glymur.version.openjpeg_version_tuple[0] == 1, @@ -574,6 +579,8 @@ class TestSuiteBands(unittest.TestCase): self.assertTrue(True) +@unittest.skipIf(fixtures.OPENJPEG_NOT_AVAILABLE, + fixtures.OPENJPEG_NOT_AVAILABLE_MSG) @unittest.skipIf(OPJ_DATA_ROOT is None, "OPJ_DATA_ROOT environment variable not set") @unittest.skipIf(glymur.version.openjpeg_version_tuple[0] == 1, diff --git a/glymur/test/test_opj_suite_2p1.py b/glymur/test/test_opj_suite_2p1.py index addc48b..4977a6e 100644 --- a/glymur/test/test_opj_suite_2p1.py +++ b/glymur/test/test_opj_suite_2p1.py @@ -36,11 +36,14 @@ import numpy as np from glymur import Jp2k import glymur +from . import fixtures from .fixtures import OPJ_DATA_ROOT from .fixtures import WARNING_INFRASTRUCTURE_ISSUE, WARNING_INFRASTRUCTURE_MSG from .fixtures import mse, peak_tolerance, read_pgx, opj_data_file +@unittest.skipIf(fixtures.OPENJPEG_NOT_AVAILABLE, + fixtures.OPENJPEG_NOT_AVAILABLE_MSG) @unittest.skipIf(OPJ_DATA_ROOT is None, "OPJ_DATA_ROOT environment variable not set") @unittest.skipIf(re.match(r'''(1|2.0.0)''', @@ -163,6 +166,8 @@ class TestSuite2point1(unittest.TestCase): with self.assertRaises(IOError): j.read() +@unittest.skipIf(fixtures.OPENJPEG_NOT_AVAILABLE, + fixtures.OPENJPEG_NOT_AVAILABLE_MSG) @unittest.skipIf(OPJ_DATA_ROOT is None, "OPJ_DATA_ROOT environment variable not set") @unittest.skipIf(re.match(r'''(1|2.0.0)''', From 982e70be79923f71848f5ed29830ba6628627d97 Mon Sep 17 00:00:00 2001 From: John Evans Date: Tue, 18 Nov 2014 11:32:55 -0500 Subject: [PATCH 5/5] filtered out tests that should not run on linux where no openjpeg --- glymur/test/test_jp2box.py | 5 ++++- glymur/test/test_jp2k.py | 6 +++--- glymur/test/test_printing.py | 4 ++++ 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/glymur/test/test_jp2box.py b/glymur/test/test_jp2box.py index 061ad23..bf10900 100644 --- a/glymur/test/test_jp2box.py +++ b/glymur/test/test_jp2box.py @@ -34,6 +34,7 @@ from glymur.jp2box import FileTypeBox, ImageHeaderBox, JP2HeaderBox from glymur.jp2box import JPEG2000SignatureBox from glymur.core import COLOR, OPACITY from glymur.core import RED, GREEN, BLUE, GREY, WHOLE_IMAGE +from glymur.version import openjpeg_version from .fixtures import ( WARNING_INFRASTRUCTURE_ISSUE, WARNING_INFRASTRUCTURE_MSG, @@ -54,6 +55,8 @@ def load_tests(loader, tests, ignore): tests.addTests(doctest.DocTestSuite('glymur.jp2box')) return tests +@unittest.skipIf(re.match('0|1.[0-2]', openjpeg_version) is not None, + "Not supported with OpenJPEG {0}".format(openjpeg_version)) @unittest.skipIf(os.name == "nt", "Temporary file issue on window.") class TestDataEntryURL(unittest.TestCase): """Test suite for DataEntryURL boxes.""" @@ -119,7 +122,7 @@ class TestDataEntryURL(unittest.TestCase): self.assertEqual(url + chr(0), read_url) -@unittest.skipIf(re.match(r'''(1|2.0.0)''', +@unittest.skipIf(re.match(r'''0|1|2.0.0''', glymur.version.openjpeg_version) is not None, "Not supported until 2.1") @unittest.skipIf(os.name == "nt", "Temporary file issue on window.") diff --git a/glymur/test/test_jp2k.py b/glymur/test/test_jp2k.py index 7e168ab..02e850a 100644 --- a/glymur/test/test_jp2k.py +++ b/glymur/test/test_jp2k.py @@ -494,7 +494,7 @@ class TestJp2k(unittest.TestCase): actdata = j2.read() self.assertTrue(fixtures.mse(actdata[0], expdata[0]) < 0.38) - @unittest.skipIf(re.match('1.[0-4]', openjpeg_version) is not None, + @unittest.skipIf(re.match('0|1.[0-4]', openjpeg_version) is not None, "Not supported with OpenJPEG {0}".format(openjpeg_version)) @unittest.skipIf(re.match('1.5.(1|2)', openjpeg_version) is not None, "Mysteriously fails in 1.5.1 and 1.5.2") @@ -826,7 +826,7 @@ class TestJp2k(unittest.TestCase): self.assertEqual(data.shape, (1024, 1024, 3)) -@unittest.skipIf(re.match('1.[0-4]', openjpeg_version) is not None, +@unittest.skipIf(re.match('0|1.[0-4]', openjpeg_version) is not None, "Not supported with OpenJPEG {0}".format(openjpeg_version)) @unittest.skipIf(os.name == "nt", "NamedTemporaryFile issue on windows") class TestJp2k_write(unittest.TestCase): @@ -1100,7 +1100,7 @@ class TestJp2k_2_0(unittest.TestCase): self.assertEqual(jasoc.box[3].box[1].box_id, 'xml ') -@unittest.skipIf(re.match(r'''(1|2.0.0)''', +@unittest.skipIf(re.match(r'''0|1|2.0.0''', glymur.version.openjpeg_version) is not None, "Not to be run until unless 2.0.1 or higher is present") class TestJp2k_2_1(unittest.TestCase): diff --git a/glymur/test/test_printing.py b/glymur/test/test_printing.py index 49bbe6d..187a15b 100644 --- a/glymur/test/test_printing.py +++ b/glymur/test/test_printing.py @@ -31,6 +31,8 @@ import lxml.etree as ET import glymur from glymur import Jp2k, command_line +from glymur.version import openjpeg_version + from . import fixtures from .fixtures import OPJ_DATA_ROOT, opj_data_file from .fixtures import WARNING_INFRASTRUCTURE_ISSUE, WARNING_INFRASTRUCTURE_MSG @@ -106,6 +108,8 @@ class TestPrinting(unittest.TestCase): with self.assertRaises(TypeError): glymur.set_printoptions(hi='low') + @unittest.skipIf(re.match('0|1.[0-4]', openjpeg_version) is not None, + "Not supported with OpenJPEG {0}".format(openjpeg_version)) def test_asoc_label_box(self): """verify printing of asoc, label boxes""" # Construct a fake file with an asoc and a label box, as