refactored configuration file handling, closes #265
This commit is contained in:
parent
19b0533764
commit
60dcdfe9c5
1 changed files with 52 additions and 57 deletions
|
|
@ -18,6 +18,19 @@ else:
|
||||||
from configparser import ConfigParser
|
from configparser import ConfigParser
|
||||||
from configparser import NoOptionError
|
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():
|
def glymurrc_fname():
|
||||||
"""Return the path to the configuration file.
|
"""Return the path to the configuration file.
|
||||||
|
|
@ -42,30 +55,23 @@ def glymurrc_fname():
|
||||||
# didn't find a configuration file.
|
# didn't find a configuration file.
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
def load_openjpeg_library(libname):
|
||||||
|
|
||||||
def load_openjpeg(path):
|
path = read_config_file(libname)
|
||||||
"""Load the openjpeg library, falling back on defaults if necessary.
|
if path is not None:
|
||||||
|
return load_library_handle(path)
|
||||||
|
|
||||||
Parameters
|
# No location specified by the configuration file, must look for it
|
||||||
----------
|
# elsewhere.
|
||||||
path : str
|
path = find_library(libname)
|
||||||
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:
|
if path is None:
|
||||||
# Could not find a library via ctypes
|
# Could not find a library via ctypes
|
||||||
if platform.system() == 'Darwin':
|
if platform.system() == 'Darwin':
|
||||||
# MacPorts
|
# MacPorts
|
||||||
path = '/opt/local/lib/libopenjpeg.dylib'
|
path = _macports_default_location[libname]
|
||||||
elif os.name == 'nt':
|
elif os.name == 'nt':
|
||||||
path = os.path.join('C:\\', 'Program files', 'OpenJPEG 1.5',
|
path = _windows_default_location[libname]
|
||||||
'bin', 'openjpeg.dll')
|
|
||||||
|
|
||||||
if path is not None and not os.path.exists(path):
|
if path is not None and not os.path.exists(path):
|
||||||
# the mac/win default location does not exist.
|
# the mac/win default location does not exist.
|
||||||
|
|
@ -73,29 +79,6 @@ def load_openjpeg(path):
|
||||||
|
|
||||||
return load_library_handle(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):
|
def load_library_handle(path):
|
||||||
"""Load the library, return the ctypes handle."""
|
"""Load the library, return the ctypes handle."""
|
||||||
|
|
||||||
|
|
@ -119,36 +102,48 @@ def load_library_handle(path):
|
||||||
return opj_lib
|
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()
|
filename = glymurrc_fname()
|
||||||
if filename is not None:
|
if filename is not None:
|
||||||
# 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)
|
||||||
for name in ['openjp2', 'openjpeg']:
|
try:
|
||||||
try:
|
path = parser.get('library', libname)
|
||||||
lib[name] = parser.get('library', name)
|
except NoOptionError:
|
||||||
except NoOptionError:
|
path = None
|
||||||
pass
|
|
||||||
|
|
||||||
return lib
|
|
||||||
|
|
||||||
|
return path
|
||||||
|
|
||||||
def glymur_config():
|
def glymur_config():
|
||||||
"""Try to ascertain locations of openjp2, openjpeg libraries.
|
|
||||||
"""
|
"""
|
||||||
libs = read_config_file()
|
Try to ascertain locations of openjp2, openjpeg libraries.
|
||||||
libopenjp2_handle = load_openjp2(libs['openjp2'])
|
|
||||||
libopenjpeg_handle = load_openjpeg(libs['openjpeg'])
|
Returns
|
||||||
if libopenjp2_handle is None and libopenjpeg_handle is None:
|
-------
|
||||||
|
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. "
|
msg = "Neither the openjp2 nor the openjpeg library could be loaded. "
|
||||||
raise IOError(msg)
|
raise IOError(msg)
|
||||||
return libopenjp2_handle, libopenjpeg_handle
|
return tuple(lst)
|
||||||
|
|
||||||
|
|
||||||
def get_configdir():
|
def get_configdir():
|
||||||
"""Return string representing the configuration directory.
|
"""Return string representing the configuration directory.
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue