Skip to content

Commit

Permalink
Remove usage of aiida.cmdline.utils.echo from non CLI code (#2829)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
sphuber authored May 2, 2019
1 parent 848b863 commit 948fa0c
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 24 deletions.
39 changes: 21 additions & 18 deletions aiida/cmdline/commands/cmd_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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))
5 changes: 2 additions & 3 deletions aiida/orm/groups.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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}

Expand Down Expand Up @@ -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):
Expand Down
5 changes: 3 additions & 2 deletions aiida/tools/visualization/graphviz.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion docs/source/verdi/verdi_user_guide.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 948fa0c

Please sign in to comment.