Abstracted out library configuration into separate config module. #71
This commit is contained in:
parent
73cdb5d586
commit
a9ef89fbf0
3 changed files with 128 additions and 135 deletions
124
glymur/lib/config.py
Normal file
124
glymur/lib/config.py
Normal 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')
|
||||||
|
|
@ -2,134 +2,11 @@
|
||||||
Wraps individual functions in openjp2 library.
|
Wraps individual functions in openjp2 library.
|
||||||
"""
|
"""
|
||||||
import ctypes
|
import ctypes
|
||||||
import platform
|
|
||||||
from ctypes.util import find_library
|
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 warnings
|
||||||
|
|
||||||
import sys
|
from .config import glymur_config
|
||||||
if sys.hexversion <= 0x03000000:
|
OPENJP2, OPENJPEG = glymur_config()
|
||||||
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)
|
|
||||||
|
|
||||||
# Progression order
|
# Progression order
|
||||||
LRCP = 0
|
LRCP = 0
|
||||||
|
|
|
||||||
|
|
@ -6,19 +6,11 @@ from ctypes.util import find_library
|
||||||
import platform
|
import platform
|
||||||
import os
|
import os
|
||||||
|
|
||||||
if os.name == "nt":
|
from .config import glymur_config
|
||||||
path = os.path.join('C:\\', 'Program files', 'OpenJPEG 1.5',
|
_, OPENJPEG = glymur_config()
|
||||||
'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'))
|
|
||||||
|
|
||||||
PATH_LEN = 4096 # maximum allowed size for filenames
|
PATH_LEN = 4096 # maximum allowed size for filenames
|
||||||
|
|
||||||
|
|
||||||
class event_mgr_t(ctypes.Structure):
|
class event_mgr_t(ctypes.Structure):
|
||||||
"""Message handler object.
|
"""Message handler object.
|
||||||
"""
|
"""
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue