Skip to content

ENH: Add "ExportFile" interface as simple alternative to "DataSink" #3054

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 25 commits into from
Oct 26, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
ee88157
Add "copy" trait to Rename, to allow copying instead of symlink
stilley2 Sep 26, 2019
b8d29a9
Revert "Add "copy" trait to Rename, to allow copying instead of symlink"
pndni-builder Sep 26, 2019
1b083f1
create ExportFile interface
pndni-builder Sep 26, 2019
9bd195a
Update nipype/interfaces/io.py
stilley2 Oct 1, 2019
c870166
Update nipype/interfaces/io.py
stilley2 Oct 1, 2019
0d18f98
Update nipype/interfaces/io.py
stilley2 Oct 1, 2019
abe83dd
Update nipype/interfaces/io.py
stilley2 Oct 1, 2019
cba09b2
Apply @effigies suggestions
stilley2 Oct 1, 2019
edcb73c
add FileExistsError to filemanipy for python 2 support
stilley2 Oct 8, 2019
de9e299
Merge remote-tracking branch 'origin/master' into feature/Rename_copy…
stilley2 Oct 8, 2019
b25c9c3
bugfix. actually import FileExistsError
stilley2 Oct 8, 2019
006cdcf
import FileExistsError in test
stilley2 Oct 9, 2019
5737d21
Update nipype/interfaces/tests/test_io.py
stilley2 Oct 9, 2019
47bb8e8
Update nipype/interfaces/tests/test_io.py
stilley2 Oct 9, 2019
1ba9ca1
add docstring to ExportFile
stilley2 Oct 9, 2019
9897a73
Merge remote-tracking branch 'stilley2/feature/Rename_copy_trait' int…
stilley2 Oct 9, 2019
ec023bf
Update nipype/interfaces/io.py
stilley2 Oct 17, 2019
1e0d7a1
Update nipype/interfaces/io.py
stilley2 Oct 17, 2019
4246b02
Update nipype/interfaces/io.py
stilley2 Oct 17, 2019
fe3dd07
Update nipype/interfaces/io.py
stilley2 Oct 17, 2019
7ed854f
bugfixes in docstring
stilley2 Oct 17, 2019
7a3c7f6
Update nipype/interfaces/io.py
stilley2 Oct 21, 2019
1c26d1d
Update nipype/interfaces/tests/test_io.py
stilley2 Oct 21, 2019
a23e16d
Add @effigies suggestion
stilley2 Oct 21, 2019
3695d36
add test_auto_ExportFile.py
stilley2 Oct 22, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions nipype/interfaces/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import tempfile
from os.path import join, dirname
from warnings import warn
import errno

from .. import config, logging
from ..utils.filemanip import (
Expand Down Expand Up @@ -2863,3 +2864,33 @@ def _list_outputs(self):

def _add_output_traits(self, base):
return add_traits(base, list(self.inputs.output_query.keys()))


class ExportFileInputSpec(BaseInterfaceInputSpec):
in_file = File(exists=True, desc='Input file name')
out_file = File(exists=False, desc='Output file name')
check_extension = traits.Bool(False, desc='Ensure that the input and output file extensions match')
clobber = traits.Bool(False, desc='Permit overwriting existing files')


class ExportFileOutputSpec(TraitedSpec):
out_file = File(exists=True, desc='Output file name')


class ExportFile(BaseInterface):
input_spec = ExportFileInputSpec
output_spec = ExportFileOutputSpec

def _run_interface(self, runtime):
if not self.inputs.clobber and op.exists(self.inputs.out_file):
raise FileExistsError(errno.EEXIST, f'File {self.inputs.out_file} exists')
if (self.inputs.check_extension and
op.splitext(self.inputs.in_file)[1] != op.splitext(self.inputs.out_file)[1]):
raise RuntimeError(f'{self.inputs.in_file} and {self.inputs.out_file} have different extensions')
shutil.copy(str(self.inputs.in_file), str(self.inputs.out_file))
return runtime

def _list_outputs(self):
outputs = self.output_spec().get()
outputs['out_file'] = self.inputs.out_file
return outputs
23 changes: 23 additions & 0 deletions nipype/interfaces/tests/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -680,3 +680,26 @@ def _mock_get_ssh_client(self):
.check(file=True, exists=True)) # exists?

old_cwd.chdir()


def test_ExportFile(tmp_path):
testin = tmp_path / 'in.txt'
testin.write_text('test string')
i = nio.ExportFile()
i.inputs.in_file = testin
i.inputs.out_file = tmp_path / 'out.tsv'
i.inputs.check_extension = True
with pytest.raises(RuntimeError):
i.run()
i.inputs.check_extension = False
i.run()
assert (tmp_path / 'out.tsv').read_text() == 'test string'
i.inputs.out_file = tmp_path / 'out.txt'
i.inputs.check_extension = True
i.run()
assert (tmp_path / 'out.txt').read_text() == 'test string'
with pytest.raises(FileExistsError):
i.run()
i.inputs.clobber = True
i.run()
assert (tmp_path / 'out.txt').read_text() == 'test string'