From 948fa0cf1a195d4d2dc59859f10094964e295601 Mon Sep 17 00:00:00 2001 From: Sebastiaan Huber Date: Thu, 2 May 2019 20:24:49 +0200 Subject: [PATCH] Remove usage of `aiida.cmdline.utils.echo` from non CLI code (#2829) The `echo` functions should only be used for code that is directly called in command line interface utilities. Internal code should simply communicate by throwing exceptions. --- aiida/cmdline/commands/cmd_graph.py | 39 ++++++++++++++------------ aiida/orm/groups.py | 5 ++-- aiida/tools/visualization/graphviz.py | 5 ++-- docs/source/verdi/verdi_user_guide.rst | 2 +- 4 files changed, 27 insertions(+), 24 deletions(-) diff --git a/aiida/cmdline/commands/cmd_graph.py b/aiida/cmdline/commands/cmd_graph.py index 55f7dfd6f8..d044ddf77c 100644 --- a/aiida/cmdline/commands/cmd_graph.py +++ b/aiida/cmdline/commands/cmd_graph.py @@ -11,6 +11,7 @@ from __future__ import division from __future__ import print_function from __future__ import absolute_import + import click from aiida.cmdline.commands.cmd_verdi import verdi @@ -31,36 +32,38 @@ def verdi_graph(): @click.option( '-a', '--ancestor-depth', - help="The maximum depth when recursing upwards, if not set it will recurse to the end", + help='The maximum depth when recursing upwards, if not set it will recurse to the end.', type=click.IntRange(min=0)) @click.option( '-d', '--descendant-depth', - help="The maximum depth when recursing through the descendants, if not set it will recurse to the end", + help='The maximum depth when recursing through the descendants, if not set it will recurse to the end', type=click.IntRange(min=0)) -@click.option('-o', '--outputs', is_flag=True, help="Always show all outputs of a calculation") -@click.option('-i', '--inputs', is_flag=True, help="Always show all inputs of a calculation") +@click.option('-o', '--outputs', is_flag=True, help='Always show all outputs of a calculation.') +@click.option('-i', '--inputs', is_flag=True, help='Always show all inputs of a calculation.') @click.option( '-f', '--output-format', - help="The output format, something that can be recognized by graphviz" - "(see http://www.graphviz.org/doc/info/output.html)", + help='The output format, something that can be recognized by graphviz' + '(see http://www.graphviz.org/doc/info/output.html).', default='dot') @decorators.with_dbenv() def generate(root_node, ancestor_depth, descendant_depth, outputs, inputs, output_format): - """ - Generate a graph from a given ROOT_NODE user-specified by its pk. - """ + """Generate a graph for a given ROOT_NODE.""" from aiida.tools.visualization.graphviz import draw_graph - exit_status, output_file_name = draw_graph( - root_node, - ancestor_depth=ancestor_depth, - descendant_depth=descendant_depth, - image_format=output_format, - include_calculation_inputs=inputs, - include_calculation_outputs=outputs) + try: + exit_status, output_file_name = draw_graph( + root_node, + ancestor_depth=ancestor_depth, + descendant_depth=descendant_depth, + image_format=output_format, + include_calculation_inputs=inputs, + include_calculation_outputs=outputs) + except OSError as exception: + echo.echo_critical(str(exception)) + if exit_status: - echo.echo_critical("Failed to generate graph") + echo.echo_critical('Failed to generate graph') else: - echo.echo_success("Output file is {}".format(output_file_name)) + echo.echo_success('Output file is {}'.format(output_file_name)) diff --git a/aiida/orm/groups.py b/aiida/orm/groups.py index ab85b5e0ea..b9d266772c 100644 --- a/aiida/orm/groups.py +++ b/aiida/orm/groups.py @@ -15,7 +15,6 @@ from enum import Enum import six -from aiida.cmdline.utils import echo from aiida.common import exceptions from aiida.common.lang import type_check from aiida.manage.manager import get_manager @@ -67,7 +66,7 @@ def get_or_create(self, label=None, **kwargs): kwargs['type_string'] = kwargs.pop('type') warnings.warn('type is deprecated, use type_string instead', DeprecationWarning) # pylint: disable=no-member if not label: - echo.echo_critical("Group label must be provided") + raise ValueError('Group label must be provided') filters = {'label': label} @@ -131,7 +130,7 @@ def __init__(self, type_string = type warnings.warn('type is deprecated, use type_string instead', DeprecationWarning) # pylint: disable=no-member if not label: - echo.echo_critical("Group label must be provided") + raise ValueError('Group label must be provided') # Check that chosen type_string is allowed if not isinstance(type_string, six.string_types): diff --git a/aiida/tools/visualization/graphviz.py b/aiida/tools/visualization/graphviz.py index 8b517e0365..89d960a093 100644 --- a/aiida/tools/visualization/graphviz.py +++ b/aiida/tools/visualization/graphviz.py @@ -202,8 +202,9 @@ def draw_link_settings(inp_id, out_id, link_label, link_type): try: exit_code = subprocess.call(['dot', '-T', image_format, fname, '-o', output_file_name]) except OSError: - from aiida.cmdline.utils import echo - echo.echo_critical('Operating system error - perhaps Graphviz is not installed?') + raise OSError('call to `dot` failed: perhaps graphviz is not installed?') + # cleaning up by removing the temporary file os.remove(fname) + return exit_code, output_file_name diff --git a/docs/source/verdi/verdi_user_guide.rst b/docs/source/verdi/verdi_user_guide.rst index 97a950523c..337238f3a8 100644 --- a/docs/source/verdi/verdi_user_guide.rst +++ b/docs/source/verdi/verdi_user_guide.rst @@ -445,7 +445,7 @@ Below is a list with all available subcommands. --help Show this message and exit. Commands: - generate Generate a graph from a given ROOT_NODE user-specified by its... + generate Generate a graph for a given ROOT_NODE. .. _verdi_group: