From 6fd1d6a4f5328662f52714eb14f49f86473a5edd Mon Sep 17 00:00:00 2001 From: John Evans Date: Mon, 27 May 2013 11:28:59 -0400 Subject: [PATCH] Can import without openjp2 library now. --- glymur/__init__.py | 30 ++++--- glymur/lib/openjp2.py | 164 ++++++++++++++++++++------------------- glymur/test/test_jp2k.py | 4 +- 3 files changed, 101 insertions(+), 97 deletions(-) diff --git a/glymur/__init__.py b/glymur/__init__.py index e5f0350..71fd620 100644 --- a/glymur/__init__.py +++ b/glymur/__init__.py @@ -23,8 +23,12 @@ def _glymurrc_fname(): fname = os.path.join(path, 'glymurrc') if os.path.exists(fname): return fname + else: + msg = "glymurrc file hinted at by GLYMURCONFIGDIR does not " + msg += "exist." + warnings.warn(msg, UserWarning) - # HOME/.glymur/jptoolrsc + # HOME/.glymur/glymurrc confdir = _get_configdir() if confdir is not None: fname = os.path.join(_get_configdir(), 'glymurrc') @@ -45,26 +49,19 @@ def _config(): # Read the configuration file for the library location. parser = ConfigParser() parser.read(filename) - try: - libopenjp2_path = parser.get('library', 'openjp2') - if not os.path.exists(libopenjp2_path): - msg = 'OpenJP2 library path specified by configuration file ' - msg += 'does not exist.' - raise ImportError(msg) - except NoOptionError as e: - msg = "Error parsing configuration file '{0}': {1}" - msg = msg.format(fname, e.message) - warnings.warn(msg, UserWarning) - except: - raise + libopenjp2_path = parser.get('library', 'openjp2') else: # No help from the config file, try to find it ourselves. from ctypes.util import find_library libopenjp2_path = find_library('openjp2') - if libopenjp2_path is None: - raise ImportError('OpenJP2 library not found.') - _OPENJP2 = ctypes.CDLL(libopenjp2_path) + try: + _OPENJP2 = ctypes.CDLL(libopenjp2_path) + except OSError: + msg = '"Library {0}" could not be loaded. Operating in degraded mode.' + msg = msg.format(libopenjp2_path) + warnings.warn(msg, UserWarning) + _OPENJP2 = None return _OPENJP2 @@ -81,6 +78,7 @@ def _get_configdir(): if 'HOME' in os.environ: return os.path.join(os.environ['HOME'], '.glymur') +import warnings import sys if sys.hexversion <= 0x03000000: from ConfigParser import SafeConfigParser as ConfigParser diff --git a/glymur/lib/openjp2.py b/glymur/lib/openjp2.py index 5ba07e6..798f23d 100644 --- a/glymur/lib/openjp2.py +++ b/glymur/lib/openjp2.py @@ -528,105 +528,110 @@ class _codestream_info_v2_t(ctypes.Structure): # Restrict the input and output argument types for each function used in the # API. -_OPENJP2.opj_create_compress.argtypes = [_codec_format_t] -_OPENJP2.opj_create_compress.restype = _codec_t_p +if _OPENJP2 is not None: + _OPENJP2.opj_create_compress.argtypes = [_codec_format_t] + _OPENJP2.opj_create_compress.restype = _codec_t_p -_OPENJP2.opj_create_decompress.argtypes = [_codec_format_t] -_OPENJP2.opj_create_decompress.restype = _codec_t_p + _OPENJP2.opj_create_decompress.argtypes = [_codec_format_t] + _OPENJP2.opj_create_decompress.restype = _codec_t_p -_argtypes = [_codec_t_p, _stream_t_p, ctypes.POINTER(_image_t)] -_OPENJP2.opj_decode.argtypes = _argtypes + _argtypes = [_codec_t_p, _stream_t_p, ctypes.POINTER(_image_t)] + _OPENJP2.opj_decode.argtypes = _argtypes -_argtypes = [_codec_t_p, ctypes.c_uint32, - ctypes.POINTER(ctypes.c_uint8), - ctypes.c_uint32, - _stream_t_p] -_OPENJP2.opj_decode_tile_data.argtypes = _argtypes + _argtypes = [_codec_t_p, ctypes.c_uint32, + ctypes.POINTER(ctypes.c_uint8), + ctypes.c_uint32, + _stream_t_p] + _OPENJP2.opj_decode_tile_data.argtypes = _argtypes -_argtypes = [ctypes.POINTER(ctypes.POINTER(_codestream_info_v2_t))] -_OPENJP2.opj_destroy_cstr_info.argtypes = _argtypes -_OPENJP2.opj_destroy_cstr_info.restype = ctypes.c_void_p + _argtypes = [ctypes.POINTER(ctypes.POINTER(_codestream_info_v2_t))] + _OPENJP2.opj_destroy_cstr_info.argtypes = _argtypes + _OPENJP2.opj_destroy_cstr_info.restype = ctypes.c_void_p -_argtypes = [_codec_t_p, _stream_t_p] -_OPENJP2.opj_encode.argtypes = _argtypes + _argtypes = [_codec_t_p, _stream_t_p] + _OPENJP2.opj_encode.argtypes = _argtypes -_OPENJP2.opj_get_cstr_info.argtypes = [_codec_t_p] -_OPENJP2.opj_get_cstr_info.restype = ctypes.POINTER(_codestream_info_v2_t) + _OPENJP2.opj_get_cstr_info.argtypes = [_codec_t_p] + _OPENJP2.opj_get_cstr_info.restype = ctypes.POINTER(_codestream_info_v2_t) -_argtypes = [_codec_t_p, - _stream_t_p, - ctypes.POINTER(_image_t), - ctypes.c_uint32] -_OPENJP2.opj_get_decoded_tile.argtypes = _argtypes + _argtypes = [_codec_t_p, + _stream_t_p, + ctypes.POINTER(_image_t), + ctypes.c_uint32] + _OPENJP2.opj_get_decoded_tile.argtypes = _argtypes -_argtypes = [ctypes.c_uint32, - ctypes.POINTER(_image_comptparm_t), - color_space_t] -_OPENJP2.opj_image_create.argtypes = _argtypes -_OPENJP2.opj_image_create.restype = ctypes.POINTER(_image_t) + _argtypes = [ctypes.c_uint32, + ctypes.POINTER(_image_comptparm_t), + color_space_t] + _OPENJP2.opj_image_create.argtypes = _argtypes + _OPENJP2.opj_image_create.restype = ctypes.POINTER(_image_t) -_argtypes = [ctypes.c_uint32, - ctypes.POINTER(_image_comptparm_t), - color_space_t] -_OPENJP2.opj_image_tile_create.argtypes = _argtypes -_OPENJP2.opj_image_tile_create.restype = ctypes.POINTER(_image_t) + _argtypes = [ctypes.c_uint32, + ctypes.POINTER(_image_comptparm_t), + color_space_t] + _OPENJP2.opj_image_tile_create.argtypes = _argtypes + _OPENJP2.opj_image_tile_create.restype = ctypes.POINTER(_image_t) -_OPENJP2.opj_image_destroy.argtypes = [ctypes.POINTER(_image_t)] + _OPENJP2.opj_image_destroy.argtypes = [ctypes.POINTER(_image_t)] -_argtypes = [_stream_t_p, _codec_t_p, ctypes.POINTER(ctypes.POINTER(_image_t))] -_OPENJP2.opj_read_header.argtypes = _argtypes + _argtypes = [_stream_t_p, _codec_t_p, + ctypes.POINTER(ctypes.POINTER(_image_t))] + _OPENJP2.opj_read_header.argtypes = _argtypes -_argtypes = [_codec_t_p, - _stream_t_p, - ctypes.POINTER(ctypes.c_uint32), ctypes.POINTER(ctypes.c_uint32), - ctypes.POINTER(ctypes.c_int32), ctypes.POINTER(ctypes.c_int32), - ctypes.POINTER(ctypes.c_int32), ctypes.POINTER(ctypes.c_int32), - ctypes.POINTER(ctypes.c_uint32), - ctypes.POINTER(_bool_t)] -_OPENJP2.opj_read_tile_header.argtypes = _argtypes + _argtypes = [_codec_t_p, + _stream_t_p, + ctypes.POINTER(ctypes.c_uint32), + ctypes.POINTER(ctypes.c_uint32), + ctypes.POINTER(ctypes.c_int32), + ctypes.POINTER(ctypes.c_int32), + ctypes.POINTER(ctypes.c_int32), + ctypes.POINTER(ctypes.c_int32), + ctypes.POINTER(ctypes.c_uint32), + ctypes.POINTER(_bool_t)] + _OPENJP2.opj_read_tile_header.argtypes = _argtypes -_argtypes = [_codec_t_p, ctypes.POINTER(_image_t), ctypes.c_int32, - ctypes.c_int32, ctypes.c_int32, ctypes.c_int32] -_OPENJP2.opj_set_decode_area.argtypes = _argtypes + _argtypes = [_codec_t_p, ctypes.POINTER(_image_t), ctypes.c_int32, + ctypes.c_int32, ctypes.c_int32, ctypes.c_int32] + _OPENJP2.opj_set_decode_area.argtypes = _argtypes -_argtypes = [ctypes.POINTER(_cparameters_t)] -_OPENJP2.opj_set_default_encoder_parameters.argtypes = _argtypes + _argtypes = [ctypes.POINTER(_cparameters_t)] + _OPENJP2.opj_set_default_encoder_parameters.argtypes = _argtypes -_argtypes = [ctypes.POINTER(_dparameters_t)] -_OPENJP2.opj_set_default_decoder_parameters.argtypes = _argtypes + _argtypes = [ctypes.POINTER(_dparameters_t)] + _OPENJP2.opj_set_default_decoder_parameters.argtypes = _argtypes -_argtypes = [_codec_t_p, ctypes.c_void_p, ctypes.c_void_p] -_OPENJP2.opj_set_error_handler.argtypes = _argtypes -_OPENJP2.opj_set_info_handler.argtypes = _argtypes -_OPENJP2.opj_set_warning_handler.argtypes = _argtypes + _argtypes = [_codec_t_p, ctypes.c_void_p, ctypes.c_void_p] + _OPENJP2.opj_set_error_handler.argtypes = _argtypes + _OPENJP2.opj_set_info_handler.argtypes = _argtypes + _OPENJP2.opj_set_warning_handler.argtypes = _argtypes -_argtypes = [_codec_t_p, ctypes.POINTER(_dparameters_t)] -_OPENJP2.opj_setup_decoder.argtypes = _argtypes + _argtypes = [_codec_t_p, ctypes.POINTER(_dparameters_t)] + _OPENJP2.opj_setup_decoder.argtypes = _argtypes -_argtypes = [_codec_t_p, - ctypes.POINTER(_cparameters_t), - ctypes.POINTER(_image_t)] -_OPENJP2.opj_setup_encoder.argtypes = _argtypes + _argtypes = [_codec_t_p, + ctypes.POINTER(_cparameters_t), + ctypes.POINTER(_image_t)] + _OPENJP2.opj_setup_encoder.argtypes = _argtypes -_argtypes = [ctypes.c_char_p, ctypes.c_int32] -_OPENJP2.opj_stream_create_default_file_stream_v3.argtypes = _argtypes -_OPENJP2.opj_stream_create_default_file_stream_v3.restype = _stream_t_p + _argtypes = [ctypes.c_char_p, ctypes.c_int32] + _OPENJP2.opj_stream_create_default_file_stream_v3.argtypes = _argtypes + _OPENJP2.opj_stream_create_default_file_stream_v3.restype = _stream_t_p -_argtypes = [_codec_t_p, ctypes.POINTER(_image_t), _stream_t_p] -_OPENJP2.opj_start_compress.argtypes = _argtypes + _argtypes = [_codec_t_p, ctypes.POINTER(_image_t), _stream_t_p] + _OPENJP2.opj_start_compress.argtypes = _argtypes -_OPENJP2.opj_end_compress.argtypes = [_codec_t_p, _stream_t_p] -_OPENJP2.opj_end_decompress.argtypes = [_codec_t_p, _stream_t_p] + _OPENJP2.opj_end_compress.argtypes = [_codec_t_p, _stream_t_p] + _OPENJP2.opj_end_decompress.argtypes = [_codec_t_p, _stream_t_p] -_OPENJP2.opj_stream_destroy_v3.argtypes = [_stream_t_p] -_OPENJP2.opj_destroy_codec.argtypes = [_codec_t_p] + _OPENJP2.opj_stream_destroy_v3.argtypes = [_stream_t_p] + _OPENJP2.opj_destroy_codec.argtypes = [_codec_t_p] -_argtypes = [_codec_t_p, - ctypes.c_uint32, - ctypes.POINTER(ctypes.c_uint8), - ctypes.c_uint32, - _stream_t_p] -_OPENJP2.opj_write_tile.argtypes = _argtypes + _argtypes = [_codec_t_p, + ctypes.c_uint32, + ctypes.POINTER(ctypes.c_uint8), + ctypes.c_uint32, + _stream_t_p] + _OPENJP2.opj_write_tile.argtypes = _argtypes def _check_error(status): @@ -655,9 +660,10 @@ _fcns = ['opj_decode', 'opj_decode_tile_data', 'opj_end_compress', 'opj_set_warning_handler', 'opj_setup_decoder', 'opj_setup_encoder', 'opj_start_compress', 'opj_write_tile'] -for _fcn in _fcns: - _attr = getattr(_OPENJP2, _fcn) - setattr(_attr, 'restype', _check_error) +if _OPENJP2 is not None: + for _fcn in _fcns: + _attr = getattr(_OPENJP2, _fcn) + setattr(_attr, 'restype', _check_error) def _create_compress(codec_format): diff --git a/glymur/test/test_jp2k.py b/glymur/test/test_jp2k.py index 256cade..5234e0b 100644 --- a/glymur/test/test_jp2k.py +++ b/glymur/test/test_jp2k.py @@ -582,7 +582,7 @@ class TestJp2k(unittest.TestCase): with patch.dict('os.environ', {'GLYMURCONFIGDIR': tdir}): # Misconfigured new configuration file should # be rejected. - with self.assertRaises(ImportError) as ce: + with self.assertWarns(UserWarning) as cw: imp.reload(glymur) @unittest.skipIf(sys.hexversion < 0x03020000, @@ -594,7 +594,7 @@ class TestJp2k(unittest.TestCase): with patch.dict('os.environ', {'GLYMURCONFIGDIR': tdir}): # Misconfigured new configuration file should # be rejected. - with self.assertRaises(ImportError) as ce: + with self.assertWarns(UserWarning) as cw: imp.reload(glymur)