cleaned up config determination a bit.

This commit is contained in:
John Evans 2013-07-10 05:32:38 -04:00
commit e6032a8b80

View file

@ -10,8 +10,10 @@ 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
from ConfigParser import NoOptionError
else: else:
from configparser import ConfigParser from configparser import ConfigParser
from configparser import NoOptionError
def glymurrc_fname(): def glymurrc_fname():
@ -28,7 +30,6 @@ def glymurrc_fname():
if os.path.exists(fname): if os.path.exists(fname):
return fname return fname
# Either GLYMURCONFIGDIR/glymurrc or $HOME/.glymur/glymurrc
confdir = get_configdir() confdir = get_configdir()
if confdir is not None: if confdir is not None:
fname = os.path.join(confdir, 'glymurrc') fname = os.path.join(confdir, 'glymurrc')
@ -39,10 +40,12 @@ def glymurrc_fname():
return None return None
def get_openjpeg_config(): def load_openjpeg(libopenjpeg_path):
""" Try to find openjpeg library on the system path first. """Load the openjpeg library, falling back on defaults if necessary.
""" """
libopenjpeg_path = find_library('openjpeg') if libopenjpeg_path is None:
# Let ctypes try to find it.
libopenjpeg_path = find_library('openjpeg')
# If we could not find it, then look in some likely locations. # If we could not find it, then look in some likely locations.
if libopenjpeg_path is None: if libopenjpeg_path is None:
@ -74,25 +77,37 @@ def get_openjpeg_config():
return openjpeg_lib return openjpeg_lib
def get_openjp2_config(): def read_config_file():
""" """
We expect to not find openjp2 on the system path since the only version We expect to not find openjp2 on the system path since the only version
that we currently care about is still in the svn trunk at openjpeg.org. that we currently care about is still in the svn trunk at openjpeg.org.
We must use a configuration file that the user must write. We must use a configuration file that the user must write.
""" """
lib = {}
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)
libopenjp2_path = parser.get('library', 'openjp2') try:
else: lib['openjp2'] = parser.get('library', 'openjp2')
except NoOptionError:
lib['openjp2'] = None
try:
lib['openjpeg'] = parser.get('library', 'openjpeg')
except NoOptionError:
lib['openjpeg'] = None
return lib
def load_openjp2(libopenjp2_path):
"""Load the openjp2 library, falling back on defaults if necessary.
"""
if libopenjp2_path is None:
# No help from the config file, try to find it ourselves. # No help from the config file, try to find it ourselves.
libopenjp2_path = find_library('openjp2') libopenjp2_path = find_library('openjp2')
if libopenjp2_path is None:
return None
try: try:
if os.name == "nt": if os.name == "nt":
openjp2_lib = ctypes.windll.LoadLibrary(libopenjp2_path) openjp2_lib = ctypes.windll.LoadLibrary(libopenjp2_path)
@ -103,15 +118,16 @@ def get_openjp2_config():
msg = msg.format(libopenjp2_path) msg = msg.format(libopenjp2_path)
warnings.warn(msg, UserWarning) warnings.warn(msg, UserWarning)
openjp2_lib = None openjp2_lib = None
return openjp2_lib
return openjp2_lib
def glymur_config(): def glymur_config():
"""Try to ascertain locations of openjp2, openjpeg libraries. """Try to ascertain locations of openjp2, openjpeg libraries.
""" """
openjp2_lib = get_openjp2_config() libs = read_config_file()
openjpeg_lib = get_openjpeg_config() libopenjp2_handle = load_openjp2(libs['openjp2'])
return openjp2_lib, openjpeg_lib libopenjpeg_handle = load_openjpeg(libs['openjpeg'])
return libopenjp2_handle, libopenjpeg_handle
def get_configdir(): def get_configdir():