Skip to content
Snippets Groups Projects
Commit 448ac858 authored by Martin Craig's avatar Martin Craig
Browse files

Added fileOrText decorator to enable text input/output to be read/output as Python strings

This is helpful for scripts which write out a logfile for example
parent 0b16a8e6
No related branches found
No related tags found
No related merge requests found
......@@ -1005,3 +1005,43 @@ def fileOrArray(*args, **kwargs):
return _update_wrapper(wrapper, func)
return decorator
def fileOrText(*args, **kwargs):
"""Decorator which can be used to ensure that any text output (e.g. log file are saved
to text files, and output files can be loaded and returned as strings.
"""
def prepIn(workdir, name, val):
infile = None
if isinstance(val, six.string_types):
with tempfile.NamedTemporaryFile(mode='w', suffix='.txt') as f:
f.write(val)
infile = f.name
return infile
def prepOut(workdir, name, val):
return op.join(workdir, '{}.txt'.format(name))
def load(path):
try:
with open(path, "r") as f:
return f.read()
except Exception: return None
def decorator(func):
fot = _FileOrThing(func,
prepIn,
prepOut,
load,
fslpath.removeExt,
*args,
**kwargs)
def wrapper(*args, **kwargs):
return fot(*args, **kwargs)
return _update_wrapper(wrapper, func)
return decorator
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment