|
32 | 32 | from .. import config, logging
|
33 | 33 | from ..utils.filemanip import (
|
34 | 34 | copyfile, simplify_list, ensure_list,
|
35 |
| - get_related_files) |
| 35 | + get_related_files, split_filename, |
| 36 | + FileExistsError) |
36 | 37 | from ..utils.misc import human_order_sorted, str2bool
|
37 | 38 | from .base import (
|
38 | 39 | TraitedSpec, traits, Str, File, Directory, BaseInterface, InputMultiPath,
|
39 | 40 | isdefined, OutputMultiPath, DynamicTraitedSpec, Undefined, BaseInterfaceInputSpec,
|
40 |
| - LibraryBaseInterface) |
| 41 | + LibraryBaseInterface, SimpleInterface) |
41 | 42 |
|
42 | 43 | iflogger = logging.getLogger('nipype.interface')
|
43 | 44 |
|
@@ -2863,3 +2864,51 @@ def _list_outputs(self):
|
2863 | 2864 |
|
2864 | 2865 | def _add_output_traits(self, base):
|
2865 | 2866 | return add_traits(base, list(self.inputs.output_query.keys()))
|
| 2867 | + |
| 2868 | + |
| 2869 | +class ExportFileInputSpec(BaseInterfaceInputSpec): |
| 2870 | + in_file = File(exists=True, mandatory=True, desc='Input file name') |
| 2871 | + out_file = File(mandatory=True, desc='Output file name') |
| 2872 | + check_extension = traits.Bool(True, desc='Ensure that the input and output file extensions match') |
| 2873 | + clobber = traits.Bool(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(SimpleInterface): |
| 2881 | + """ Export a file to an absolute path |
| 2882 | +
|
| 2883 | + This interface copies an input file to a named output file. |
| 2884 | + This is useful to save individual files to a specific location, |
| 2885 | + instead of more flexible interfaces like DataSink. |
| 2886 | +
|
| 2887 | + Examples |
| 2888 | + -------- |
| 2889 | +
|
| 2890 | + >>> from nipype.interfaces.io import ExportFile |
| 2891 | + >>> import os.path as op |
| 2892 | + >>> ef = ExportFile() |
| 2893 | + >>> ef.inputs.in_file = "T1.nii.gz" |
| 2894 | + >>> os.mkdir("output_folder") |
| 2895 | + >>> ef.inputs.out_file = op.abspath("output_folder/sub1_out.nii.gz") |
| 2896 | + >>> res = ef.run() |
| 2897 | + >>> os.path.exists(res.outputs.out_file) |
| 2898 | + True |
| 2899 | +
|
| 2900 | + """ |
| 2901 | + input_spec = ExportFileInputSpec |
| 2902 | + output_spec = ExportFileOutputSpec |
| 2903 | + |
| 2904 | + def _run_interface(self, runtime): |
| 2905 | + if not self.inputs.clobber and op.exists(self.inputs.out_file): |
| 2906 | + raise FileExistsError(self.inputs.out_file) |
| 2907 | + if not op.isabs(self.inputs.out_file): |
| 2908 | + raise ValueError('Out_file must be an absolute path.') |
| 2909 | + if (self.inputs.check_extension and |
| 2910 | + split_filename(self.inputs.in_file)[2] != split_filename(self.inputs.out_file)[2]): |
| 2911 | + raise RuntimeError('%s and %s have different extensions' % (self.inputs.in_file, self.inputs.out_file)) |
| 2912 | + shutil.copy(str(self.inputs.in_file), str(self.inputs.out_file)) |
| 2913 | + self._results['out_file'] = self.inputs.out_file |
| 2914 | + return runtime |
0 commit comments