Sergio Pascual 0f3ec8c
# Licensed under a 3-clause BSD style license - see LICENSE.rst
Sergio Pascual 0f3ec8c
Sergio Pascual 0f3ec8c
"""
Sergio Pascual 0f3ec8c
Handle loading ply package from system or from the bundled copy
Sergio Pascual 0f3ec8c
"""
Sergio Pascual 0f3ec8c
Sergio Pascual 0f3ec8c
import imp
Sergio Pascual 0f3ec8c
from distutils.version import StrictVersion
Sergio Pascual 0f3ec8c
Sergio Pascual 0f3ec8c
Sergio Pascual 0f3ec8c
def _find_module(name, path=None):
Sergio Pascual 0f3ec8c
    """
Sergio Pascual 0f3ec8c
    Alternative to `imp.find_module` that can also search in subpackages.
Sergio Pascual 0f3ec8c
    """
Sergio Pascual 0f3ec8c
Sergio Pascual 0f3ec8c
    parts = name.split('.')
Sergio Pascual 0f3ec8c
Sergio Pascual 0f3ec8c
    for part in parts:
Sergio Pascual 0f3ec8c
        if path is not None:
Sergio Pascual 0f3ec8c
            path = [path]
Sergio Pascual 0f3ec8c
Sergio Pascual 0f3ec8c
        fh, path, descr = imp.find_module(part, path)
Sergio Pascual 0f3ec8c
Sergio Pascual 0f3ec8c
    return fh, path, descr
Sergio Pascual 0f3ec8c
Sergio Pascual 0f3ec8c
_PLY_MIN_VERSION = StrictVersion('3.4')
Sergio Pascual 0f3ec8c
Sergio Pascual 0f3ec8c
# Update this to prevent Astropy from using its bundled copy of ply
Sergio Pascual 0f3ec8c
# (but only if some other version of at least _PLY_MIN_VERSION can
Sergio Pascual 0f3ec8c
# be provided)
Sergio Pascual 0f3ec8c
_PLY_SEARCH_PATH = ['ply']
Sergio Pascual 0f3ec8c
Sergio Pascual 0f3ec8c
Sergio Pascual 0f3ec8c
for mod_name in _PLY_SEARCH_PATH:
Sergio Pascual 0f3ec8c
    try:
Sergio Pascual 0f3ec8c
        mod_info = _find_module(mod_name)
Sergio Pascual 0f3ec8c
        #mod_lex_info = _find_module(mod_name + '.lex')
Sergio Pascual 0f3ec8c
    except ImportError:
Sergio Pascual 0f3ec8c
        continue
Sergio Pascual 0f3ec8c
Sergio Pascual 0f3ec8c
    mod = imp.load_module(__name__, *mod_info)
Sergio Pascual 0f3ec8c
    #mod_lex = imp.load_module(__name__ + '.lex', *mod_lex_info)
Sergio Pascual 0f3ec8c
Sergio Pascual 0f3ec8c
    try:
Sergio Pascual 0f3ec8c
    #    if StrictVersion(mod_lex.__version__) >= _PLY_MIN_VERSION:
Sergio Pascual 0f3ec8c
    #        break
Sergio Pascual 0f3ec8c
        break
Sergio Pascual 0f3ec8c
    except (AttributeError, ValueError):
Sergio Pascual 0f3ec8c
        # Attribute error if the ply module isn't what it should be and doesn't
Sergio Pascual 0f3ec8c
        # have a .__version__; ValueError if the version string exists but is
Sergio Pascual 0f3ec8c
        # somehow bogus/unparseable
Sergio Pascual 0f3ec8c
        continue
Sergio Pascual 0f3ec8c
else:
Sergio Pascual 0f3ec8c
    raise ImportError(
Sergio Pascual 0f3ec8c
        "Astropy requires the 'ply' module of minimum version {0}; "
Sergio Pascual 0f3ec8c
        "normally this is bundled with the astropy package so if you get "
Sergio Pascual 0f3ec8c
        "this warning consult the packager of your Astropy "
Sergio Pascual 0f3ec8c
        "distribution.".format(_PLY_MIN_VERSION))