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

Added load_plugin_safe method to return calc node if plugin is not available #1185

Merged
merged 4 commits into from
Feb 23, 2018
Merged
Show file tree
Hide file tree
Changes from 2 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
9 changes: 1 addition & 8 deletions aiida/backends/djsite/db/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,21 +186,14 @@ def get_aiida_class(self):
from aiida.orm.node import Node
from aiida.common.old_pluginloader import from_type_to_pluginclassname
from aiida.common.pluginloader import load_plugin_safe
from aiida.common import aiidalogger

try:
pluginclassname = from_type_to_pluginclassname(self.type)
except DbContentError:
raise DbContentError("The type name of node with pk= {} is "
"not valid: '{}'".format(self.pk, self.type))

try:
PluginClass = load_plugin_safe(Node, 'aiida.orm', pluginclassname)
except MissingPluginError:

aiidalogger.error("Unable to find plugin for type '{}' (node= {}), "
"will use base Node class".format(self.type, self.pk))
PluginClass = Node
PluginClass = load_plugin_safe(Node, 'aiida.orm', pluginclassname, self.type, self.pk)

return PluginClass(dbnode=self)

Expand Down
7 changes: 1 addition & 6 deletions aiida/backends/sqlalchemy/models/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,12 +161,7 @@ def get_aiida_class(self):
raise DbContentError("The type name of node with pk= {} is "
"not valid: '{}'".format(self.pk, self.type))

try:
PluginClass = load_plugin_safe(Node, 'aiida.orm', pluginclassname)
except MissingPluginError:
aiidalogger.error("Unable to find plugin for type '{}' (node= {}), "
"will use base Node class".format(self.type, self.pk))
PluginClass = Node
PluginClass = load_plugin_safe(Node, 'aiida.orm', pluginclassname, self.type, self.pk)

return PluginClass(dbnode=self)

Expand Down
6 changes: 6 additions & 0 deletions aiida/backends/tests/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1453,6 +1453,12 @@ def test_load_plugin_safe(self):
obj = kpoint.dbnode.get_aiida_class()
self.assertEqual(type(data), type(obj))

###### for node
n1 = Node().store()
obj = n1.dbnode.get_aiida_class()
self.assertEqual(type(n1), type(obj))



class TestSubNodesAndLinks(AiidaTestCase):

Expand Down
24 changes: 19 additions & 5 deletions aiida/common/pluginloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,15 +125,19 @@ def get_plugin(category, name):

return plugin

def load_plugin_safe(base_class, plugins_module, plugin_type):
def load_plugin_safe(base_class, plugins_module, plugin_type, nodeType, nodePk):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

all good, but I just realised that you should use names_with_underscores for variables instead of camelCase, or we risk that people get confused when specifying the variable names of a function (the same inside the functions).

"""
It is a copy of load_plugin function to return closely related node class
if plugin is not available. We are duplicating load_plugin function to not break
its default behaviour.
It is a wrapper of load_plugin function to return closely related node class
if plugin is not available. By default it returns base Node class and does not
raise exception.

params: Look at the docstring of aiida.common.old_pluginloader.load_plugin for more Info +
:param: nodeType: type of the node
:param nodePk: node pk

params: Look at the docstring of aiida.common.old_pluginloader.load_plugin for more Info
:return: The plugin class
"""
from aiida.common import aiidalogger

try:
PluginClass = load_plugin(base_class, plugins_module, plugin_type)
Expand Down Expand Up @@ -165,6 +169,16 @@ def load_plugin_safe(base_class, plugins_module, plugin_type):
else:
PluginClass = load_plugin(base_class, plugins_module, 'calculation.Calculation')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing else (for baseNodeType). If there is a node of a different baseNodeType, this will crash as PluginClass is not defined. This e.g. will happen for Node (that starts with node.Node. I think).
Instead, put an else, and return Node with a warning as above:

aiidalogger.error("Unable to find plugin for type '{}' (node= {}), "
                                "will use base Node class".format(self.type, self.pk))
              PluginClass = Node

Maybe add also the case where the baseNodeType is 'node' so you don't get a warning in that case. Can you add a test for Node as well (just to see that it returns Node and does not crash).


## for base node
elif baseNodeType == "node":
PluginClass = base_class

## default case
else:
aiidalogger.error("Unable to find plugin for type '{}' (node= {}), "
"will use base Node class".format(nodeType, nodePk))
PluginClass = base_class

return PluginClass


Expand Down