diff --git a/glymur/lib/config.py b/glymur/lib/config.py index 04aece2..a475233 100644 --- a/glymur/lib/config.py +++ b/glymur/lib/config.py @@ -18,6 +18,19 @@ else: from configparser import ConfigParser from configparser import NoOptionError +# default library locations for MacPorts +_macports_default_location = { + 'openjp2': '/opt/local/lib/libopenjp2.dylib', + 'openjpeg': '/opt/local/lib/libopenjpeg.dylib' +} + +# default library locations on Windows +_windows_default_location = { + 'openjp2': os.path.join('C:\\', 'Program files', 'OpenJPEG 2.0', + 'bin', 'openjp2.dll'), + 'openjpeg': os.path.join('C:\\', 'Program files', 'OpenJPEG 1.5', + 'bin', 'openjpeg.dll') +} def glymurrc_fname(): """Return the path to the configuration file. @@ -42,30 +55,23 @@ def glymurrc_fname(): # didn't find a configuration file. return None +def load_openjpeg_library(libname): + + path = read_config_file(libname) + if path is not None: + return load_library_handle(path) -def load_openjpeg(path): - """Load the openjpeg library, falling back on defaults if necessary. + # No location specified by the configuration file, must look for it + # elsewhere. + path = find_library(libname) - Parameters - ---------- - path : str - Path to openjpeg 1.5 library as specified by configuration file. Will - be None if no configuration file specified. - """ - if path is None: - # Let ctypes try to find it. - path = find_library('openjpeg') - - # If we could not find it, then look in some likely locations on mac - # and win. if path is None: # Could not find a library via ctypes if platform.system() == 'Darwin': # MacPorts - path = '/opt/local/lib/libopenjpeg.dylib' + path = _macports_default_location[libname] elif os.name == 'nt': - path = os.path.join('C:\\', 'Program files', 'OpenJPEG 1.5', - 'bin', 'openjpeg.dll') + path = _windows_default_location[libname] if path is not None and not os.path.exists(path): # the mac/win default location does not exist. @@ -73,29 +79,6 @@ def load_openjpeg(path): return load_library_handle(path) -def load_openjp2(path): - """Load the openjp2 library, falling back on defaults if necessary. - """ - if path is None: - # No help from the config file, try to find it via ctypes. - path = find_library('openjp2') - - if path is None: - # Could not find a library via ctypes - if platform.system() == 'Darwin': - # MacPorts - path = '/opt/local/lib/libopenjp2.dylib' - elif os.name == 'nt': - path = os.path.join('C:\\', 'Program files', 'OpenJPEG 2.0', - 'bin', 'openjp2.dll') - - if path is not None and not os.path.exists(path): - # the mac/win default location does not exist. - return None - - return load_library_handle(path) - - def load_library_handle(path): """Load the library, return the ctypes handle.""" @@ -119,36 +102,48 @@ def load_library_handle(path): return opj_lib -def read_config_file(): +def read_config_file(libname): """ - We must use a configuration file that the user must write. + Extract library locations from a configuration file. + + Parameters + ---------- + libname : str + One of either 'openjp2' or 'openjpeg' + + Returns + ------- + path : None or str + None if no location is specified, otherwise a path to the library """ - lib = {'openjp2': None, 'openjpeg': None} filename = glymurrc_fname() if filename is not None: # Read the configuration file for the library location. parser = ConfigParser() parser.read(filename) - for name in ['openjp2', 'openjpeg']: - try: - lib[name] = parser.get('library', name) - except NoOptionError: - pass - - return lib + try: + path = parser.get('library', libname) + except NoOptionError: + path = None + return path def glymur_config(): - """Try to ascertain locations of openjp2, openjpeg libraries. """ - libs = read_config_file() - libopenjp2_handle = load_openjp2(libs['openjp2']) - libopenjpeg_handle = load_openjpeg(libs['openjpeg']) - if libopenjp2_handle is None and libopenjpeg_handle is None: + Try to ascertain locations of openjp2, openjpeg libraries. + + Returns + ------- + tpl : tuple + tuple of library handles + """ + lst = [] + for libname in ['openjp2', 'openjpeg']: + lst.append(load_openjpeg_library(libname)) + if all(handle is None for handle in lst): msg = "Neither the openjp2 nor the openjpeg library could be loaded. " raise IOError(msg) - return libopenjp2_handle, libopenjpeg_handle - + return tuple(lst) def get_configdir(): """Return string representing the configuration directory.