Skip to content

Commit 01af20f

Browse files
committed
RF: Prefer alternate_exts over abusing files_types
1 parent 5570d36 commit 01af20f

File tree

5 files changed

+11
-8
lines changed

5 files changed

+11
-8
lines changed

nibabel/filename_parser.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,6 @@ def types_filenames(template_fname, types_exts,
131131
elif found_ext == found_ext.lower():
132132
proc_ext = lambda s: s.lower()
133133
for name, ext in types_exts:
134-
if name in tfns: # Allow multipe definitions of image, header, etc,
135-
continue # giving priority to those found first.
136134
if name == direct_set_name:
137135
tfns[name] = template_fname
138136
continue

nibabel/freesurfer/mghformat.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -459,8 +459,7 @@ class MGHImage(SpatialImage):
459459
""" Class for MGH format image
460460
"""
461461
header_class = MGHHeader
462-
files_types = (('image', '.mgh'),
463-
('image', '.mgz'))
462+
files_types = (('image', '.mgh'),)
464463
_compressed_exts = (('.gz',))
465464

466465
makeable = True

nibabel/openers.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,9 @@ class ImageOpener(Opener):
160160
def register_ext_from_image(opener_klass, ext, func_def):
161161
"""Decorator for adding extension / opener_function associations.
162162
163-
Should be used to decorate classes.
163+
Should be used to decorate classes. Updates ImageOpener class with
164+
desired extension / opener association. Updates decorated class by
165+
adding ```ext``` to ```klass.alternate_exts```.
164166
165167
Parameters
166168
----------
@@ -176,12 +178,12 @@ def register_ext_from_image(opener_klass, ext, func_def):
176178
177179
Returns
178180
-------
179-
opener_klass, with a side-effect of updating the ImageOpener class
180-
with the desired extension / opener association.
181+
opener_klass
181182
"""
182183
def decorate(klass):
183184
assert ext not in opener_klass.compress_ext_map, \
184185
"Cannot redefine extension-function mappings."
185186
opener_klass.compress_ext_map[ext] = func_def
187+
klass.alternate_exts += (ext,)
186188
return klass
187189
return decorate

nibabel/spatialimages.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,7 @@ class SpatialImage(object):
325325
''' Template class for images '''
326326
header_class = Header
327327
files_types = (('image', None),)
328+
alternate_exts = () # Modified by @ImageOpener.register_ext_from_image
328329
_compressed_exts = ()
329330

330331
makeable = True # Used in test code
@@ -874,7 +875,8 @@ def from_image(klass, img):
874875

875876
@classmethod
876877
def is_valid_extension(klass, ext):
877-
return np.any([ft[1] == ext.lower() for ft in klass.files_types])
878+
valid = tuple(ft[1] for ft in klass.files_types) + klass.alternate_exts
879+
return ext.lower() in valid
878880

879881
@classmethod
880882
def path_maybe_image(klass, filename, sniff=None):

nibabel/tests/test_openers.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ def test_BinOpener():
9090
BinOpener, 'test.txt', 'r')
9191

9292
class TestImageOpener:
93+
alternate_exts = ()
9394
def setUp(self):
9495
self.compress_ext_map = ImageOpener.compress_ext_map.copy()
9596

@@ -115,6 +116,7 @@ def file_opener(fileish, mode):
115116
dec(self.__class__)
116117
assert_equal(n_associations + 1, len(ImageOpener.compress_ext_map))
117118
assert_true('.foo' in ImageOpener.compress_ext_map)
119+
assert_true('.foo' in self.alternate_exts)
118120

119121
with InTemporaryDirectory():
120122
with ImageOpener('test.foo', 'w'):

0 commit comments

Comments
 (0)