Skip to content

Commit

Permalink
Reuse prepend_text and append_text in `verdi computer/code duplic…
Browse files Browse the repository at this point in the history
…ate` (#3788)

This required adding a new `ContextualDefaultOption` class to get a
default callable that also receives the context. Moreover, a bug was
fixed in the function that prompts for the `prepend/append_text`, as if
the file was not touched/modified, instead of reusing the text provided
in input, an empty text was used instead.

Co-authored-by: ConradJohnston <40352432+ConradJohnston@users.noreply.github.com>
  • Loading branch information
giovannipizzi and ConradJohnston authored Feb 28, 2020
1 parent 67d4504 commit d44dcbd
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 7 deletions.
4 changes: 2 additions & 2 deletions aiida/cmdline/commands/cmd_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ def setup_code(non_interactive, **kwargs):
@options_code.REMOTE_ABS_PATH(contextual_default=partial(get_default, 'remote_abs_path'))
@options_code.FOLDER(contextual_default=partial(get_default, 'code_folder'))
@options_code.REL_PATH(contextual_default=partial(get_default, 'code_rel_path'))
@options.PREPEND_TEXT()
@options.APPEND_TEXT()
@options.PREPEND_TEXT(cls=options.ContextualDefaultOption, contextual_default=partial(get_default, 'prepend_text'))
@options.APPEND_TEXT(cls=options.ContextualDefaultOption, contextual_default=partial(get_default, 'append_text'))
@options.NON_INTERACTIVE()
@click.option('--hide-original', is_flag=True, default=False, help='Hide the code being copied.')
@click.pass_context
Expand Down
8 changes: 6 additions & 2 deletions aiida/cmdline/commands/cmd_computer.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,8 +277,12 @@ def computer_setup(ctx, non_interactive, **kwargs):
@options_computer.WORKDIR(contextual_default=partial(get_parameter_default, 'work_dir'))
@options_computer.MPI_RUN_COMMAND(contextual_default=partial(get_parameter_default, 'mpirun_command'))
@options_computer.MPI_PROCS_PER_MACHINE(contextual_default=partial(get_parameter_default, 'mpiprocs_per_machine'))
@options.PREPEND_TEXT()
@options.APPEND_TEXT()
@options.PREPEND_TEXT(
cls=options.ContextualDefaultOption, contextual_default=partial(get_parameter_default, 'prepend_text')
)
@options.APPEND_TEXT(
cls=options.ContextualDefaultOption, contextual_default=partial(get_parameter_default, 'append_text')
)
@options.NON_INTERACTIVE()
@click.pass_context
@with_dbenv()
Expand Down
1 change: 1 addition & 0 deletions aiida/cmdline/params/options/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from .. import types
from .multivalue import MultipleValueOption
from .overridable import OverridableOption
from .contextualdefault import ContextualDefaultOption
from .config import ConfigFileOption

__all__ = (
Expand Down
32 changes: 32 additions & 0 deletions aiida/cmdline/params/options/contextualdefault.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# -*- coding: utf-8 -*-
###########################################################################
# 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 #
###########################################################################
"""
.. py:module::contextualdefault
:synopsis: Tools for options which allow for a default callable that needs
also the context ctx
"""

import click


class ContextualDefaultOption(click.Option):
"""A class that extends click.Option allowing to define a default callable
that also get the context ctx as a parameter.
"""

def __init__(self, *args, contextual_default=None, **kwargs):
self._contextual_default = contextual_default
super().__init__(*args, **kwargs)

def get_default(self, ctx):
"""If a contextual default is defined, use it, otherwise behave normally."""
if self._contextual_default is None:
return super().get_default(ctx)
return self._contextual_default(ctx)
2 changes: 1 addition & 1 deletion aiida/cmdline/utils/multi_line_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def edit_pre_post(pre=None, post=None, summary=None):
pre = re.sub(r'(^#=.*$\n)+', '', pre, flags=re.M).strip()
post = re.sub(r'(^#=.*$\n)+', '', post, flags=re.M).strip()
else:
pre, post = ('', '')
pre, post = (pre or '', post or '')
return pre, post


Expand Down
6 changes: 4 additions & 2 deletions tests/cmdline/commands/test_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ def setUp(self):
)
code.label = 'code'
code.description = 'desc'
code.set_prepend_text('text to prepend')
code.set_append_text('text to append')
code.store()
self.code = code

Expand Down Expand Up @@ -237,7 +239,7 @@ def test_code_show(self):
self.assertTrue(str(self.code.pk) in result.output)

def test_code_duplicate_interactive(self):
"""Test code duplication interacgtive."""
"""Test code duplication interactive."""
os.environ['VISUAL'] = 'sleep 1; vim -cwq'
os.environ['EDITOR'] = 'sleep 1; vim -cwq'
label = 'code_duplicate_interactive'
Expand All @@ -252,7 +254,7 @@ def test_code_duplicate_interactive(self):
self.assertEqual(self.code.get_append_text(), new_code.get_append_text())

def test_code_duplicate_non_interactive(self):
"""Test code duplication non-interacgtive."""
"""Test code duplication non-interactive."""
label = 'code_duplicate_noninteractive'
result = self.cli_runner.invoke(code_duplicate, ['--non-interactive', '--label=' + label, str(self.code.pk)])
self.assertIsNone(result.exception, result.output)
Expand Down
2 changes: 2 additions & 0 deletions tests/cmdline/commands/test_computer.py
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,8 @@ def setUpClass(cls, *args, **kwargs):
workdir='/tmp/aiida'
)
cls.comp.set_default_mpiprocs_per_machine(1)
cls.comp.set_prepend_text('text to prepend')
cls.comp.set_append_text('text to append')
cls.comp.store()

def setUp(self):
Expand Down

0 comments on commit d44dcbd

Please sign in to comment.