Skip to content

Commit 5b2d756

Browse files
committed
RF - refactored data packages to have relative path as primary key
1 parent d742127 commit 5b2d756

File tree

3 files changed

+37
-32
lines changed

3 files changed

+37
-32
lines changed

nibabel/data.py

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -239,12 +239,12 @@ def make_datasource(pkg_def, **kwargs):
239239
240240
`data_path` is the only allowed keyword argument.
241241
242-
`pkg_def` is a dictionary with at least one key - 'name'. 'name' is a
243-
string which may be contain hyphens e.g. ``nipy-templates``.
242+
`pkg_def` is a dictionary with at least one key - 'relpath'. 'relpath' is a
243+
relative path with unix forward slash separators.
244244
245245
The relative path to the data is found with::
246246
247-
names = pkg_def['name'].split('-')
247+
names = pkg_def['name'].split('/')
248248
rel_path = os.path.join(names)
249249
250250
We search for this relative path in the list of paths given by `data_path`.
@@ -255,11 +255,12 @@ def make_datasource(pkg_def, **kwargs):
255255
Parameters
256256
----------
257257
pkg_def : dict
258-
dict containing at least the key 'name'. If the name contains hyphens
259-
these as taken as directory separators, so we find the relative path to
260-
the data with ``rel_pth = pkg_def['name'].replace('-', os.path.sep)``.
261-
`pkg_def` can also contain a key 'install hint' that we use in the
262-
returned error message from trying to use the resulting datasource
258+
dict containing at least the key 'relpath'. 'relpath' is the data path of
259+
the package relative to `data_path`. It is in unix path format (using
260+
forward slashes as directory separators). `pkg_def` can also contain
261+
optional keys 'name' (the name of the package), and / or a key 'install
262+
hint' that we use in the returned error message from trying to use the
263+
resulting datasource
263264
data_path : sequence of strings or None, optional
264265
sequence of paths in which to search for data. If None (the
265266
default), then use ``get_data_path()``
@@ -274,21 +275,20 @@ def make_datasource(pkg_def, **kwargs):
274275
data_path = kwargs.get('data_path')
275276
if data_path is None:
276277
data_path = get_data_path()
277-
name = pkg_def['name']
278-
names = name.split('-')
278+
unix_relpath = pkg_def['relpath']
279+
names = unix_relpath.split('/')
279280
try:
280281
pth = find_data_dir(data_path, *names)
281282
except DataError, exception:
282283
pth = [pjoin(this_data_path, *names)
283284
for this_data_path in data_path]
284285
pkg_hint = pkg_def.get('install hint', DEFAULT_INSTALL_HINT)
285-
msg = '''%(exc)s;
286-
Is it possible you have not installed a data package?
287-
From the names, maybe you need data package "%(name)s"?
288-
289-
%(pkg_hint)s''' % dict(exc=exception,
290-
name=name,
291-
pkg_hint=pkg_hint)
286+
msg = ('%s; Is it possible you have not installed a data package?' %
287+
exception)
288+
if 'name' in pkg_def:
289+
msg += '\n\nYou may need the package "%s"' % pkg_def['name']
290+
if not pkg_hint is None:
291+
msg += '\n\n%s' % pkg_hint
292292
raise DataError(msg)
293293
return VersionedDatasource(pth)
294294

@@ -321,34 +321,37 @@ def datasource_or_bomber(pkg_def, **options):
321321
Parameters
322322
----------
323323
pkg_def : dict
324-
dict containing at least key 'name'. Can optioanlly have key 'install
325-
hint' (for helpful error messages) and 'min version' giving the minimum
326-
necessary version string for the package.
324+
dict containing at least key 'relpath'. Can optioanlly have keys 'name'
325+
(package name), 'install hint' (for helpful error messages) and 'min
326+
version' giving the minimum necessary version string for the package.
327327
data_path : sequence of strings or None, optional
328328
329329
Returns
330330
-------
331331
ds : datasource or ``Bomber`` instance
332332
'''
333-
name = pkg_def['name']
333+
unix_relpath = pkg_def['relpath']
334334
version = pkg_def.get('min version')
335335
pkg_hint = pkg_def.get('install hint', DEFAULT_INSTALL_HINT)
336-
names = name.split('-')
337-
rel_path = os.path.sep.join(names)
336+
names = unix_relpath.split('/')
337+
sys_relpath = os.path.sep.join(names)
338338
try:
339339
ds = make_datasource(pkg_def, **options)
340340
except DataError, exception:
341-
return Bomber(rel_path, exception)
341+
return Bomber(sys_relpath, exception)
342342
# check version
343343
if (version is None or
344344
LooseVersion(ds.version) >= LooseVersion(version)):
345345
return ds
346-
pkg_name = '-'.join(names)
346+
if 'name' in pkg_def:
347+
pkg_name = pkg_def['name']
348+
else:
349+
pkg_name = 'data at ' + unix_relpath
347350
msg = ('%(name)s is version %(pkg_version)s but we need '
348351
'version >= %(req_version)s\n\n%(pkg_hint)s' %
349-
dict(name=name,
352+
dict(name=pkg_name,
350353
pkg_version=ds.version,
351354
req_version=version,
352355
pkg_hint=pkg_hint))
353-
return Bomber(rel_path, DataError(msg))
356+
return Bomber(sys_relpath, DataError(msg))
354357

nibabel/nicom/tests/data_pkgs.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
from ... import data as nibd
44

55
PUBLIC_PKG_DEF = dict(
6-
name = 'nipy-dicom_public',
6+
relpath = 'nipy/dicom/public',
7+
name = 'nipy-dicom-public',
78
version = '0.1')
89

910
PRIVATE_PKG_DEF = dict(
10-
name = 'nipy-dicom_private',
11+
relpath = 'nipy/dicom/private',
12+
name = 'nipy-dicom-private',
1113
version = '0.1')
1214

1315

nibabel/tests/test_data.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def setup_environment():
4141

4242

4343
def teardown_environment():
44-
"""Restore things that were remebered by the setup_environment function
44+
"""Restore things that were remembered by the setup_environment function
4545
"""
4646
orig_env = GIVEN_ENV['env']
4747
for key in env.keys():
@@ -221,7 +221,7 @@ def test_find_data_dir():
221221
@with_environment
222222
def test_make_datasource():
223223
pkg_def = dict(
224-
name = 'pkg')
224+
relpath = 'pkg')
225225
with TemporaryDirectory() as tmpdir:
226226
nibd.get_data_path = lambda : [tmpdir]
227227
yield (assert_raises,
@@ -251,7 +251,7 @@ def test_bomber():
251251
@with_environment
252252
def test_datasource_or_bomber():
253253
pkg_def = dict(
254-
name = 'pkg')
254+
relpath = 'pkg')
255255
with TemporaryDirectory() as tmpdir:
256256
nibd.get_data_path = lambda : [tmpdir]
257257
ds = datasource_or_bomber(pkg_def)

0 commit comments

Comments
 (0)