Skip to content

Commit 1b083f1

Browse files
committed
create ExportFile interface
1 parent b8d29a9 commit 1b083f1

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

nipype/interfaces/io.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import tempfile
2929
from os.path import join, dirname
3030
from warnings import warn
31+
import errno
3132

3233
from .. import config, logging
3334
from ..utils.filemanip import (
@@ -2863,3 +2864,33 @@ def _list_outputs(self):
28632864

28642865
def _add_output_traits(self, base):
28652866
return add_traits(base, list(self.inputs.output_query.keys()))
2867+
2868+
2869+
class ExportFileInputSpec(BaseInterfaceInputSpec):
2870+
in_file = File(exists=True, desc='Input file name')
2871+
out_file = File(exists=False, desc='Output file name')
2872+
check_extension = traits.Bool(False, desc='Ensure that the input and output file extensions match')
2873+
clobber = traits.Bool(False, desc='Permit overwriting existing files')
2874+
2875+
2876+
class ExportFileOutputSpec(TraitedSpec):
2877+
out_file = File(exists=True, desc='Output file name')
2878+
2879+
2880+
class ExportFile(BaseInterface):
2881+
input_spec = ExportFileInputSpec
2882+
output_spec = ExportFileOutputSpec
2883+
2884+
def _run_interface(self, runtime):
2885+
if not self.inputs.clobber and op.exists(self.inputs.out_file):
2886+
raise FileExistsError(errno.EEXIST, f'File {self.inputs.out_file} exists')
2887+
if (self.inputs.check_extension and
2888+
op.splitext(self.inputs.in_file)[1] != op.splitext(self.inputs.out_file)[1]):
2889+
raise RuntimeError(f'{self.inputs.in_file} and {self.inputs.out_file} have different extensions')
2890+
shutil.copy(str(self.inputs.in_file), str(self.inputs.out_file))
2891+
return runtime
2892+
2893+
def _list_outputs(self):
2894+
outputs = self.output_spec().get()
2895+
outputs['out_file'] = self.inputs.out_file
2896+
return outputs

nipype/interfaces/tests/test_io.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -680,3 +680,26 @@ def _mock_get_ssh_client(self):
680680
.check(file=True, exists=True)) # exists?
681681

682682
old_cwd.chdir()
683+
684+
685+
def test_ExportFile(tmp_path):
686+
testin = tmp_path / 'in.txt'
687+
testin.write_text('test string')
688+
i = nio.ExportFile()
689+
i.inputs.in_file = testin
690+
i.inputs.out_file = tmp_path / 'out.tsv'
691+
i.inputs.check_extension = True
692+
with pytest.raises(RuntimeError):
693+
i.run()
694+
i.inputs.check_extension = False
695+
i.run()
696+
assert (tmp_path / 'out.tsv').read_text() == 'test string'
697+
i.inputs.out_file = tmp_path / 'out.txt'
698+
i.inputs.check_extension = True
699+
i.run()
700+
assert (tmp_path / 'out.txt').read_text() == 'test string'
701+
with pytest.raises(FileExistsError):
702+
i.run()
703+
i.inputs.clobber = True
704+
i.run()
705+
assert (tmp_path / 'out.txt').read_text() == 'test string'

0 commit comments

Comments
 (0)