From bb1d18c2a04780d1e80720a9ed72803c3962e47b Mon Sep 17 00:00:00 2001 From: John Evans Date: Mon, 24 Jun 2013 08:24:15 -0400 Subject: [PATCH] Moved configuration file parsing back to lib.openjp2 --- glymur/__init__.py | 83 --------------------------------------- glymur/lib/openjp2.py | 91 ++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 89 insertions(+), 85 deletions(-) diff --git a/glymur/__init__.py b/glymur/__init__.py index 473bb65..79fcc5e 100644 --- a/glymur/__init__.py +++ b/glymur/__init__.py @@ -2,89 +2,6 @@ """ -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 - else: - msg = "Configuration file '{0}' does not exist.".format(confdir) - warnings.warn(msg, UserWarning) - - # didn't find a configuration file. - return None - - -def _config(): - """Read configuration file. - - Based on matplotlib. - """ - 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. - from ctypes.util import find_library - libopenjp2_path = find_library('openjp2') - - if libopenjp2_path is None: - return None - - try: - _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 _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') - -import warnings -import sys -if sys.hexversion <= 0x03000000: - from ConfigParser import SafeConfigParser as ConfigParser - from ConfigParser import NoOptionError -else: - from configparser import ConfigParser - from configparser import NoOptionError -import ctypes -import os - -_OPENJP2 = _config() - from .jp2k import Jp2k from .jp2dump import jp2dump diff --git a/glymur/lib/openjp2.py b/glymur/lib/openjp2.py index 798f23d..d7578d0 100644 --- a/glymur/lib/openjp2.py +++ b/glymur/lib/openjp2.py @@ -2,12 +2,99 @@ Wraps individual functions in openjp2 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 + else: + msg = "Configuration file '{0}' does not exist.".format(confdir) + warnings.warn(msg, UserWarning) + + # didn't find a configuration file. + return None + + +def _config(): + """Read configuration file. + + Based on matplotlib. + """ + 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. + from ctypes.util import find_library + libopenjp2_path = find_library('openjp2') + + if libopenjp2_path is None: + return None + + try: + _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 _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') + import ctypes +import os +import warnings + +import sys +if sys.hexversion <= 0x03000000: + from ConfigParser import SafeConfigParser as ConfigParser + from ConfigParser import NoOptionError +else: + from configparser import ConfigParser + from configparser import NoOptionError + +_OPENJP2 = _config() import numpy as np -from .. import _OPENJP2 -from ..core import LRCP, RLCP, RPCL, PCRL, CPRL +# Progression order +LRCP = 0 +RLCP = 1 +RPCL = 2 +PCRL = 3 +CPRL = 4 _ERROR_MSG_LST = []