NIPY logo

Site Navigation

NIPY Community

Table Of Contents

Previous topic

testing.nosepatch

This Page

utils.data

Module: utils.data

Inheritance diagram for nipy.utils.data:

Utilities to find files from NIPY data packages

Classes

Bomber

class nipy.utils.data.Bomber(name, msg)

Bases: object

Class to raise an informative error when used

__init__(name, msg)

DataError

class nipy.utils.data.DataError

Bases: exceptions.OSError

__init__()

x.__init__(...) initializes x; see help(type(x)) for signature

args
errno

exception errno

filename

exception filename

message
strerror

exception strerror

Datasource

class nipy.utils.data.Datasource(base_path)

Bases: object

Simple class to add base path to relative path

Methods

get_filename
list_files
__init__(base_path)

Initialize datasource

Parameters :

base_path : str

path to prepend to all relative paths

Examples

>>> from os.path import join as pjoin
>>> repo = Datasource(pjoin('a', 'path'))
>>> fname = repo.get_filename('somedir', 'afile.txt')
>>> fname == pjoin('a', 'path', 'somedir', 'afile.txt')
True
get_filename(*path_parts)

Prepend base path to *path_parts

We make no check whether the returned path exists.

Parameters :

*path_parts : sequence of strings

Returns :

fname : str

result of os.path.join(*path_parts), with ``self.base_path prepended

list_files(relative=True)

Recursively list the files in the data source directory.

Parameters :

relative: bool, optional :

If True, path returned are relative to the base paht of the data source.

Returns :

file_list: list of strings :

List of the paths of all the files in the data source.

VersionedDatasource

class nipy.utils.data.VersionedDatasource(base_path, config_filename=None)

Bases: nipy.utils.data.Datasource

Datasource with version information in config file

Methods

get_filename
list_files
__init__(base_path, config_filename=None)

Initialize versioned datasource

We assume that there is a configuration file with version information in datasource directory tree.

The configuration file contains an entry like:

[DEFAULT]
version = 0.3

The version should have at least a major and a minor version number in the form above.

Parameters :

base_path : str

path to prepend to all relative paths

config_filaname : None or str

relative path to configuration file containing version

get_filename(*path_parts)

Prepend base path to *path_parts

We make no check whether the returned path exists.

Parameters :

*path_parts : sequence of strings

Returns :

fname : str

result of os.path.join(*path_parts), with ``self.base_path prepended

list_files(relative=True)

Recursively list the files in the data source directory.

Parameters :

relative: bool, optional :

If True, path returned are relative to the base paht of the data source.

Returns :

file_list: list of strings :

List of the paths of all the files in the data source.

Functions

nipy.utils.data.find_data_dir(root_dirs, *names)

Find relative path given path prefixes to search

We raise a DataError if we can’t find the relative path

Parameters :

root_dirs : sequence of strings

sequence of paths in which to search for data directory

*names : sequence of strings

sequence of strings naming directory to find. The name to search for is given by os.path.join(*names)

Returns :

data_dir : str

full path (root path added to *names above)

nipy.utils.data.get_data_path()

Return specified or guessed locations of NIPY data files

The algorithm is to return paths, extracted from strings, where strings are found in the following order:

  1. The contents of environment variable NIPY_DATA_PATH
  2. Any section = DATA, key = path value in a config.ini file in your nipy user directory (found with get_nipy_user_dir())
  3. Any section = DATA, key = path value in any files found with a sorted(glob.glob(os.path.join(sys_dir, '*.ini'))) search, where sys_dir is found with get_nipy_system_dir()
  4. If sys.prefix is /usr, we add /usr/local/share/nipy. We need this because Python 2.6 in Debian / Ubuntu does default installs to /usr/local.
  5. The result of get_nipy_user_dir()

Therefore, any paths found in NIPY_DATA_PATH will be searched before paths found in the user directory config.ini

Parameters :None :
Returns :paths : sequence of paths

Notes

We have to add /usr/local/share/nipy if sys.prefix is /usr, because Debian has patched distutils in Python 2.6 to do default distutils installs there:

Examples

>>> pth = get_data_path()
nipy.utils.data.make_datasource(*names, **kwargs)

Return datasource *names as found in data_path

data_path is the only allowed keyword argument.

The relative path of the directory we are looking for is given by os.path.join(*names). We search for this path in the list of paths given by data_path. By default data_path is given by get_data_path() in this module.

If we can’t find the relative path, raise a DataError

Parameters :

*names : sequence of strings

The relative path to search for is given by os.path.join(*names)

data_path : sequence of strings or None, optional

sequence of paths in which to search for data. If None (the default), then use get_data_path()

Returns :

datasource : VersionedDatasource

An initialized VersionedDatasource instance