Skip to content

fix: allow full path parsing in rename #699

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 1 commit into from
Oct 25, 2013
Merged
Changes from all commits
Commits
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
24 changes: 18 additions & 6 deletions nipype/interfaces/utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,15 @@ def _list_outputs(self):
class RenameInputSpec(DynamicTraitedSpec):

in_file = File(exists=True, mandatory=True, desc="file to rename")
keep_ext = traits.Bool(desc="Keep in_file extension, replace non-extension component of name")
keep_ext = traits.Bool(desc=("Keep in_file extension, replace "
"non-extension component of name"))
format_string = traits.String(mandatory=True,
desc="Python formatting string for output template")
parse_string = traits.String(desc="Python regexp parse string to define replacement inputs")
desc=("Python formatting string for output "
"template"))
parse_string = traits.String(desc=("Python regexp parse string to define "
"replacement inputs"))
use_fullpath = traits.Bool(False, use_default=True,
desc="Use full path as input to regex parser")


class RenameOutputSpec(TraitedSpec):
Expand Down Expand Up @@ -210,22 +215,29 @@ def __init__(self, format_string=None, **inputs):
def _rename(self):
fmt_dict = dict()
if isdefined(self.inputs.parse_string):
m = re.search(self.inputs.parse_string, os.path.split(self.inputs.in_file)[1])
if isdefined(self.inputs.use_fullpath) and self.inputs.use_fullpath:
m = re.search(self.inputs.parse_string,
self.inputs.in_file)
else:
m = re.search(self.inputs.parse_string,
os.path.split(self.inputs.in_file)[1])
if m:
fmt_dict.update(m.groupdict())
for field in self.fmt_fields:
val = getattr(self.inputs, field)
if isdefined(val):
fmt_dict[field] = getattr(self.inputs, field)
if self.inputs.keep_ext:
fmt_string = "".join([self.inputs.format_string, split_filename(self.inputs.in_file)[2]])
fmt_string = "".join([self.inputs.format_string,
split_filename(self.inputs.in_file)[2]])
else:
fmt_string = self.inputs.format_string
return fmt_string % fmt_dict

def _run_interface(self, runtime):
runtime.returncode = 0
_ = copyfile(self.inputs.in_file, os.path.join(os.getcwd(), self._rename()))
_ = copyfile(self.inputs.in_file, os.path.join(os.getcwd(),
self._rename()))
return runtime

def _list_outputs(self):
Expand Down