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

add echo_deprecated function #1648

Merged
merged 5 commits into from
Jun 18, 2018
Merged
Show file tree
Hide file tree
Changes from 4 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
5 changes: 5 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
aiida/cmdline/utils/multi_line_input.py|
aiida/cmdline/utils/test_multiline.py|
aiida/cmdline/utils/templates.py|
aiida/cmdline/utils/echo.py|
docs/update_req_for_rtd.py| # a|b -> match a OR b
.ci/test_setup.py|
aiida/transport/.*.py|
Expand All @@ -39,6 +40,10 @@
aiida/control/.*.py|
aiida/cmdline/tests/.*.py|
aiida/cmdline/commands/daemon.py|
aiida/cmdline/utils/multi_line_input.py|
aiida/cmdline/utils/test_multiline.py|
aiida/cmdline/utils/templates.py|
aiida/cmdline/utils/echo.py|
docs/update_req_for_rtd.py|
.ci/test_setup.py|
.ci/test_plugin_testcase.py|
Expand Down
3 changes: 2 additions & 1 deletion aiida/cmdline/commands/rehash.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,5 @@ def rehash(nodes, entry_point):
echo.echo('.', nl=False)
node.rehash()

echo.echo_success('{} nodes re-hashed'.format(i + 1), prefix='\n')
echo.echo('')
echo.echo_success('{} nodes re-hashed'.format(i + 1))
3 changes: 2 additions & 1 deletion aiida/cmdline/commands/work.py
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,8 @@ def plugins(entry_point):
for entry_point in entry_points:
echo.echo("* {}".format(entry_point))

echo.echo_info('Pass the entry point as an argument to display detailed information', prefix='\n')
echo.echo('')
echo.echo_info('Pass the entry point as an argument to display detailed information')
else:
echo.echo_error('No workflow plugins found')

Expand Down
90 changes: 51 additions & 39 deletions aiida/cmdline/utils/echo.py
Original file line number Diff line number Diff line change
@@ -1,108 +1,120 @@
# -*- coding: utf-8 -*-
""" Convenience functions for printing output from verdi commands """
import enum
import sys
import click


def echo(message, bold=False, prefix=None, nl=True):
#pylint: disable=too-few-public-methods
class ExitCode(enum.Enum):
"""Exit codes for the verdi command line."""
CRITICAL = 1
DEPRECATED = 80
UNKNOWN = 99


#pylint: disable=invalid-name
def echo(message, bold=False, nl=True):
"""
Print a normal message through click's echo function to stdout

:param message: the string representing the message to print
:param bold: whether to print the message in bold
:param prefix: an optional string that will be printed before the message
:param nl: whether to print a newline at the end of the message
"""
if prefix is not None:
click.secho(prefix)

click.secho(message, bold=bold, nl=nl)


def echo_info(message, bold=False, prefix=None, nl=True):
def echo_info(message, bold=False, nl=True):
"""
Print an info message through click's echo function to stdout, prefixed with 'Info:'

:param message: the string representing the message to print
:param bold: whether to print the message in bold
:param prefix: an optional string that will be printed before the message. Note that it does not automatically
include a newline character
:param nl: whether to print a newline at the end of the message
"""
if prefix is not None:
click.secho(prefix, nl=False)

click.secho('Info: ', fg='blue', bold=True, nl=False)
click.secho(message, bold=bold, nl=nl)


def echo_success(message, bold=False, prefix=None, nl=True):
def echo_success(message, bold=False, nl=True):
"""
Print a success message through click's echo function to stdout, prefixed with 'Success:'

:param message: the string representing the message to print
:param bold: whether to print the message in bold
:param prefix: an optional string that will be printed before the message. Note that it does not automatically
include a newline character
:param nl: whether to print a newline at the end of the message
"""
if prefix is not None:
click.secho(prefix, nl=False)

click.secho('Success: ', fg='green', bold=True, nl=False)
click.secho(message, bold=bold, nl=nl)


def echo_warning(message, bold=False, prefix=None, nl=True):
def echo_warning(message, bold=False, nl=True):
"""
Print a warning message through click's echo function to stdout, prefixed with 'Warning:'

:param message: the string representing the message to print
:param bold: whether to print the message in bold
:param prefix: an optional string that will be printed before the message. Note that it does not automatically
include a newline character
:param nl: whether to print a newline at the end of the message
"""
if prefix is not None:
click.secho(prefix, nl=False)

click.secho('Warning: ', fg='yellow', bold=True, nl=False)
click.secho(message, bold=bold, nl=nl)


def echo_error(message, bold=False, prefix=None, nl=True):
def echo_error(message, bold=False, nl=True):
"""
Print an error message through click's echo function to stdout, prefixed with 'Error:'

:param message: the string representing the message to print
:param bold: whether to print the message in bold
:param prefix: an optional string that will be printed before the message. Note that it does not automatically
include a newline character
:param nl: whether to print a newline at the end of the message
"""
if prefix is not None:
click.secho(prefix, nl=False)

click.secho('Error: ', fg='red', bold=True, nl=False, err=True)
click.secho(message, bold=bold, nl=nl, err=True)


def echo_critical(message, bold=False, prefix=None, nl=True, exit_status=1):
def echo_exit(message, bold=False, nl=True, exit_status=ExitCode.UNKNOWN, exit_label='Exit: '):
"""
Print an error message through click's echo function to stdout, prefixed with 'Critical:'
and then calls sys.exit with the give exit_status. This should be used to print messages for errors
that cannot be recovered from and so the script should be directly terminated with a non-zero exit
status to indicate that the command failed
Print error message through click's echo function to stdout, prefixed with
exit_label and then calls sys.exit with the given exit_status.

:param message: the string representing the message to print
:param bold: whether to print the message in bold
:param prefix: an optional string that will be printed before the message. Note that it does not automatically
include a newline character
:param nl: whether to print a newline at the end of the message
:param exit_status: the integer to pass to the sys.exit() call
:param exit_label: the prefix for the error message
"""
if prefix is not None:
click.secho(prefix, nl=False)

click.secho('Critical: ', fg='red', bold=True, nl=False, err=True)
click.secho(exit_label, fg='red', bold=True, nl=False, err=True)
click.secho(message, bold=bold, nl=nl, err=True)
sys.exit(exit_status)


def echo_critical(message, bold=False, nl=True):
"""
Print an error message through click's echo function to stdout, prefixed with 'Critical:'
and then calls sys.exit with the given exit_status.

This should be used to print messages for errors that cannot be recovered
from and so the script should be directly terminated with a non-zero exit
status to indicate that the command failed

:param message: the string representing the message to print
:param bold: whether to print the message in bold
:param nl: whether to print a newline at the end of the message
"""
echo_exit(message=message, bold=bold, nl=nl, exit_status=ExitCode.CRITICAL, exit_label='Critical: ')


def echo_deprecated(message, bold=False, nl=True):
"""
Print an error message through click's echo function to stdout, prefixed with 'Deprecated:'
and then calls sys.exit with the given exit_status.

This should be used to indicate deprecated commands.

:param message: the string representing the message to print
:param bold: whether to print the message in bold
:param nl: whether to print a newline at the end of the message
"""
echo_exit(message=message, bold=bold, nl=nl, exit_status=ExitCode.DEPRECATED, exit_label='Deprecated: ')
8 changes: 4 additions & 4 deletions aiida/cmdline/utils/multi_line_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ def edit_pre_post(pre=None, post=None, summary=None):
execution scripts for both codes and computers
"""
from aiida.cmdline.utils.templates import env
t = env.get_template('prepost.bash.tpl')
template = env.get_template('prepost.bash.tpl')
summary = summary or {}
summary = {k: v for k, v in summary.iteritems() if v}
content = t.render(default_pre=pre or '', default_post=post or '', summary=summary)
content = template.render(default_pre=pre or '', default_post=post or '', summary=summary)
mlinput = click.edit(content, extension='.bash')
if mlinput:
import re
Expand All @@ -30,8 +30,8 @@ def edit_comment(old_cmt=''):
call up an editor to edit comments to nodes in the database
"""
from aiida.cmdline.utils.templates import env
t = env.get_template('new_cmt.txt.tpl')
content = t.render(old_comment=old_cmt)
template = env.get_template('new_cmt.txt.tpl')
content = template.render(old_comment=old_cmt)
mlinput = click.edit(content, extension='.txt')
if mlinput:
import re
Expand Down
2 changes: 2 additions & 0 deletions aiida/cmdline/utils/templates.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"""Templates for input/output of verdi commands."""
from jinja2 import Environment, PackageLoader

#pylint: disable=invalid-name
env = Environment(loader=PackageLoader('aiida', 'cmdline/templates'))
1 change: 0 additions & 1 deletion aiida/cmdline/utils/test_multiline.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import os
import unittest

import click
from click.testing import CliRunner

from aiida.cmdline.utils.multi_line_input import edit_pre_post, edit_comment
Expand Down