Source code for aiida.cmdline.utils.multi_line_input
###########################################################################
# Copyright (c), The AiiDA team. All rights reserved. #
# This file is part of the AiiDA code. #
# #
# The code is hosted on GitHub at https://github.com/aiidateam/aiida-core #
# For further information on the license, see the LICENSE.txt file #
# For further information please visit http://www.aiida.net #
###########################################################################
"""Utilities for getting multi line input from the commandline."""
import re
import click
[docs]
def edit_multiline_template(template_name, comment_marker='#=', extension=None, **kwargs):
"""Open a template file for editing in a text editor.
:param template: name of the template to use from the ``aiida.cmdline.templates`` directory.
:param comment_marker: the set of symbols that mark a comment line that should be stripped from the final value
:param extension: the file extension to give to the rendered template file.
:param kwargs: keywords that will be passed to the template rendering engine.
:return: the final string value entered in the editor with all comment lines stripped or an empty string if the
``click.edit`` returned ``None``.
"""
from aiida.cmdline.utils.templates import env
template = env.get_template(template_name)
rendered = template.render(**kwargs)
content = click.edit(rendered, extension=extension)
if content is not None:
# Remove all comments, which are all lines that start with the comment marker
return re.sub(f'(^{re.escape(comment_marker)}.*$\n)+', '', content, flags=re.M).strip()
return ''