Skip to content

Commit 598b739

Browse files
committed
Merge pull request #361 from effigies/remove-imageopener-decorator
RF: remove ImageOpener decorator Image classes should directly update the ImageOpener.compress_ext_map dictionary.
2 parents 9a67e76 + 212076f commit 598b739

File tree

3 files changed

+20
-45
lines changed

3 files changed

+20
-45
lines changed

nibabel/freesurfer/mghformat.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -454,13 +454,13 @@ def writeftr_to(self, fileobj):
454454
fileobj.write(ftr_nd.tostring())
455455

456456

457-
# Register .mgz extension as compressed
458-
@ImageOpener.register_ext_from_image('.mgz', ImageOpener.gz_def)
459457
class MGHImage(SpatialImage):
460458
""" Class for MGH format image
461459
"""
462460
header_class = MGHHeader
463-
valid_exts = ('.mgh',)
461+
valid_exts = ('.mgh', '.mgz')
462+
# Register that .mgz extension signals gzip compression
463+
ImageOpener.compress_ext_map['.mgz'] = ImageOpener.gz_def
464464
files_types = (('image', '.mgh'),)
465465
_compressed_suffixes = ()
466466

nibabel/openers.py

+16-36
Original file line numberDiff line numberDiff line change
@@ -149,42 +149,22 @@ def __exit__(self, exc_type, exc_val, exc_tb):
149149

150150

151151
class ImageOpener(Opener):
152-
""" Opener-type class passed to image classes to collect compressed extensions
152+
""" Opener-type class to collect extra compressed extensions
153153
154-
This class allows itself to have image extensions added to its class
155-
attributes, via the `register_ex_from_images`. The class can therefore
156-
change state when image classes are defined.
154+
A trivial sub-class of opener to which image classes can add extra
155+
extensions with custom openers, such as compressed openers.
156+
157+
To add an extension, add a line to the class definition (not __init__):
158+
159+
ImageOpener.compress_ext_map[ext] = func_def
160+
161+
``ext`` is a file extension beginning with '.' and should be included in
162+
the image class's ``valid_exts`` tuple.
163+
164+
``func_def`` is a `(function, (args,))` tuple, where `function accepts a
165+
filename as the first parameter, and `args` defines the other arguments
166+
that `function` accepts. These arguments must be any (unordered) subset of
167+
`mode`, `compresslevel`, and `buffering`.
157168
"""
169+
# Add new extensions to this dictionary
158170
compress_ext_map = Opener.compress_ext_map.copy()
159-
160-
@classmethod
161-
def register_ext_from_image(opener_klass, ext, func_def):
162-
"""Decorator for adding extension / opener_function associations.
163-
164-
Should be used to decorate classes. Updates ImageOpener class with
165-
desired extension / opener association. Updates decorated class by
166-
adding ```ext``` to ```klass.alternate_exts```.
167-
168-
Parameters
169-
----------
170-
opener_klass : decorated class
171-
ext : file extension to associate `func_def` with.
172-
should start with '.'
173-
func_def : opener function/parameter tuple
174-
Should be a `(function, (args,))` tuple, where `function` accepts
175-
a filename as the first parameter, and `args` defines the
176-
other arguments that `function` accepts. These arguments must
177-
be any (unordered) subset of `mode`, `compresslevel`,
178-
and `buffering`.
179-
180-
Returns
181-
-------
182-
opener_klass
183-
"""
184-
def decorate(klass):
185-
assert ext not in opener_klass.compress_ext_map, \
186-
"Cannot redefine extension-function mappings."
187-
opener_klass.compress_ext_map[ext] = func_def
188-
klass.valid_exts += (ext,)
189-
return klass
190-
return decorate

nibabel/tests/test_openers.py

+1-6
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,6 @@ def test_BinOpener():
9393

9494

9595
class TestImageOpener:
96-
valid_exts = ()
97-
9896
def setUp(self):
9997
self.compress_ext_map = ImageOpener.compress_ext_map.copy()
10098

@@ -115,12 +113,9 @@ def file_opener(fileish, mode):
115113

116114
# Add the association
117115
n_associations = len(ImageOpener.compress_ext_map)
118-
dec = ImageOpener.register_ext_from_image('.foo',
119-
(file_opener, ('mode',)))
120-
dec(self.__class__)
116+
ImageOpener.compress_ext_map['.foo'] = (file_opener, ('mode',))
121117
assert_equal(n_associations + 1, len(ImageOpener.compress_ext_map))
122118
assert_true('.foo' in ImageOpener.compress_ext_map)
123-
assert_true('.foo' in self.valid_exts)
124119

125120
with InTemporaryDirectory():
126121
with ImageOpener('test.foo', 'w'):

0 commit comments

Comments
 (0)