Skip to content
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

Add CalculationTools base and entry point aiida.tools.calculations #2331

Merged
Merged
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
34 changes: 34 additions & 0 deletions aiida/orm/node/process/calculation/calcjob.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,40 @@ class CalcJobNode(CalculationNode):
JOB_STATE_KEY = 'state'
JOB_STATE_ATTRIBUTE_KEY = 'attributes.{}'.format(JOB_STATE_KEY)

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

@property
def tools(self):
"""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.common.exceptions import MultipleEntryPointError, MissingEntryPointError, LoadingEntryPointError
from aiida.plugins.entry_point import is_valid_entry_point_string, get_entry_point_from_string, load_entry_point
from aiida.tools.calculations import CalculationTools

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

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

try:
tools_class = load_entry_point('aiida.tools.calculations', entry_point.name)
self._tools = tools_class(self)
except (MultipleEntryPointError, MissingEntryPointError, LoadingEntryPointError) as exception:
self._tools = CalculationTools(self)
self.logger.warning('could not load the calculation tools entry point {}: {}'.format(
entry_point.name, exception))

return self._tools

def __dir__(self):
"""
Allow to list all valid attributes, adding also the use_* methods
Expand Down
7 changes: 7 additions & 0 deletions aiida/orm/node/process/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ class ProcessNode(Sealable, Node):
# Specific sub classes should be marked as cacheable when appropriate
_cacheable = False

def __str__(self):
base = super(ProcessNode, self).__str__()
if self.process_type:
return '{} ({})'.format(base, self.process_type)

return '{}'.format(base)

@classproperty
def _updatable_attributes(cls):
return super(ProcessNode, cls)._updatable_attributes + (
Expand Down
7 changes: 7 additions & 0 deletions aiida/tools/calculations/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# -*- coding: utf-8 -*-
# pylint: disable=wildcard-import,undefined-variable
"""Calculation tool plugins for Calculation classes."""

from .base import *

__all__ = (base.__all__)
17 changes: 17 additions & 0 deletions aiida/tools/calculations/base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# -*- coding: utf-8 -*-
"""Base class for CalculationTools

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__ = ('CalculationTools',)


class CalculationTools(object):
"""Base class for CalculationTools."""

# pylint: disable=too-few-public-methods

def __init__(self, node):
self._node = node
2 changes: 2 additions & 0 deletions setup.json
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,8 @@
"tcod = aiida.tools.dbexporters.tcod"
],
"aiida.tests": [],
"aiida.tools.calculations": [
],
"aiida.tools.dbimporters": [
"cod = aiida.tools.dbimporters.plugins.cod:CodDbImporter",
"icsd = aiida.tools.dbimporters.plugins.icsd:IcsdDbImporter",
Expand Down