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

ShellCalculation: Resolve escaped curly braces in arguments #102

Merged
merged 1 commit into from
Sep 5, 2024
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
18 changes: 18 additions & 0 deletions docs/source/howto.rst
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,24 @@ To pass arguments to the shell command, pass them as a string to the ``arguments

which should print something like ``2022-03-17``.

.. tip::

Curly braces ``{}`` carry specific meaning in ``arguments`` and are considered as placeholders for input files (see :ref:`this section <how-to:running-a-shell-command-with-files-as-arguments>`).
If you need literal curly braces, escape them by doubling them, `just as you would for a normal f-string <https://docs.python.org/3/library/string.html#format-string-syntax>`_:

.. code::

from aiida_shell import launch_shell_job
results, node = launch_shell_job(
'echo',
arguments='some{{curly}}braces',
)
print(results['stdout'].get_content())

which prints ``some{curly}braces``.


.. _how-to:running-a-shell-command-with-files-as-arguments:

Running a shell command with files as arguments
===============================================
Expand Down
2 changes: 1 addition & 1 deletion src/aiida_shell/calculations/shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ def process_arguments_and_nodes(

# If the argument contains no placeholders simply append the argument and continue.
if not field_names:
processed_arguments.append(argument)
processed_arguments.append(argument.format())
continue

# Otherwise we validate that there is exactly one placeholder and that a ``SinglefileData`` input node is
Expand Down
12 changes: 12 additions & 0 deletions tests/calculations/test_shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,18 @@ def test_arguments_files_filenames(generate_calc_job, generate_code):
assert code_info.cmdline_params == ['custom_filename']


def test_arguments_escaped_braces(generate_calc_job, generate_code):
"""Test the ``arguments`` with arguments containing escaped curly braces."""
arguments = List(['some{{escaped}}braces'])
inputs = {
'code': generate_code(),
'arguments': arguments,
}
_, calc_info = generate_calc_job('core.shell', inputs)
code_info = calc_info.codes_info[0]
assert code_info.cmdline_params == ['some{escaped}braces']


def test_output_filename(generate_calc_job, generate_code, file_regression):
"""Test the ``metadata.options.output_filename`` input."""
output_filename = 'custom_stdout'
Expand Down
Loading