Abstracted out library configuration into separate config module. #71

This commit is contained in:
jevans 2013-07-09 18:20:12 -04:00
commit a9ef89fbf0
3 changed files with 128 additions and 135 deletions

124
glymur/lib/config.py Normal file
View file

@ -0,0 +1,124 @@
"""
Configure glymur to use installed libraries if possible.
"""
import ctypes
from ctypes.util import find_library
import os
import platform
import sys
if sys.hexversion <= 0x03000000:
from ConfigParser import SafeConfigParser as ConfigParser
else:
from configparser import ConfigParser
def glymurrc_fname():
"""Return the path to the configuration file.
Search order:
1) current working directory
2) environ var XDG_CONFIG_HOME
3) $HOME/.config/glymur/glymurrc
"""
# Current directory.
fname = os.path.join(os.getcwd(), 'glymurrc')
if os.path.exists(fname):
return fname
# Either GLYMURCONFIGDIR/glymurrc or $HOME/.glymur/glymurrc
confdir = get_configdir()
if confdir is not None:
fname = os.path.join(confdir, 'glymurrc')
if os.path.exists(fname):
return fname
# didn't find a configuration file.
return None
def get_openjpeg_config():
libopenjpeg_path = find_library('openjpeg')
# If we could not find it, then look in some likely locations.
if libopenjpeg_path is None:
if platform.system() == 'Darwin':
path = '/opt/local/lib/libopenjpeg.dylib'
if os.path.exists(path):
libopenjpeg_path = path
elif os.name == 'nt':
path = os.path.join('C:\\', 'Program files', 'OpenJPEG 1.5',
'bin', 'openjpeg.dll')
if os.path.exists(path):
libopenjpeg_path = path
try:
if os.name == "nt":
openjpeg_lib = ctypes.windll.LoadLibrary(libopenjpeg_path)
else:
openjpeg_lib = ctypes.CDLL(libopenjpeg_path)
except OSError:
openjpeg_lib = None
if openjpeg_lib is not None:
# Must be at least 1.5.0
openjpeg_lib.opj_version.restype = ctypes.c_char_p
v = openjpeg_lib.opj_version()
v = v.decode('utf-8')
major, minor, patch = v.split('.')
if minor != '5':
openjpeg_lib = None
return openjpeg_lib
def get_openjp2_config():
filename = glymurrc_fname()
if filename is not None:
# Read the configuration file for the library location.
parser = ConfigParser()
parser.read(filename)
libopenjp2_path = parser.get('library', 'openjp2')
else:
# No help from the config file, try to find it ourselves.
libopenjp2_path = find_library('openjp2')
if libopenjp2_path is None:
return None
try:
if os.name == "nt":
openjp2_lib = ctypes.windll.LoadLibrary(libopenjp2_path)
else:
openjp2_lib = 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_lib = None
return openjp2_lib
def glymur_config():
"""Try to ascertain locations of openjp2, openjpeg libraries.
"""
openjp2_lib = get_openjp2_config()
openjpeg_lib = get_openjpeg_config()
return openjp2_lib, openjpeg_lib
def get_configdir():
"""Return string representing the configuration directory.
Default is $HOME/.config/glymur. You can override this with the
XDG_CONFIG_HOME environment variable.
"""
if 'XDG_CONFIG_HOME' in os.environ:
return os.path.join(os.environ['XDG_CONFIG_HOME'], 'glymur')
if 'HOME' in os.environ:
return os.path.join(os.environ['HOME'], '.config', 'glymur')
if 'USERPROFILE' in os.environ:
# Windows?
return os.path.join(os.environ['USERPROFILE'], 'Application Data', 'glymur')

View file

@ -2,134 +2,11 @@
Wraps individual functions in openjp2 library.
"""
import ctypes
import platform
from ctypes.util import find_library
def glymurrc_fname():
"""Return the path to the configuration file.
Search order:
1) current working directory
2) environ var XDG_CONFIG_HOME
3) $HOME/.config/glymur/glymurrc
"""
# Current directory.
fname = os.path.join(os.getcwd(), 'glymurrc')
if os.path.exists(fname):
return fname
# Either GLYMURCONFIGDIR/glymurrc or $HOME/.glymur/glymurrc
confdir = get_configdir()
if confdir is not None:
fname = os.path.join(confdir, 'glymurrc')
if os.path.exists(fname):
return fname
# didn't find a configuration file.
return None
def get_openjpeg_config():
libopenjpeg_path = find_library('openjpeg')
# If we could not find it, then look in some likely locations.
if libopenjpeg_path is None:
if platform.system() == 'Darwin':
path = '/opt/local/lib/libopenjpeg.dylib'
if os.path.exists(path):
libopenjpeg_path = path
elif os.name == 'nt':
path = os.path.join('C:\\', 'Program files', 'OpenJPEG 1.5',
'bin', 'openjpeg.dll')
if os.path.exists(path):
libopenjpeg_path = path
try:
if os.name == "nt":
OPENJPEG = ctypes.windll.LoadLibrary(libopenjpeg_path)
else:
OPENJPEG = ctypes.CDLL(libopenjpeg_path)
except OSError:
OPENJPEG = None
if OPENJPEG is not None:
# Must be at least 1.5.0
OPENJPEG.opj_version.restype = ctypes.c_char_p
v = OPENJPEG.opj_version()
v = v.decode('utf-8')
major, minor, patch = v.split('.')
if minor != '5':
OPENJPEG = None
return OPENJPEG
def get_openjp2_config():
filename = glymurrc_fname()
if filename is not None:
# Read the configuration file for the library location.
parser = ConfigParser()
parser.read(filename)
libopenjp2_path = parser.get('library', 'openjp2')
else:
# No help from the config file, try to find it ourselves.
libopenjp2_path = find_library('openjp2')
if libopenjp2_path is None:
return None
try:
if os.name == "nt":
OPENJP2 = ctypes.windll.LoadLibrary(libopenjp2_path)
else:
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
def config():
"""Read configuration file.
"""
OPENJP2 = get_openjp2_config()
OPENJPEG = get_openjpeg_config()
return OPENJP2, OPENJPEG
def get_configdir():
"""Return string representing the configuration directory.
Default is $HOME/.config/glymur. You can override this with the
XDG_CONFIG_HOME environment variable.
"""
if 'XDG_CONFIG_HOME' in os.environ:
return os.path.join(os.environ['XDG_CONFIG_HOME'], 'glymur')
if 'HOME' in os.environ:
return os.path.join(os.environ['HOME'], '.config', 'glymur')
if 'USERPROFILE' in os.environ:
return os.path.join(os.environ['USERPROFILE'], 'Application Data', 'glymur')
import os
import warnings
import sys
if sys.hexversion <= 0x03000000:
from ConfigParser import SafeConfigParser as ConfigParser
else:
from configparser import ConfigParser
OPENJP2, OPENJPEG = config()
if OPENJP2 is None and OPENJPEG is None:
wmsg = "Neither the glymur configuration file could not be located "
wmsg += "nor could openjpeg 1.5.1 be located. Glymur can only "
wmsg += "operate under extremely degraded conditions."
warnings.warn(wmsg, UserWarning)
from .config import glymur_config
OPENJP2, OPENJPEG = glymur_config()
# Progression order
LRCP = 0

View file

@ -6,19 +6,11 @@ from ctypes.util import find_library
import platform
import os
if os.name == "nt":
path = os.path.join('C:\\', 'Program files', 'OpenJPEG 1.5',
'bin', 'openjpeg.dll')
OPENJPEG = ctypes.windll.LoadLibrary(path)
else:
if platform.system() == 'Darwin':
OPENJPEG = ctypes.CDLL('/opt/local/lib/libopenjpeg.dylib')
elif platform.system() == 'Linux':
OPENJPEG = ctypes.CDLL(find_library('openjpeg'))
from .config import glymur_config
_, OPENJPEG = glymur_config()
PATH_LEN = 4096 # maximum allowed size for filenames
class event_mgr_t(ctypes.Structure):
"""Message handler object.
"""