Skip to content

Commit

Permalink
Catch AttributeError in _prepare_yaml
Browse files Browse the repository at this point in the history
of `orm.PortableCode`. Otherwise, the `_exportcontent` method of
`orm.Data` will fail, due to the discrepancy of the properties defined
in the pydantic model and what is actually being set as attributes of
the class.
  • Loading branch information
GeigerJ2 committed Oct 2, 2024
1 parent 5e08600 commit 1ac66a4
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions src/aiida/orm/nodes/data/code/abstract.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from __future__ import annotations

import abc
import contextlib
import functools
import pathlib
import typing as t
Expand Down Expand Up @@ -382,23 +383,24 @@ def get_builder(self) -> 'ProcessBuilder':

return builder

def _prepare_yml(self, *args, **kwargs):
"""Export code to a yml file."""
def _prepare_yaml(self, *args, **kwargs):
"""Export code to a YAML file."""
import yaml

code_data = {}
sort = kwargs.get('sort', False)

for key in self.Model.model_fields.keys():
value = getattr(self, key).label if key == 'computer' else getattr(self, key)
with contextlib.suppress(AttributeError):
value = getattr(self, key).label if key == 'computer' else getattr(self, key)

# If the attribute is not set, for example ``with_mpi`` do not export it, so that there are no null-values
# in the resulting YAML file
if value is not None:
code_data[key] = str(value)
# If the attribute is not set, for example ``with_mpi`` do not export it
# so that there are no null-values in the resulting YAML file
if value is not None:
code_data[key] = str(value)

return yaml.dump(code_data, sort_keys=sort, encoding='utf-8'), {}

def _prepare_yaml(self, *args, **kwargs):
def _prepare_yml(self, *args, **kwargs):
"""Also allow for export as .yaml"""
return self._prepare_yml(*args, **kwargs)
return self._prepare_yaml(*args, **kwargs)

0 comments on commit 1ac66a4

Please sign in to comment.