Skip to content

Commit

Permalink
NodeTranslator: do not assume get_export_formats exists (#4188)
Browse files Browse the repository at this point in the history
The `get_all_download_formats` will try to call `get_export_formats` on
all the known plugin types. However, it is not guaranteed that all
plugins implement this method. This was properly considered in the case
that `full_type` is not None, however, in the other case the
`AttributeError` was not being caught causing the REST API to except.
  • Loading branch information
sphuber authored Jun 19, 2020
1 parent 3a57c03 commit 112f4b8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
17 changes: 7 additions & 10 deletions aiida/restapi/translator/nodes/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -416,17 +416,14 @@ def get_all_download_formats(full_type=None):
for name in get_entry_point_names(entry_point_group):
try:
node_cls = load_entry_point(entry_point_group, name)
except LoadingEntryPointError:
pass
else:
node_cls.get_export_formats()
try:
available_formats = node_cls.get_export_formats()
if available_formats:
full_type = construct_full_type(node_cls.class_node_type, '')
all_formats[full_type] = available_formats
except AttributeError:
pass
except (AttributeError, LoadingEntryPointError):
continue

if available_formats:
full_type = construct_full_type(node_cls.class_node_type, '')
all_formats[full_type] = available_formats

return all_formats

@staticmethod
Expand Down
15 changes: 15 additions & 0 deletions tests/restapi/test_translator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
"""Tests for the `aiida.restapi.translator` module."""
# pylint: disable=invalid-name
from aiida.restapi.translator.nodes.node import NodeTranslator
from aiida.orm import Data


def test_get_all_download_formats():
"""Test the `get_all_download_formats` method."""
NodeTranslator.get_all_download_formats()


def test_get_all_download_formats_missing_get_export_formats(monkeypatch):
"""Test `get_all_download_formats` does not except if a `Data` class does not implement `get_export_formats`."""
monkeypatch.delattr(Data, 'get_export_formats')
NodeTranslator.get_all_download_formats()

0 comments on commit 112f4b8

Please sign in to comment.