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.
    """
    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:
        # Remove all comments, which are all lines that start with the comment marker
        value = re.sub(r'(^' + re.escape(comment_marker) + '.*$\n)+',
                       '',
                       content,
                       flags=re.M).strip()

    return value
Пример #2
0
def edit_pre_post(pre=None, post=None, summary=None):
    """
    use click to call up an editor to write or edit pre / post
    execution scripts for both codes and computers
    """
    from aiida.cmdline.utils.templates import env
    template = env.get_template('prepost.bash.tpl')
    summary = summary or {}
    summary = {k: v for k, v in summary.items() if v}

    # Define a separator that will be splitting pre- and post- execution
    # parts of the submission script
    separator = '#====================================================#\n' \
                '#=               Post execution script              =#\n' \
                '#=  I am acting as a separator, do not modify me!!! =#\n' \
                '#====================================================#\n'

    content = template.render(default_pre=pre or '',
                              separator=separator,
                              default_post=post or '',
                              summary=summary)
    mlinput = click.edit(content, extension='.bash')
    if mlinput:
        import re

        # Splitting the text in pre- and post- halfs
        try:
            pre, post = mlinput.split(separator)
        except ValueError as err:
            if str(err) == 'need more than 1 value to unpack':
                raise InputValidationError(
                    'Looks like you modified the '
                    'separator that should NOT be modified. Please be '
                    'careful!')
            elif str(err) == 'too many values to unpack':
                raise InputValidationError(
                    'Looks like you have more than one '
                    'separator, while only one is needed '
                    '(and allowed). Please be careful!')
            else:
                raise err

        # Removing all the comments starting from '#=' in both pre- and post-
        # parts
        pre = re.sub(r'(^#=.*$\n)+', '', pre, flags=re.M).strip()
        post = re.sub(r'(^#=.*$\n)+', '', post, flags=re.M).strip()
    else:
        pre, post = (pre or '', post or '')
    return pre, post
def edit_comment(old_cmt=''):
    """
    call up an editor to edit comments to nodes in the database
    """
    from aiida.cmdline.utils.templates import env
    template = env.get_template('new_cmt.txt.tpl')
    content = template.render(old_comment=old_cmt)
    mlinput = click.edit(content, extension='.txt')
    if mlinput:
        regex = r'^(?!#=)(.*)$'
        cmt = '\n'.join(re.findall(regex, mlinput, flags=re.M))
        cmt = cmt.strip('\n')
    else:
        cmt = ''
    return cmt
Пример #4
0
def edit_pre_post(pre=None, post=None, summary=None):
    """
    use click to call up an editor to write or edit pre / post
    execution scripts for both codes and computers
    """
    from aiida.cmdline.utils.templates import env
    template = env.get_template('prepost.bash.tpl')
    summary = summary or {}
    summary = {k: v for k, v in summary.iteritems() if v}
    content = template.render(default_pre=pre or '',
                              default_post=post or '',
                              summary=summary)
    mlinput = click.edit(content, extension='.bash')
    if mlinput:
        import re
        stripped_input = re.sub(r'(^#.*$\n)+', '#', mlinput,
                                flags=re.M).strip('#')
        pre, post = [text.strip() for text in stripped_input.split('#')]
    else:
        pre, post = ('', '')
    return pre, post