Merge branch 'issue4' into devel

This commit is contained in:
John Evans 2013-05-27 11:29:42 -04:00
commit ce49078d94
3 changed files with 101 additions and 97 deletions

View file

@ -23,8 +23,12 @@ def _glymurrc_fname():
fname = os.path.join(path, 'glymurrc') fname = os.path.join(path, 'glymurrc')
if os.path.exists(fname): if os.path.exists(fname):
return 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() confdir = _get_configdir()
if confdir is not None: if confdir is not None:
fname = os.path.join(_get_configdir(), 'glymurrc') fname = os.path.join(_get_configdir(), 'glymurrc')
@ -45,26 +49,19 @@ def _config():
# Read the configuration file for the library location. # Read the configuration file for the library location.
parser = ConfigParser() parser = ConfigParser()
parser.read(filename) parser.read(filename)
try:
libopenjp2_path = parser.get('library', 'openjp2') 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
else: else:
# No help from the config file, try to find it ourselves. # No help from the config file, try to find it ourselves.
from ctypes.util import find_library from ctypes.util import find_library
libopenjp2_path = find_library('openjp2') libopenjp2_path = find_library('openjp2')
if libopenjp2_path is None:
raise ImportError('OpenJP2 library not found.')
try:
_OPENJP2 = ctypes.CDLL(libopenjp2_path) _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 return _OPENJP2
@ -81,6 +78,7 @@ def _get_configdir():
if 'HOME' in os.environ: if 'HOME' in os.environ:
return os.path.join(os.environ['HOME'], '.glymur') return os.path.join(os.environ['HOME'], '.glymur')
import warnings
import sys import sys
if sys.hexversion <= 0x03000000: if sys.hexversion <= 0x03000000:
from ConfigParser import SafeConfigParser as ConfigParser from ConfigParser import SafeConfigParser as ConfigParser

View file

@ -528,6 +528,7 @@ class _codestream_info_v2_t(ctypes.Structure):
# Restrict the input and output argument types for each function used in the # Restrict the input and output argument types for each function used in the
# API. # API.
if _OPENJP2 is not None:
_OPENJP2.opj_create_compress.argtypes = [_codec_format_t] _OPENJP2.opj_create_compress.argtypes = [_codec_format_t]
_OPENJP2.opj_create_compress.restype = _codec_t_p _OPENJP2.opj_create_compress.restype = _codec_t_p
@ -573,14 +574,18 @@ _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))] _argtypes = [_stream_t_p, _codec_t_p,
ctypes.POINTER(ctypes.POINTER(_image_t))]
_OPENJP2.opj_read_header.argtypes = _argtypes _OPENJP2.opj_read_header.argtypes = _argtypes
_argtypes = [_codec_t_p, _argtypes = [_codec_t_p,
_stream_t_p, _stream_t_p,
ctypes.POINTER(ctypes.c_uint32), ctypes.POINTER(ctypes.c_uint32), ctypes.POINTER(ctypes.c_uint32),
ctypes.POINTER(ctypes.c_int32), ctypes.POINTER(ctypes.c_int32), 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_int32),
ctypes.POINTER(ctypes.c_int32),
ctypes.POINTER(ctypes.c_uint32), ctypes.POINTER(ctypes.c_uint32),
ctypes.POINTER(_bool_t)] ctypes.POINTER(_bool_t)]
_OPENJP2.opj_read_tile_header.argtypes = _argtypes _OPENJP2.opj_read_tile_header.argtypes = _argtypes
@ -655,6 +660,7 @@ _fcns = ['opj_decode', 'opj_decode_tile_data', 'opj_end_compress',
'opj_set_warning_handler', 'opj_set_warning_handler',
'opj_setup_decoder', 'opj_setup_encoder', 'opj_start_compress', 'opj_setup_decoder', 'opj_setup_encoder', 'opj_start_compress',
'opj_write_tile'] 'opj_write_tile']
if _OPENJP2 is not None:
for _fcn in _fcns: for _fcn in _fcns:
_attr = getattr(_OPENJP2, _fcn) _attr = getattr(_OPENJP2, _fcn)
setattr(_attr, 'restype', _check_error) setattr(_attr, 'restype', _check_error)

View file

@ -582,7 +582,7 @@ class TestJp2k(unittest.TestCase):
with patch.dict('os.environ', {'GLYMURCONFIGDIR': tdir}): with patch.dict('os.environ', {'GLYMURCONFIGDIR': tdir}):
# Misconfigured new configuration file should # Misconfigured new configuration file should
# be rejected. # be rejected.
with self.assertRaises(ImportError) as ce: with self.assertWarns(UserWarning) as cw:
imp.reload(glymur) imp.reload(glymur)
@unittest.skipIf(sys.hexversion < 0x03020000, @unittest.skipIf(sys.hexversion < 0x03020000,
@ -594,7 +594,7 @@ class TestJp2k(unittest.TestCase):
with patch.dict('os.environ', {'GLYMURCONFIGDIR': tdir}): with patch.dict('os.environ', {'GLYMURCONFIGDIR': tdir}):
# Misconfigured new configuration file should # Misconfigured new configuration file should
# be rejected. # be rejected.
with self.assertRaises(ImportError) as ce: with self.assertWarns(UserWarning) as cw:
imp.reload(glymur) imp.reload(glymur)