Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Christoph Arthofer
fslpy
Commits
96736732
Commit
96736732
authored
Mar 28, 2020
by
Paul McCarthy
🚵
Browse files
RF: make FileOrThing+Results classes "public:, for type testing
parent
346147da
Changes
1
Hide whitespace changes
Inline
Side-by-side
fsl/wrappers/wrapperutils.py
View file @
96736732
...
...
@@ -403,18 +403,18 @@ def namedPositionals(func, args):
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.
"""
class
_
FileOrThing
(
object
):
class
FileOrThing
(
object
):
"""Decorator which ensures that certain arguments which are passed into the
decorated function are always passed as file names. Both positional and
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
details.
...
...
@@ -445,7 +445,7 @@ class _FileOrThing(object):
**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
accessible via an attribute called ``stdout``. All output arguments with a
value of ``LOAD`` will be present as dictionary entries, with the keyword
...
...
@@ -460,7 +460,7 @@ class _FileOrThing(object):
The above description holds in all situations, except when an argument
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
return value unchanged.
...
...
@@ -534,27 +534,27 @@ class _FileOrThing(object):
**Using with other decorators**
``
_
FileOrThing`` decorators can be chained with other ``
_
FileOrThing``
decorators, and other decorators. When multiple ``
_
FileOrThing``
``FileOrThing`` decorators can be chained with other ``FileOrThing``
decorators, and other decorators. When multiple ``FileOrThing``
decorators are used on a single function, the outputs from each decorator
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
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
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
"thing") as value.
Where possible (i.e. for outputs named with a valid Python
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
:meth:`stdout` property.
...
...
@@ -562,7 +562,7 @@ class _FileOrThing(object):
def
__init__
(
self
,
stdout
):
"""Create a ``
_
Results`` dict.
"""Create a ``Results`` dict.
:arg stdout: Return value of the decorated function (typically a
tuple containing the standard output and error of the
...
...
@@ -593,7 +593,7 @@ class _FileOrThing(object):
removeExt
,
*
args
,
**
kwargs
):
"""Initialise a ``
_
FileOrThing`` decorator.
"""Initialise a ``FileOrThing`` decorator.
:arg func: The function to be decorated.
...
...
@@ -618,7 +618,7 @@ class _FileOrThing(object):
All other positional arguments are interpreted as the names of the
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.
...
...
@@ -673,8 +673,8 @@ class _FileOrThing(object):
'or LOAD with submit=True!'
)
return
func
(
*
args
,
**
kwargs
)
# If this
_
FileOrThing is being called
# by another
_
FileOrThing don't create
# If this FileOrThing is being called
# by another FileOrThing don't create
# another working directory. We do this
# sneakily, by setting an attribute on
# the wrapped function which stores the
...
...
@@ -734,8 +734,8 @@ class _FileOrThing(object):
passed to the ``prepOut`` function specified at :meth:`__init__`.
All other arguments are passed through the ``prepIn`` function.
:arg parent: ``True`` if this ``
_
FileOrThing`` is the first in a
chain of ``
_
FileOrThing`` decorators.
:arg parent: ``True`` if this ``FileOrThing`` is the first in a
chain of ``FileOrThing`` decorators.
:arg workdir: Directory in which all temporary files should be stored.
...
...
@@ -779,7 +779,7 @@ class _FileOrThing(object):
# Prefixed outputs are only
# managed by the parent
#
_
FileOrthing in a chain of
# FileOrthing in a chain of
# FoT decorators.
if
not
parent
:
prefix
=
None
...
...
@@ -894,11 +894,11 @@ class _FileOrThing(object):
def
__generateResult
(
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. 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 result: Function return value.
...
...
@@ -909,16 +909,16 @@ class _FileOrThing(object):
:arg prefixes: Dictionary containing output-prefix patterns to be
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
# another
_
FileOrThing, the
# another FileOrThing, the
# results will get merged together
# into a single
_
Results dict.
if
not
isinstance
(
result
,
_
FileOrThing
.
_
Results
):
result
=
_
FileOrThing
.
_
Results
(
result
)
# into a single Results dict.
if
not
isinstance
(
result
,
FileOrThing
.
Results
):
result
=
FileOrThing
.
Results
(
result
)
# Load the LOADed outputs
for
oname
,
ofile
in
outfiles
.
items
():
...
...
@@ -1039,13 +1039,13 @@ def fileOrImage(*args, **kwargs):
raise
RuntimeError
(
'Cannot handle type: {}'
.
format
(
intypes
))
def
decorator
(
func
):
fot
=
_
FileOrThing
(
func
,
prepIn
,
prepOut
,
load
,
fslimage
.
removeExt
,
*
args
,
**
kwargs
)
fot
=
FileOrThing
(
func
,
prepIn
,
prepOut
,
load
,
fslimage
.
removeExt
,
*
args
,
**
kwargs
)
def
wrapper
(
*
args
,
**
kwargs
):
result
=
fot
(
*
args
,
**
kwargs
)
...
...
@@ -1081,13 +1081,13 @@ def fileOrArray(*args, **kwargs):
except
Exception
:
return
None
def
decorator
(
func
):
fot
=
_
FileOrThing
(
func
,
prepIn
,
prepOut
,
load
,
fslpath
.
removeExt
,
*
args
,
**
kwargs
)
fot
=
FileOrThing
(
func
,
prepIn
,
prepOut
,
load
,
fslpath
.
removeExt
,
*
args
,
**
kwargs
)
def
wrapper
(
*
args
,
**
kwargs
):
return
fot
(
*
args
,
**
kwargs
)
...
...
@@ -1148,13 +1148,13 @@ def fileOrText(*args, **kwargs):
except
Exception
:
return
None
def
decorator
(
func
):
fot
=
_
FileOrThing
(
func
,
prepIn
,
prepOut
,
load
,
fslpath
.
removeExt
,
*
args
,
**
kwargs
)
fot
=
FileOrThing
(
func
,
prepIn
,
prepOut
,
load
,
fslpath
.
removeExt
,
*
args
,
**
kwargs
)
def
wrapper
(
*
args
,
**
kwargs
):
return
fot
(
*
args
,
**
kwargs
)
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment