Skip to content

Commit caac0c6

Browse files
committed
Implement is_fairworkflow() decorator, execution works in notebook test
1 parent 035bf5b commit caac0c6

2 files changed

Lines changed: 37 additions & 2 deletions

File tree

fairworkflows/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
from ._version import __version__
22
from .fairstep import FairStep, FairVariable, mark_as_fairstep
3-
from .fairworkflow import FairWorkflow
3+
from .fairworkflow import FairWorkflow, is_fairworkflow

fairworkflows/fairworkflow.py

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from pathlib import Path
44
from tempfile import TemporaryDirectory
55
from typing import Iterator, Optional
6+
import inspect
67

78
import networkx as nx
89
import rdflib
@@ -69,7 +70,9 @@ def from_rdf(cls, rdf: rdflib.Graph, uri: str,
6970
return self
7071

7172
@classmethod
72-
def from_noodles_promise(cls, noodles_promise):
73+
def from_noodles_promise(cls, noodles_promise, description: str = None, label: str = None,
74+
is_pplan_plan: bool = True, derived_from=None):
75+
self = cls(description=description, label=label, is_pplan_plan=is_pplan_plan, derived_from=derived_from)
7376
self.noodles_promise = noodles_promise
7477

7578
def _extract_steps(self, rdf, uri, fetch_steps=False):
@@ -395,3 +398,35 @@ def __str__(self):
395398
s += self._rdf.serialize(format='trig').decode('utf-8')
396399
return s
397400

401+
402+
def is_fairworkflow(label: str = None, is_pplan_plan: bool = True):
403+
"""Mark a function as returning a FAIR workflow.
404+
405+
Use as decorator to mark a function as a FAIR workflow. Set properties of the fair workflow using
406+
arguments to the decorator.
407+
408+
The raw code of the function will be used to set the description of the fair workflow.
409+
410+
The type annotations of the input arguments and return statement will be used to
411+
automatically set inputs and outputs of the FAIR workflow.
412+
413+
Args:
414+
label (str): Label of the fair workflow (corresponds to rdfs:label predicate)
415+
is_pplan_plan (str): Denotes whether this workflow is a pplan:Plan
416+
417+
"""
418+
def _modify_function(func):
419+
"""
420+
Store FairStep object as _fairstep attribute of the function. Use inspection to get the
421+
description, inputs, and outputs of the step based on the function specification.
422+
"""
423+
def _wrapper(*args, **kwargs):
424+
promise = func(*args, **kwargs)
425+
426+
# Description of workflow is the raw function code
427+
description = inspect.getsource(func)
428+
429+
return FairWorkflow.from_noodles_promise(promise, description=description, label=label,
430+
is_pplan_plan=is_pplan_plan, derived_from=None)
431+
return _wrapper
432+
return _modify_function

0 commit comments

Comments
 (0)