Skip to content
Snippets Groups Projects
Commit 96736732 authored by Paul McCarthy's avatar Paul McCarthy :mountain_bicyclist:
Browse files

RF: make FileOrThing+Results classes "public:, for type testing

parent 346147da
No related branches found
No related tags found
No related merge requests found
...@@ -403,18 +403,18 @@ def namedPositionals(func, args): ...@@ -403,18 +403,18 @@ def namedPositionals(func, args):
LOAD = object() LOAD = object()
"""Constant used by the :class:`_FileOrThing` class to indicate that an output """Constant used by the :class:`FileOrThing` class to indicate that an output
file should be loaded into memory and returned as a Python object. file should be loaded into memory and returned as a Python object.
""" """
class _FileOrThing(object): class FileOrThing(object):
"""Decorator which ensures that certain arguments which are passed into the """Decorator which ensures that certain arguments which are passed into the
decorated function are always passed as file names. Both positional and decorated function are always passed as file names. Both positional and
keyword arguments can be specified. keyword arguments can be specified.
The ``_FileOrThing`` class is not intended to be used directly - see the The ``FileOrThing`` class is not intended to be used directly - see the
:func:`fileOrImage` and :func:`fileOrArray` decorator functions for more :func:`fileOrImage` and :func:`fileOrArray` decorator functions for more
details. details.
...@@ -445,7 +445,7 @@ class _FileOrThing(object): ...@@ -445,7 +445,7 @@ class _FileOrThing(object):
**Return value** **Return value**
Functions decorated with a ``_FileOrThing`` decorator will always return a Functions decorated with a ``FileOrThing`` decorator will always return a
``dict``-like object, where the function's actual return value is ``dict``-like object, where the function's actual return value is
accessible via an attribute called ``stdout``. All output arguments with a accessible via an attribute called ``stdout``. All output arguments with a
value of ``LOAD`` will be present as dictionary entries, with the keyword value of ``LOAD`` will be present as dictionary entries, with the keyword
...@@ -460,7 +460,7 @@ class _FileOrThing(object): ...@@ -460,7 +460,7 @@ class _FileOrThing(object):
The above description holds in all situations, except when an argument The above description holds in all situations, except when an argument
called ``submit`` is passed, and is set to a value which evaluates to called ``submit`` is passed, and is set to a value which evaluates to
``True``. In this case, the ``_FileOrThing`` decorator will pass all ``True``. In this case, the ``FileOrThing`` decorator will pass all
arguments straight through to the decorated function, and will return its arguments straight through to the decorated function, and will return its
return value unchanged. return value unchanged.
...@@ -534,27 +534,27 @@ class _FileOrThing(object): ...@@ -534,27 +534,27 @@ class _FileOrThing(object):
**Using with other decorators** **Using with other decorators**
``_FileOrThing`` decorators can be chained with other ``_FileOrThing`` ``FileOrThing`` decorators can be chained with other ``FileOrThing``
decorators, and other decorators. When multiple ``_FileOrThing`` decorators, and other decorators. When multiple ``FileOrThing``
decorators are used on a single function, the outputs from each decorator decorators are used on a single function, the outputs from each decorator
are merged together into a single dict-like object. are merged together into a single dict-like object.
``_FileOrThing`` decorators can be used with any other decorators ``FileOrThing`` decorators can be used with any other decorators
**as long as** they do not manipulate the return value, and as long as **as long as** they do not manipulate the return value, and as long as
the ``_FileOrThing`` decorators are adjacent to each other. the ``FileOrThing`` decorators are adjacent to each other.
""" """
class _Results(dict): class Results(dict):
"""A custom ``dict`` type used to return outputs from a function """A custom ``dict`` type used to return outputs from a function
decorated with ``_FileOrThing``. All outputs are stored as dictionary decorated with ``FileOrThing``. All outputs are stored as dictionary
items, with the argument name as key, and the output object (the items, with the argument name as key, and the output object (the
"thing") as value. "thing") as value.
Where possible (i.e. for outputs named with a valid Python Where possible (i.e. for outputs named with a valid Python
identifier), the outputs are also made accessible as attributes of identifier), the outputs are also made accessible as attributes of
the ``_Results`` object. the ``Results`` object.
The decorated function's actual return value is accessible via the The decorated function's actual return value is accessible via the
:meth:`stdout` property. :meth:`stdout` property.
...@@ -562,7 +562,7 @@ class _FileOrThing(object): ...@@ -562,7 +562,7 @@ class _FileOrThing(object):
def __init__(self, stdout): def __init__(self, stdout):
"""Create a ``_Results`` dict. """Create a ``Results`` dict.
:arg stdout: Return value of the decorated function (typically a :arg stdout: Return value of the decorated function (typically a
tuple containing the standard output and error of the tuple containing the standard output and error of the
...@@ -593,7 +593,7 @@ class _FileOrThing(object): ...@@ -593,7 +593,7 @@ class _FileOrThing(object):
removeExt, removeExt,
*args, *args,
**kwargs): **kwargs):
"""Initialise a ``_FileOrThing`` decorator. """Initialise a ``FileOrThing`` decorator.
:arg func: The function to be decorated. :arg func: The function to be decorated.
...@@ -618,7 +618,7 @@ class _FileOrThing(object): ...@@ -618,7 +618,7 @@ class _FileOrThing(object):
All other positional arguments are interpreted as the names of the All other positional arguments are interpreted as the names of the
arguments to the function which will be handled by this arguments to the function which will be handled by this
``_FileOrThing`` decorator. If not provided, *all* arguments passed to ``FileOrThing`` decorator. If not provided, *all* arguments passed to
the function will be handled. the function will be handled.
...@@ -673,8 +673,8 @@ class _FileOrThing(object): ...@@ -673,8 +673,8 @@ class _FileOrThing(object):
'or LOAD with submit=True!') 'or LOAD with submit=True!')
return func(*args, **kwargs) return func(*args, **kwargs)
# If this _FileOrThing is being called # If this FileOrThing is being called
# by another _FileOrThing don't create # by another FileOrThing don't create
# another working directory. We do this # another working directory. We do this
# sneakily, by setting an attribute on # sneakily, by setting an attribute on
# the wrapped function which stores the # the wrapped function which stores the
...@@ -734,8 +734,8 @@ class _FileOrThing(object): ...@@ -734,8 +734,8 @@ class _FileOrThing(object):
passed to the ``prepOut`` function specified at :meth:`__init__`. passed to the ``prepOut`` function specified at :meth:`__init__`.
All other arguments are passed through the ``prepIn`` function. All other arguments are passed through the ``prepIn`` function.
:arg parent: ``True`` if this ``_FileOrThing`` is the first in a :arg parent: ``True`` if this ``FileOrThing`` is the first in a
chain of ``_FileOrThing`` decorators. chain of ``FileOrThing`` decorators.
:arg workdir: Directory in which all temporary files should be stored. :arg workdir: Directory in which all temporary files should be stored.
...@@ -779,7 +779,7 @@ class _FileOrThing(object): ...@@ -779,7 +779,7 @@ class _FileOrThing(object):
# Prefixed outputs are only # Prefixed outputs are only
# managed by the parent # managed by the parent
# _FileOrthing in a chain of # FileOrthing in a chain of
# FoT decorators. # FoT decorators.
if not parent: if not parent:
prefix = None prefix = None
...@@ -894,11 +894,11 @@ class _FileOrThing(object): ...@@ -894,11 +894,11 @@ class _FileOrThing(object):
def __generateResult( def __generateResult(
self, workdir, result, outprefix, outfiles, prefixes): self, workdir, result, outprefix, outfiles, prefixes):
"""Loads function outputs and returns a :class:`_Results` object. """Loads function outputs and returns a :class:`Results` object.
Called by :meth:`__call__` after the decorated function has been Called by :meth:`__call__` after the decorated function has been
called. Figures out what files should be loaded, and loads them into called. Figures out what files should be loaded, and loads them into
a ``_Results`` object. a ``Results`` object.
:arg workdir: Directory which contains the function outputs. :arg workdir: Directory which contains the function outputs.
:arg result: Function return value. :arg result: Function return value.
...@@ -909,16 +909,16 @@ class _FileOrThing(object): ...@@ -909,16 +909,16 @@ class _FileOrThing(object):
:arg prefixes: Dictionary containing output-prefix patterns to be :arg prefixes: Dictionary containing output-prefix patterns to be
loaded (see :meth:`__prepareArgs`). loaded (see :meth:`__prepareArgs`).
:returns: A ``_Results`` object containing all loaded outputs. :returns: A ``Results`` object containing all loaded outputs.
""" """
# make a _Results object to store # make a Results object to store
# the output. If we are decorating # the output. If we are decorating
# another _FileOrThing, the # another FileOrThing, the
# results will get merged together # results will get merged together
# into a single _Results dict. # into a single Results dict.
if not isinstance(result, _FileOrThing._Results): if not isinstance(result, FileOrThing.Results):
result = _FileOrThing._Results(result) result = FileOrThing.Results(result)
# Load the LOADed outputs # Load the LOADed outputs
for oname, ofile in outfiles.items(): for oname, ofile in outfiles.items():
...@@ -1039,13 +1039,13 @@ def fileOrImage(*args, **kwargs): ...@@ -1039,13 +1039,13 @@ def fileOrImage(*args, **kwargs):
raise RuntimeError('Cannot handle type: {}'.format(intypes)) raise RuntimeError('Cannot handle type: {}'.format(intypes))
def decorator(func): def decorator(func):
fot = _FileOrThing(func, fot = FileOrThing(func,
prepIn, prepIn,
prepOut, prepOut,
load, load,
fslimage.removeExt, fslimage.removeExt,
*args, *args,
**kwargs) **kwargs)
def wrapper(*args, **kwargs): def wrapper(*args, **kwargs):
result = fot(*args, **kwargs) result = fot(*args, **kwargs)
...@@ -1081,13 +1081,13 @@ def fileOrArray(*args, **kwargs): ...@@ -1081,13 +1081,13 @@ def fileOrArray(*args, **kwargs):
except Exception: return None except Exception: return None
def decorator(func): def decorator(func):
fot = _FileOrThing(func, fot = FileOrThing(func,
prepIn, prepIn,
prepOut, prepOut,
load, load,
fslpath.removeExt, fslpath.removeExt,
*args, *args,
**kwargs) **kwargs)
def wrapper(*args, **kwargs): def wrapper(*args, **kwargs):
return fot(*args, **kwargs) return fot(*args, **kwargs)
...@@ -1148,13 +1148,13 @@ def fileOrText(*args, **kwargs): ...@@ -1148,13 +1148,13 @@ def fileOrText(*args, **kwargs):
except Exception: return None except Exception: return None
def decorator(func): def decorator(func):
fot = _FileOrThing(func, fot = FileOrThing(func,
prepIn, prepIn,
prepOut, prepOut,
load, load,
fslpath.removeExt, fslpath.removeExt,
*args, *args,
**kwargs) **kwargs)
def wrapper(*args, **kwargs): def wrapper(*args, **kwargs):
return fot(*args, **kwargs) return fot(*args, **kwargs)
......
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