view pylearn/datasets/config.py @ 1531:88f361283a19 tip

Fix url/name to pylearn2.
author Frederic Bastien <nouiz@nouiz.org>
date Mon, 09 Sep 2013 10:08:05 -0400
parents e7c4d031d333
children
line wrap: on
line source

"""Configuration options for datasets


Especially, the locations of data files.
"""

import os, sys, logging
def _logger():  return logging.getLogger('pylearn.datasets.config')
def debug(*msg): _logger().debug(' '.join(str(m) for m in msg))
def info(*msg): _logger().info(' '.join(str(m) for m in msg))
def warn(*msg): _logger().warn(' '.join(str(m) for m in msg))
def warning(*msg): _logger().warning(' '.join(str(m) for m in msg))
def error(*msg): _logger().error(' '.join(str(m) for m in msg))


def env_get(key, default, key2 = None):
    if key2 and os.getenv(key) is None:
        key=key2
    if os.getenv(key) is None:
        if env_get.first_warning:
            warning("Environment variable", key, 'is not set. Using default of', default)
            env_get.first_warning = False
        return default
    else:
        return os.getenv(key)
env_get.first_warning = True

def data_root():
    """Deprecated, use data_roots() or get_filepath_in_roots()

    It id deprecated as it don't allow to use more then 1 path.
    """
    roots = env_get('PYLEARN_DATA_ROOT', os.getenv('HOME')+'/data', 'DBPATH')
    return roots.split(':')[0]

def data_roots():
    """Return a list of path that are in the PYLEARN_DATA_ROOT env variable."""
    if hasattr(data_roots, 'rval'):
        return data_roots.rval
    roots = os.getenv('PYLEARN_DATA_ROOT')
    if roots is None:
        roots = [data_root()]
    else:
        # Note that under Windows, we cannot use ':' as a delimiter because
        # paths may contain this character. Thus we use ';' instead (similar to
        # the PATH environment variable in Windows).
        if sys.platform == 'win32':
            roots = roots.split(';')
        else:
            roots = roots.split(':')
    # Remove paths that are not directories.
    data_roots.rval = [r for r in roots if os.path.isdir(r)]
    return data_roots.rval


def get_filepath_in_roots(*names):
    """Return the full path of name that exist under a directory
    in the PYLEARN_DATA_ROOT env variable.

    If their is multiple file name, we return the first that exist.
    This allow to get one of the file that is there.
    """
    for name in names:
        for root in data_roots():
            path = os.path.join(root,name)
            if os.path.exists(path):
                return path