Skip to content

Add tools property and entrypoint for workflows #6884

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 39 additions & 1 deletion src/aiida/orm/nodes/process/workflow/workchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@
###########################################################################
"""Module with `Node` sub class for workchain processes."""

from typing import Optional, Tuple
from typing import TYPE_CHECKING, Optional, Tuple

from aiida.common import exceptions
from aiida.common.lang import classproperty

from .workflow import WorkflowNode

if TYPE_CHECKING:
from aiida.tools.workflows import WorkflowTools

Check warning on line 19 in src/aiida/orm/nodes/process/workflow/workchain.py

View check run for this annotation

Codecov / codecov/patch

src/aiida/orm/nodes/process/workflow/workchain.py#L19

Added line #L19 was not covered by tests

__all__ = ('WorkChainNode',)


Expand All @@ -22,6 +26,40 @@

STEPPER_STATE_INFO_KEY = 'stepper_state_info'

# An optional entry point for a CalculationTools instance
_tools = None

@property
def tools(self) -> 'WorkflowTools':
"""Return the calculation tools that are registered for the process type associated with this calculation.

If the entry point name stored in the `process_type` of the CalcJobNode has an accompanying entry point in the
`aiida.tools.calculations` entry point category, it will attempt to load the entry point and instantiate it
passing the node to the constructor. If the entry point does not exist, cannot be resolved or loaded, a warning
will be logged and the base CalculationTools class will be instantiated and returned.

:return: CalculationTools instance
"""
from aiida.plugins.entry_point import get_entry_point_from_string, is_valid_entry_point_string, load_entry_point
from aiida.tools.workflows import WorkflowTools

Check warning on line 44 in src/aiida/orm/nodes/process/workflow/workchain.py

View check run for this annotation

Codecov / codecov/patch

src/aiida/orm/nodes/process/workflow/workchain.py#L43-L44

Added lines #L43 - L44 were not covered by tests

if self._tools is None:
entry_point_string = self.process_type

Check warning on line 47 in src/aiida/orm/nodes/process/workflow/workchain.py

View check run for this annotation

Codecov / codecov/patch

src/aiida/orm/nodes/process/workflow/workchain.py#L46-L47

Added lines #L46 - L47 were not covered by tests

if entry_point_string and is_valid_entry_point_string(entry_point_string):
entry_point = get_entry_point_from_string(entry_point_string)

Check warning on line 50 in src/aiida/orm/nodes/process/workflow/workchain.py

View check run for this annotation

Codecov / codecov/patch

src/aiida/orm/nodes/process/workflow/workchain.py#L49-L50

Added lines #L49 - L50 were not covered by tests

try:
tools_class = load_entry_point('aiida.tools.workflows', entry_point.name)
self._tools = tools_class(self)
except exceptions.EntryPointError as exception:
self._tools = WorkflowTools(self)
self.logger.warning(

Check warning on line 57 in src/aiida/orm/nodes/process/workflow/workchain.py

View check run for this annotation

Codecov / codecov/patch

src/aiida/orm/nodes/process/workflow/workchain.py#L52-L57

Added lines #L52 - L57 were not covered by tests
f'could not load the workflow tools entry point {entry_point.name}: {exception}'
)

return self._tools

Check warning on line 61 in src/aiida/orm/nodes/process/workflow/workchain.py

View check run for this annotation

Codecov / codecov/patch

src/aiida/orm/nodes/process/workflow/workchain.py#L61

Added line #L61 was not covered by tests

@classproperty
def _updatable_attributes(cls) -> Tuple[str, ...]: # type: ignore[override] # noqa: N805
return super()._updatable_attributes + (cls.STEPPER_STATE_INFO_KEY,)
Expand Down
21 changes: 21 additions & 0 deletions src/aiida/tools/workflows/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
###########################################################################
# Copyright (c), The AiiDA team. All rights reserved. #
# This file is part of the AiiDA code. #
# #
# The code is hosted on GitHub at https://github.com/aiidateam/aiida-core #
# For further information on the license, see the LICENSE.txt file #
# For further information please visit http://www.aiida.net #
###########################################################################
"""Workflow tool plugins for Workflow classes."""

# AUTO-GENERATED

# fmt: off

from .base import *

Check warning on line 15 in src/aiida/tools/workflows/__init__.py

View check run for this annotation

Codecov / codecov/patch

src/aiida/tools/workflows/__init__.py#L15

Added line #L15 was not covered by tests

__all__ = (

Check warning on line 17 in src/aiida/tools/workflows/__init__.py

View check run for this annotation

Codecov / codecov/patch

src/aiida/tools/workflows/__init__.py#L17

Added line #L17 was not covered by tests
'WorkflowTools',
)

# fmt: on
22 changes: 22 additions & 0 deletions src/aiida/tools/workflows/base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
###########################################################################
# Copyright (c), The AiiDA team. All rights reserved. #
# This file is part of the AiiDA code. #
# #
# The code is hosted on GitHub at https://github.com/aiidateam/aiida-core #
# For further information on the license, see the LICENSE.txt file #
# For further information please visit http://www.aiida.net #
###########################################################################
"""Base class for WorkflowTools

Sub-classes can be registered in the `aiida.tools.calculations` category to enable the `CalcJobNode` class from being
able to find the tools plugin, load it and expose it through the `tools` property of the `CalcJobNode`.
"""

__all__ = ('WorkflowTools',)

Check warning on line 15 in src/aiida/tools/workflows/base.py

View check run for this annotation

Codecov / codecov/patch

src/aiida/tools/workflows/base.py#L15

Added line #L15 was not covered by tests


class WorkflowTools:

Check warning on line 18 in src/aiida/tools/workflows/base.py

View check run for this annotation

Codecov / codecov/patch

src/aiida/tools/workflows/base.py#L18

Added line #L18 was not covered by tests
"""Base class for WorkflowTools."""

def __init__(self, node):
self._node = node

Check warning on line 22 in src/aiida/tools/workflows/base.py

View check run for this annotation

Codecov / codecov/patch

src/aiida/tools/workflows/base.py#L21-L22

Added lines #L21 - L22 were not covered by tests
Loading