Skip to content

Commit a84c8e8

Browse files
committed
ENH: Add info to ImageFile for better error messages
1 parent c0b0f6a commit a84c8e8

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

nipype/interfaces/base/traits_extension.py

+19-3
Original file line numberDiff line numberDiff line change
@@ -214,9 +214,26 @@ def __init__(self,
214214
"""
215215
self.types = types
216216
self.allow_compressed = allow_compressed
217+
self._exts = None
217218
super(ImageFile, self).__init__(value, filter, auto_set, entries,
218219
exists, **metadata)
219220

221+
def info(self):
222+
existing='n existing' if self.exists else ''
223+
comma = ',' if self.exists and not self.allow_compressed else ''
224+
uncompressed=' uncompressed' if not self.allow_compressed else ''
225+
with_ext = ' (valid extensions: [{}])'.format(', '.join(self.exts)) \
226+
if self.types else ''
227+
return 'a{existing}{comma}{uncompressed} file{with_ext}'.format(
228+
existing=existing, comma=comma, uncompressed=uncompressed,
229+
with_ext=with_ext)
230+
231+
@property
232+
def exts(self):
233+
if self.types and self._exts is None:
234+
self._exts = self.grab_exts()
235+
return self._exts
236+
220237
def grab_exts(self):
221238
# TODO: file type validation
222239
exts = []
@@ -243,11 +260,10 @@ def validate(self, object, name, value):
243260
"""
244261
validated_value = super(ImageFile, self).validate(object, name, value)
245262
if validated_value and self.types:
246-
self._exts = self.grab_exts()
247-
if not any(validated_value.endswith(x) for x in self._exts):
263+
if not any(validated_value.endswith(x) for x in self.exts):
248264
raise TraitError(
249265
args="{} is not included in allowed types: {}".format(
250-
validated_value, ', '.join(self._exts)))
266+
validated_value, ', '.join(self.exts)))
251267
return validated_value
252268

253269

0 commit comments

Comments
 (0)