<s> '''<STR_LIT>''' <EOL> from __future__ import division , print_function , absolute_import <EOL> import os <EOL> from os . path import join as pjoin <EOL> from os import environ as env <EOL> import sys <EOL> import tempfile <EOL> from . . data import ( get_data_path , find_data_dir , <EOL> DataError , _cfg_value , make_datasource , <EOL> Datasource , VersionedDatasource , Bomber , <EOL> datasource_or_bomber ) <EOL> from . . tmpdirs import TemporaryDirectory <EOL> from . . import data as nibd <EOL> from nose import with_setup <EOL> from nose . tools import ( assert_equal , assert_raises , raises , assert_false ) <EOL> from . test_environment import ( setup_environment , <EOL> teardown_environment , <EOL> DATA_KEY , <EOL> USER_KEY ) <EOL> DATA_FUNCS = { } <EOL> def setup_data_env ( ) : <EOL> setup_environment ( ) <EOL> global DATA_FUNCS <EOL> DATA_FUNCS [ '<STR_LIT>' ] = nibd . get_nipy_user_dir <EOL> DATA_FUNCS [ '<STR_LIT>' ] = nibd . get_nipy_system_dir <EOL> DATA_FUNCS [ '<STR_LIT>' ] = nibd . get_data_path <EOL> def teardown_data_env ( ) : <EOL> teardown_environment ( ) <EOL> nibd . get_nipy_user_dir = DATA_FUNCS [ '<STR_LIT>' ] <EOL> nibd . get_nipy_system_dir = DATA_FUNCS [ '<STR_LIT>' ] <EOL> nibd . get_data_path = DATA_FUNCS [ '<STR_LIT>' ] <EOL> with_environment = with_setup ( setup_data_env , teardown_data_env ) <EOL> def test_datasource ( ) : <EOL> pth = pjoin ( '<STR_LIT>' , '<STR_LIT:path>' ) <EOL> ds = Datasource ( pth ) <EOL> yield assert_equal , ds . get_filename ( '<STR_LIT>' ) , pjoin ( pth , '<STR_LIT>' ) <EOL> yield ( assert_equal , ds . get_filename ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> pjoin ( pth , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> def test_versioned ( ) : <EOL> with TemporaryDirectory ( ) as tmpdir : <EOL> yield ( assert_raises , <EOL> DataError , <EOL> VersionedDatasource , <EOL> tmpdir ) <EOL> tmpfile = pjoin ( tmpdir , '<STR_LIT>' ) <EOL> with open ( tmpfile , '<STR_LIT>' ) as fobj : <EOL> fobj . write ( '<STR_LIT>' ) <EOL> fobj . write ( '<STR_LIT>' ) <EOL> yield ( assert_raises , <EOL> DataError , <EOL> VersionedDatasource , <EOL> tmpdir ) <EOL> with open ( tmpfile , '<STR_LIT>' ) as fobj : <EOL> fobj . write ( '<STR_LIT>' ) <EOL> fobj . write ( '<STR_LIT>' ) <EOL> yield ( assert_raises , <EOL> DataError , <EOL> VersionedDatasource , <EOL> tmpdir ) <EOL> with open ( tmpfile , '<STR_LIT>' ) as fobj : <EOL> fobj . write ( '<STR_LIT>' ) <EOL> fobj . write ( '<STR_LIT>' ) <EOL> vds = VersionedDatasource ( tmpdir ) <EOL> yield assert_equal , vds . version , '<STR_LIT>' <EOL> yield assert_equal , vds . version_no , <NUM_LIT:0.1> <EOL> yield assert_equal , vds . major_version , <NUM_LIT:0> <EOL> yield assert_equal , vds . minor_version , <NUM_LIT:1> <EOL> yield assert_equal , vds . get_filename ( '<STR_LIT>' ) , tmpfile <EOL> with open ( tmpfile , '<STR_LIT>' ) as fobj : <EOL> fobj . write ( '<STR_LIT>' ) <EOL> fobj . write ( '<STR_LIT>' ) <EOL> vds = VersionedDatasource ( tmpdir ) <EOL> yield assert_equal , vds . version , '<STR_LIT>' <EOL> yield assert_equal , vds . version_no , <NUM_LIT:0.1> <EOL> yield assert_equal , vds . major_version , <NUM_LIT:0> <EOL> yield assert_equal , vds . minor_version , <NUM_LIT:1> <EOL> def test__cfg_value ( ) : <EOL> yield assert_equal , _cfg_value ( '<STR_LIT>' ) , '<STR_LIT>' <EOL> try : <EOL> fd , tmpfile = tempfile . mkstemp ( ) <EOL> fobj = os . fdopen ( fd , '<STR_LIT>' ) <EOL> fobj . write ( '<STR_LIT>' ) <EOL> fobj . write ( '<STR_LIT>' ) <EOL> fobj . flush ( ) <EOL> yield assert_equal , _cfg_value ( tmpfile ) , '<STR_LIT>' <EOL> fobj . write ( '<STR_LIT>' ) <EOL> fobj . write ( '<STR_LIT>' ) <EOL> fobj . flush ( ) <EOL> yield assert_equal , _cfg_value ( tmpfile ) , '<STR_LIT>' <EOL> fobj . write ( '<STR_LIT>' ) <EOL> fobj . flush ( ) <EOL> yield assert_equal , _cfg_value ( tmpfile ) , '<STR_LIT>' <EOL> fobj . close ( ) <EOL> finally : <EOL> try : <EOL> os . unlink ( tmpfile ) <EOL> except : <EOL> pass <EOL> @ with_environment <EOL> def test_data_path ( ) : <EOL> if DATA_KEY in env : <EOL> del env [ DATA_KEY ] <EOL> if USER_KEY in env : <EOL> del os . environ [ USER_KEY ] <EOL> fake_user_dir = '<STR_LIT>' <EOL> nibd . get_nipy_system_dir = lambda : '<STR_LIT>' <EOL> nibd . get_nipy_user_dir = lambda : fake_user_dir <EOL> old_pth = get_data_path ( ) <EOL> def_dirs = [ pjoin ( sys . prefix , '<STR_LIT>' , '<STR_LIT>' ) ] <EOL> if sys . prefix == '<STR_LIT>' : <EOL> def_dirs . append ( pjoin ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> assert_equal ( old_pth , def_dirs + [ '<STR_LIT>' ] ) <EOL> tst_pth = '<STR_LIT>' + os . path . pathsep + '<STR_LIT>' <EOL> tst_list = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> os . environ [ DATA_KEY ] = tst_list [ <NUM_LIT:0> ] <EOL> assert_equal ( get_data_path ( ) , tst_list [ : <NUM_LIT:1> ] + old_pth ) <EOL> os . environ [ DATA_KEY ] = tst_pth <EOL> assert_equal ( get_data_path ( ) , tst_list + old_pth ) <EOL> del os . environ [ DATA_KEY ] <EOL> with TemporaryDirectory ( ) as tmpdir : <EOL> tmpfile = pjoin ( tmpdir , '<STR_LIT>' ) <EOL> with open ( tmpfile , '<STR_LIT>' ) as fobj : <EOL> fobj . write ( '<STR_LIT>' ) <EOL> fobj . write ( '<STR_LIT>' % tst_pth ) <EOL> nibd . get_nipy_user_dir = lambda : tmpdir <EOL> assert_equal ( get_data_path ( ) , tst_list + def_dirs + [ tmpdir ] ) <EOL> nibd . get_nipy_user_dir = lambda : fake_user_dir <EOL> assert_equal ( get_data_path ( ) , old_pth ) <EOL> with TemporaryDirectory ( ) as tmpdir : <EOL> nibd . get_nipy_system_dir = lambda : tmpdir <EOL> tmpfile = pjoin ( tmpdir , '<STR_LIT>' ) <EOL> with open ( tmpfile , '<STR_LIT>' ) as fobj : <EOL> fobj . write ( '<STR_LIT>' ) <EOL> fobj . write ( '<STR_LIT>' % tst_pth ) <EOL> tmpfile = pjoin ( tmpdir , '<STR_LIT>' ) <EOL> with open ( tmpfile , '<STR_LIT>' ) as fobj : <EOL> fobj . write ( '<STR_LIT>' ) <EOL> fobj . write ( '<STR_LIT>' % '<STR_LIT>' ) <EOL> assert_equal ( get_data_path ( ) , <EOL> tst_list + [ '<STR_LIT>' ] + old_pth ) <EOL> def test_find_data_dir ( ) : <EOL> here , fname = os . path . split ( __file__ ) <EOL> under_here , subhere = os . path . split ( here ) <EOL> yield ( assert_raises , <EOL> DataError , <EOL> find_data_dir , <EOL> [ here ] , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> yield ( assert_raises , <EOL> DataError , <EOL> find_data_dir , <EOL> [ here ] , <EOL> fname ) <EOL> dd = find_data_dir ( [ under_here ] , subhere ) <EOL> yield assert_equal , dd , here <EOL> dud_dir = pjoin ( under_here , '<STR_LIT>' ) <EOL> dd = find_data_dir ( [ dud_dir , under_here ] , subhere ) <EOL> yield assert_equal , dd , here <EOL> @ with_environment <EOL> def test_make_datasource ( ) : <EOL> pkg_def = dict ( <EOL> relpath = '<STR_LIT>' ) <EOL> with TemporaryDirectory ( ) as tmpdir : <EOL> nibd . get_data_path = lambda : [ tmpdir ] <EOL> yield ( assert_raises , <EOL> DataError , <EOL> make_datasource , <EOL> pkg_def ) <EOL> pkg_dir = pjoin ( tmpdir , '<STR_LIT>' ) <EOL> os . mkdir ( pkg_dir ) <EOL> yield ( assert_raises , <EOL> DataError , <EOL> make_datasource , <EOL> pkg_def ) <EOL> tmpfile = pjoin ( pkg_dir , '<STR_LIT>' ) <EOL> with open ( tmpfile , '<STR_LIT>' ) as fobj : <EOL> fobj . write ( '<STR_LIT>' ) <EOL> fobj . write ( '<STR_LIT>' ) <EOL> ds = make_datasource ( pkg_def , data_path = [ tmpdir ] ) <EOL> yield assert_equal , ds . version , '<STR_LIT>' <EOL> @ raises ( DataError ) <EOL> def test_bomber ( ) : <EOL> b = Bomber ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> b . any_attribute <EOL> def test_bomber_inspect ( ) : <EOL> b = Bomber ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> assert_false ( hasattr ( b , '<STR_LIT>' ) ) <EOL> @ with_environment <EOL> def test_datasource_or_bomber ( ) : <EOL> pkg_def = dict ( <EOL> relpath = '<STR_LIT>' ) <EOL> with TemporaryDirectory ( ) as tmpdir : <EOL> nibd . get_data_path = lambda : [ tmpdir ] <EOL> ds = datasource_or_bomber ( pkg_def ) <EOL> yield ( assert_raises , <EOL> DataError , <EOL> getattr , <EOL> ds , <EOL> '<STR_LIT>' ) <EOL> pkg_dir = pjoin ( tmpdir , '<STR_LIT>' ) <EOL> os . mkdir ( pkg_dir ) <EOL> tmpfile = pjoin ( pkg_dir , '<STR_LIT>' ) <EOL> with open ( tmpfile , '<STR_LIT>' ) as fobj : <EOL> fobj . write ( '<STR_LIT>' ) <EOL> fobj . write ( '<STR_LIT>' ) <EOL> ds = datasource_or_bomber ( pkg_def ) <EOL> ds . get_filename ( '<STR_LIT>' ) <EOL> pkg_def [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> ds = datasource_or_bomber ( pkg_def ) <EOL> ds . get_filename ( '<STR_LIT>' ) <EOL> pkg_def [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> ds = datasource_or_bomber ( pkg_def ) <EOL> yield ( assert_raises , <EOL> DataError , <EOL> getattr , <EOL> ds , <EOL> '<STR_LIT>' ) </s>
