Skip to content

Commit

Permalink
CLI: Add --raw option to verdi code list (#5763)
Browse files Browse the repository at this point in the history
If the flag is specified, the output will only print the projected data
without decorative table graphics or additional information.
  • Loading branch information
sphuber authored Nov 16, 2022
1 parent 40e13cd commit 17b3fb8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
14 changes: 9 additions & 5 deletions aiida/cmdline/commands/cmd_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,10 +320,11 @@ def relabel(code, label):
@options.ALL(help='Include hidden codes.')
@options.ALL_USERS(help='Include codes from all users.')
@options.PROJECT(type=click.Choice(VALID_PROJECTIONS.keys()), default=['full_label', 'pk', 'entry_point'])
@options.RAW()
@click.option('-o', '--show-owner', 'show_owner', is_flag=True, default=False, help='Show owners of codes.')
@with_dbenv()
def code_list(computer, default_calc_job_plugin, all_entries, all_users, show_owner, project):
# pylint: disable=too-many-branches
def code_list(computer, default_calc_job_plugin, all_entries, all_users, raw, show_owner, project):
# pylint: disable=too-many-branches,too-many-locals
"""List the available codes."""
from aiida import orm
from aiida.orm.utils.node import load_node_class
Expand Down Expand Up @@ -379,7 +380,8 @@ def code_list(computer, default_calc_job_plugin, all_entries, all_users, show_ow
return

table = []
headers = [projection.replace('_', ' ').capitalize() for projection in project]
headers = [projection.replace('_', ' ').capitalize() for projection in project] if not raw else []
table_format = 'plain' if raw else None

for result in query.iterdict():
row = []
Expand All @@ -392,5 +394,7 @@ def code_list(computer, default_calc_job_plugin, all_entries, all_users, show_ow
row.append('@'.join(str(result[entity][projection]) for entity, projection in VALID_PROJECTIONS[key]))
table.append(row)

echo.echo(tabulate.tabulate(table, headers=headers))
echo.echo_report('\nUse `verdi code show IDENTIFIER` to see details for a code', prefix=False)
echo.echo(tabulate.tabulate(table, headers=headers, tablefmt=table_format))

if not raw:
echo.echo_report('\nUse `verdi code show IDENTIFIER` to see details for a code', prefix=False)
10 changes: 10 additions & 0 deletions tests/cmdline/commands/test_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,16 @@ def test_code_list_hide(run_cli_command, code):
assert code.full_label in result.output


@pytest.mark.usefixtures('aiida_profile')
def test_code_list_raw(run_cli_command, code):
"""Test ``verdi code list --raw``."""
options = ['--raw']
result = run_cli_command(cmd_code.code_list, options)
assert str(code.pk) in result.output
assert 'Full label' not in result.output
assert 'Use `verdi code show IDENTIFIER`' not in result.output


@pytest.mark.usefixtures('aiida_profile_clean')
def test_hide_one(run_cli_command, code):
"""Test ``verdi code hide``."""
Expand Down

0 comments on commit 17b3fb8

Please sign in to comment.