From 80045ae7308de673a838c152998a6257c9a79134 Mon Sep 17 00:00:00 2001 From: Ahmed Basem <62952819+AhmedBasem20@users.noreply.github.com> Date: Wed, 22 Feb 2023 17:58:43 +0200 Subject: [PATCH] CLI: Allow specifying output filename in `verdi node graph generate` (#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 --- aiida/cmdline/commands/cmd_node.py | 13 ++++++++----- tests/cmdline/commands/test_node.py | 9 +++++++++ 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/aiida/cmdline/commands/cmd_node.py b/aiida/cmdline/commands/cmd_node.py index 1743036c14..50502fddc2 100644 --- a/aiida/cmdline/commands/cmd_node.py +++ b/aiida/cmdline/commands/cmd_node.py @@ -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). @@ -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') diff --git a/tests/cmdline/commands/test_node.py b/tests/cmdline/commands/test_node.py index a20f2a3fac..a7b885711d 100644 --- a/tests/cmdline/commands/test_node.py +++ b/tests/cmdline/commands/test_node.py @@ -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...'