-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The `ShellCode` data plugin is a subclass of the `InstalledCode` plugin from `aiida-core`. The new plugin is used by the `launch_shell_job` utility function whenever a code has to be created on the fly. This will serve to be able to query for these kinds of codes and to distinguish them from codes that are typically setup manually.
- Loading branch information
Showing
8 changed files
with
141 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# -*- coding: utf-8 -*- | ||
"""Module for :mod:`aiida_shell.data`.""" | ||
from .code import ShellCode | ||
|
||
__all__ = ('ShellCode',) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# -*- coding: utf-8 -*- | ||
"""Code that represents a shell command.""" | ||
from __future__ import annotations | ||
|
||
from aiida.orm import InstalledCode | ||
|
||
__all__ = ('ShellCode',) | ||
|
||
|
||
class ShellCode(InstalledCode): | ||
"""Code that represents a shell command. | ||
This code type is automatically generated by the :func:`~aiida_shell.engine.launch.launch_shell_job` function and is | ||
a subclass of :class:`~aiida.orm.nodes.data.code.installed.InstalledCode`. It can therefore be used for any other | ||
calculation job as well. | ||
""" | ||
|
||
def __init__(self, *args, default_calc_job_plugin: str = 'core.shell', **kwargs) -> None: | ||
"""Construct a new instance.""" | ||
self.validate_default_calc_job_plugin(default_calc_job_plugin) | ||
super().__init__(*args, default_calc_job_plugin=default_calc_job_plugin, **kwargs) | ||
|
||
@staticmethod | ||
def validate_default_calc_job_plugin(default_calc_job_plugin: str) -> None: | ||
"""Validate the default calculation job plugin. | ||
The ``ShellCode`` should only be used with the ``core.shell`` calculation job entry point. | ||
:raises ValueError: If ``default_calc_job_plugin`` is not ``core.shell``. | ||
""" | ||
if default_calc_job_plugin != 'core.shell': | ||
raise ValueError(f'`default_calc_job_plugin` has to be `core.shell`, but got: {default_calc_job_plugin}') | ||
|
||
@classmethod | ||
def _get_cli_options(cls) -> dict: | ||
"""Return the CLI options that would allow to create an instance of this class.""" | ||
options = super()._get_cli_options() | ||
options['default_calc_job_plugin']['default'] = 'core.shell' | ||
return options |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# -*- coding: utf-8 -*- | ||
"""Tests for :mod:`aiida_shell.data.code`.""" | ||
import pytest | ||
|
||
from aiida_shell.data.code import ShellCode | ||
|
||
|
||
def test_constructor(generate_computer): | ||
"""Test initializing an instance.""" | ||
code = ShellCode( | ||
label='bash', | ||
computer=generate_computer(), | ||
filepath_executable='/bin/bash', | ||
default_calc_job_plugin='core.shell', | ||
) | ||
assert isinstance(code, ShellCode) | ||
|
||
|
||
def test_constructor_invalid(generate_computer): | ||
"""Test the constructor raises if ``default_calc_job_plugin`` is not ``core.shell``.""" | ||
with pytest.raises(ValueError, match=r'`default_calc_job_plugin` has to be `core.shell`, but got: .*'): | ||
ShellCode( | ||
label='bash', | ||
computer=generate_computer(), | ||
filepath_executable='/bin/bash', | ||
default_calc_job_plugin='core.arithmetic.add', | ||
) | ||
|
||
|
||
@pytest.mark.parametrize(('value', 'exception'), ( | ||
('core.shell', None), | ||
('core.arithmetic.add', r'`default_calc_job_plugin` has to be `core.shell`, but got: .*'), | ||
)) | ||
def test_validate_default_calc_job_plugin(value, exception): | ||
"""Test the constructor raises if ``default_calc_job_plugin`` is not ``core.shell``.""" | ||
if exception: | ||
with pytest.raises(ValueError, match=exception): | ||
ShellCode.validate_default_calc_job_plugin(value) | ||
else: | ||
assert ShellCode.validate_default_calc_job_plugin(value) is None |