Skip to content

Commit

Permalink
CLI: Allow specifying output filename in verdi node graph generate (#…
Browse files Browse the repository at this point in the history
…5897)

The `verdi node graph generate` now takes an optional argument to define
the filename to which the output should be written. The call to `render`
now uses the `outfile` argument instead of `filename` since the former
respects the specified value, whereas the latter will add a suffix that
corresponds to the requested output format.

Co-authored-by: Sebastiaan Huber <mail@sphuber.net>
  • Loading branch information
AhmedBasem20 and sphuber authored Feb 22, 2023
1 parent 34f7dee commit 80045ae
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
13 changes: 8 additions & 5 deletions aiida/cmdline/commands/cmd_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -435,10 +435,11 @@ def verdi_graph():
multiple=True
)
@click.option('-s', '--show', is_flag=True, help='Open the rendered result with the default application.')
@arguments.OUTPUT_FILE(required=False)
@decorators.with_dbenv()
def graph_generate(
root_node, link_types, identifier, ancestor_depth, descendant_depth, process_out, process_in, engine, output_format,
highlight_classes, show
highlight_classes, show, output_file
):
"""
Generate a graph from a ROOT_NODE (specified by pk or uuid).
Expand Down Expand Up @@ -468,11 +469,13 @@ def graph_generate(
include_process_inputs=process_in,
highlight_classes=highlight_classes,
)
output_file_name = graph.graphviz.render(
filename=f'{root_node.pk}.{engine}', format=output_format, view=show, cleanup=True
)

echo.echo_success(f'Output file: {output_file_name}')
if not output_file:
output_file = pathlib.Path(f'{root_node.pk}.{engine}.{output_format}')

output_file_name = graph.graphviz.render(outfile=output_file, format=output_format, view=show, cleanup=True)

echo.echo_success(f'Output written to `{output_file_name}`')


@verdi_node.group('comment')
Expand Down
9 changes: 9 additions & 0 deletions tests/cmdline/commands/test_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,15 @@ def test_node_id_label_format(self, run_cli_command):
finally:
delete_temporary_file(filename)

@pytest.mark.parametrize('output_file', ('without_extension', 'without_extension.pdf'))
def test_output_file(self, run_cli_command, output_file):
"""Test that the output file can be specified through an argument."""
try:
run_cli_command(cmd_node.graph_generate, [str(self.node.pk), output_file])
assert os.path.isfile(output_file)
finally:
delete_temporary_file(output_file)


COMMENT = 'Well I never...'

Expand Down

0 comments on commit 80045ae

Please sign in to comment.