Skip to content

Commit 06f8f4c

Browse files
authoredMay 28, 2024··
CLI: verdi node graph generate root nodes as arguments (#6427)
Recently, the command was changed to support specifying multiple root nodes. To keep backwards compatibility, the `-N/--nodes` option was added. This led to some pretty awkward behavior though with also the output file being defined as an argument being deprecated in favor of the `-O/--output-file` option. If backwards compatibility wasn't a concern, a better interface would be to take root nodes as positional arguments, which is the standard across `verdi` commands. Since this is a more intuitive and consistent design, it is adopted here despite it breaking backwards compatibility.
1 parent 9e3ebf6 commit 06f8f4c

File tree

2 files changed

+11
-49
lines changed

2 files changed

+11
-49
lines changed
 

‎src/aiida/cmdline/commands/cmd_node.py

+7-37
Original file line numberDiff line numberDiff line change
@@ -425,8 +425,7 @@ def verdi_graph():
425425

426426

427427
@verdi_graph.command('generate')
428-
@arguments.NODE('root_node', required=False)
429-
@options.NODES(help='The root node(s) whose provenance graph to include.')
428+
@arguments.NODES('root_nodes')
430429
@click.option(
431430
'-l',
432431
'--link-types',
@@ -477,15 +476,12 @@ def verdi_graph():
477476
@click.option(
478477
'-O',
479478
'--output-file',
480-
'output_filename',
481479
type=click.Path(dir_okay=False, path_type=pathlib.Path),
482480
help='The file to write the output to.',
483481
)
484-
@arguments.OUTPUT_FILE(required=False)
485482
@decorators.with_dbenv()
486483
def graph_generate(
487-
root_node,
488-
nodes,
484+
root_nodes,
489485
link_types,
490486
identifier,
491487
ancestor_depth,
@@ -496,44 +492,18 @@ def graph_generate(
496492
output_format,
497493
highlight_classes,
498494
show,
499-
output_filename,
500495
output_file,
501496
):
502497
"""Generate a graph from one or multiple root nodes."""
503498
from aiida.cmdline.utils import echo
504499
from aiida.tools.visualization import Graph
505500

506-
if root_node and nodes:
507-
echo.echo_warning(
508-
'Specifying the root node positionally and the `-N/--nodes` option at the same time is not supported, '
509-
'ignoring the `ROOT_NODE`. Please use the `-N/--nodes` option only.'
510-
)
511-
root_node = None
512-
513-
if root_node:
514-
echo.echo_deprecated(
515-
'Specifying the root node positionally is deprecated, please use the `-N/--nodes` option instead.'
516-
)
517-
518-
root_nodes = nodes or [root_node]
519-
520-
if output_file and output_filename:
521-
echo.echo_warning(
522-
'Specifying the output file positionally and the `-O/--output-file` option at the same time is not '
523-
'supported, ignoring the `OUTPUF_FILE`. Please use the `-O/--output-file` option only.'
524-
)
525-
output_file = None
526-
527-
if output_file:
528-
echo.echo_deprecated(
529-
'Specifying the output file positionally is deprecated, please use the `-O/--output-file` option instead.'
530-
)
531-
532-
output_filename = output_file or output_filename
501+
if not root_nodes:
502+
echo.echo_critical('No root node(s) specified.')
533503

534-
if not output_filename:
504+
if not output_file:
535505
pks = '.'.join(str(n.pk) for n in root_nodes)
536-
output_filename = pathlib.Path(f'{pks}.{engine}.{output_format}')
506+
output_file = pathlib.Path(f'{pks}.{engine}.{output_format}')
537507

538508
echo.echo_info(f'Initiating graphviz engine: {engine}')
539509
graph = Graph(engine=engine, node_id_type=identifier)
@@ -559,7 +529,7 @@ def graph_generate(
559529
highlight_classes=highlight_classes,
560530
)
561531

562-
filename_written = graph.graphviz.render(outfile=output_filename, format=output_format, view=show, cleanup=True)
532+
filename_written = graph.graphviz.render(outfile=output_file, format=output_format, view=show, cleanup=True)
563533

564534
echo.echo_success(f'Output written to `{filename_written}`')
565535

‎tests/cmdline/commands/test_node.py

+4-12
Original file line numberDiff line numberDiff line change
@@ -278,21 +278,13 @@ def test_generate_graph(self, run_cli_command):
278278
delete_temporary_file(filename)
279279

280280
def test_multiple_nodes(self, run_cli_command):
281-
"""Test the `-N/--nodes` option to specify multiple root nodes."""
281+
"""Test multiple root nodes."""
282282
node = orm.Data().store()
283-
options = ['-N', str(self.node.pk), str(node.pk)]
283+
options = [str(self.node.pk), str(node.pk)]
284284
result = run_cli_command(cmd_node.graph_generate, options)
285285
assert 'Success: Output written to' in result.output
286286
assert os.path.isfile(f'{self.node.pk}.{node.pk}.dot.pdf')
287287

288-
def test_deprecation_warnings(self, run_cli_command):
289-
"""Test that the positional arguments are deprecated and emit a warning."""
290-
options = [str(self.node.pk), 'output.pdf']
291-
result = run_cli_command(cmd_node.graph_generate, options)
292-
assert 'Specifying the root node positionally is deprecated' in result.output
293-
assert 'Specifying the output file positionally is deprecated' in result.output
294-
assert os.path.isfile('output.pdf')
295-
296288
def test_catch_bad_pk(self, run_cli_command):
297289
"""Test that an invalid root_node pk (non-numeric, negative, or decimal),
298290
or non-existent pk will produce an error
@@ -409,11 +401,11 @@ def test_node_id_label_format(self, run_cli_command):
409401

410402
@pytest.mark.parametrize('output_file', ('without_extension', 'without_extension.pdf'))
411403
def test_output_file(self, run_cli_command, output_file):
412-
"""Test that the output file can be specified through an argument."""
404+
"""Test that the output file can be specified through ``-O/--output-file``."""
413405
with warnings.catch_warnings():
414406
warnings.simplefilter('ignore')
415407
try:
416-
run_cli_command(cmd_node.graph_generate, [str(self.node.pk), output_file])
408+
run_cli_command(cmd_node.graph_generate, [str(self.node.pk), '--output-file', output_file])
417409
assert os.path.isfile(output_file)
418410
finally:
419411
delete_temporary_file(output_file)

0 commit comments

Comments
 (0)
Please sign in to comment.