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
Pipeline #2878 failed with stages
in 128 minutes and 49 seconds
......@@ -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
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment