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