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
......@@ -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:
infile =
return infile
def prepOut(workdir, name, val):
return op.join(workdir, '{}.txt'.format(name))
def load(path):
with open(path, "r") as f:
except Exception: return None
def decorator(func):
fot = _FileOrThing(func,
def wrapper(*args, **kwargs):
return fot(*args, **kwargs)
return _update_wrapper(wrapper, func)
return decorator
Supports Markdown
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