29
29
fmlogger = logging .getLogger ("filemanip" )
30
30
31
31
32
+ related_filetype_sets = [
33
+ ('.hdr' , '.img' , '.mat' ),
34
+ ('.BRIK' , '.HEAD' ),
35
+ ]
36
+
37
+
32
38
class FileNotFoundError (Exception ):
33
39
pass
34
40
@@ -215,7 +221,8 @@ def hash_timestamp(afile):
215
221
216
222
217
223
def copyfile (originalfile , newfile , copy = False , create_new = False ,
218
- hashmethod = None , use_hardlink = False ):
224
+ hashmethod = None , use_hardlink = False ,
225
+ copy_related_files = True ):
219
226
"""Copy or link ``originalfile`` to ``newfile``.
220
227
221
228
If ``use_hardlink`` is True, and the file can be hard-linked, then a
@@ -236,6 +243,9 @@ def copyfile(originalfile, newfile, copy=False, create_new=False,
236
243
use_hardlink : Bool
237
244
specifies whether to hard-link files, when able
238
245
(Default=False), taking precedence over copy
246
+ copy_related_files : Bool
247
+ specifies whether to also operate on related files, as defined in
248
+ ``related_filetype_sets``
239
249
240
250
Returns
241
251
-------
@@ -328,38 +338,36 @@ def copyfile(originalfile, newfile, copy=False, create_new=False,
328
338
fmlogger .warn (e .message )
329
339
330
340
# Associated files
331
- if originalfile .endswith (".img" ):
332
- hdrofile = originalfile [:- 4 ] + ".hdr"
333
- hdrnfile = newfile [:- 4 ] + ".hdr"
334
- matofile = originalfile [:- 4 ] + ".mat"
335
- if os .path .exists (matofile ):
336
- matnfile = newfile [:- 4 ] + ".mat"
337
- copyfile (matofile , matnfile , copy , hashmethod = hashmethod ,
338
- use_hardlink = use_hardlink )
339
- copyfile (hdrofile , hdrnfile , copy , hashmethod = hashmethod ,
340
- use_hardlink = use_hardlink )
341
- elif originalfile .endswith (".BRIK" ):
342
- hdrofile = originalfile [:- 5 ] + ".HEAD"
343
- hdrnfile = newfile [:- 5 ] + ".HEAD"
344
- copyfile (hdrofile , hdrnfile , copy , hashmethod = hashmethod ,
345
- use_hardlink = use_hardlink )
341
+ if copy_related_files :
342
+ related_file_pairs = (get_related_files (f , include_this_file = False )
343
+ for f in (originalfile , newfile ))
344
+ for alt_ofile , alt_nfile in zip (* related_file_pairs ):
345
+ if os .path .exists (alt_ofile ):
346
+ copyfile (alt_ofile , alt_nfile , copy , hashmethod = hashmethod ,
347
+ use_hardlink = use_hardlink , copy_related_files = False )
346
348
347
349
return newfile
348
350
349
351
350
- def get_related_files (filename ):
351
- """Returns a list of related files for Nifti-Pair, Analyze (SPM) and AFNI
352
- files
352
+ def get_related_files (filename , include_this_file = True ):
353
+ """Returns a list of related files, as defined in
354
+ ``related_filetype_sets``, for a filename. (e.g., Nifti-Pair, Analyze (SPM)
355
+ and AFNI files).
356
+
357
+ Parameters
358
+ ----------
359
+ filename : str
360
+ File name to find related filetypes of.
361
+ include_this_file : bool
362
+ If true, output includes the input filename.
353
363
"""
354
364
related_files = []
355
- if filename .endswith (".img" ) or filename .endswith (".hdr" ):
356
- path , name , ext = split_filename (filename )
357
- for ext in ['.hdr' , '.img' , '.mat' ]:
358
- related_files .append (os .path .join (path , name + ext ))
359
- elif filename .endswith (".BRIK" ) or filename .endswith (".HEAD" ):
360
- path , name , ext = split_filename (filename )
361
- for ext in ['.BRIK' , '.HEAD' ]:
362
- related_files .append (os .path .join (path , name + ext ))
365
+ path , name , this_type = split_filename (filename )
366
+ for type_set in related_filetype_sets :
367
+ if this_type in type_set :
368
+ for related_type in type_set :
369
+ if include_this_file or related_type != this_type :
370
+ related_files .append (os .path .join (path , name + related_type ))
363
371
if not len (related_files ):
364
372
related_files = [filename ]
365
373
return related_files
0 commit comments