Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion templateflow/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,9 @@ def ls(template, **kwargs):
def get(template, **kwargs):
"""Fetch the assets corresponding to template and optional filters."""
entities = {k: _nulls(v) for k, v in kwargs.items() if v != ''}
click.echo('\n'.join(f'{match}' for match in api.get(template, **entities)))
paths = api.get(template, **entities)
filenames = [str(paths)] if isinstance(paths, Path) else [str(file) for file in paths]
click.echo('\n'.join(filenames))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

perhaps past the reach of this PR - but it would be nice to print out a more helpful message with empty lists, i.e.

Suggested change
click.echo('\n'.join(filenames))
if not filenames:
click.echo('No files found.')
return
click.echo('\n'.join(filenames))

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One currently useful feature is that the output is filenames. Helpful messages on stderr are fine, but I wouldn't want to change the utility of stdout to bash scripts.

Fine for another PR, but I don't want to scope creep this one.



if __name__ == '__main__':
Expand Down
69 changes: 69 additions & 0 deletions templateflow/tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
from pathlib import Path

import click.testing
import pytest

from .. import cli


@pytest.fixture
def runner():
return click.testing.CliRunner()


def test_ls_one(runner):
result = runner.invoke(cli.main, ['ls', 'MNI152Lin', '--res', '1', '-s', 'T1w'])

# One result
lines = result.stdout.strip().splitlines()
assert len(lines) == 1

assert 'tpl-MNI152Lin/tpl-MNI152Lin_res-01_T1w.nii.gz' in lines[0]

path = Path(lines[0])

assert path.exists()


def test_ls_multi(runner):
result = runner.invoke(cli.main, ['ls', 'MNI152Lin', '--res', '1', '-s', 'T1w', '-s', 'T2w'])

# Two results
lines = result.stdout.strip().splitlines()
assert len(lines) == 2

assert 'tpl-MNI152Lin/tpl-MNI152Lin_res-01_T1w.nii.gz' in lines[0]
assert 'tpl-MNI152Lin/tpl-MNI152Lin_res-01_T2w.nii.gz' in lines[1]

paths = [Path(line) for line in lines]

assert all(path.exists() for path in paths)


def test_get_one(runner):
result = runner.invoke(cli.main, ['get', 'MNI152Lin', '--res', '1', '-s', 'T1w'])

# One result, possible download status before
lines = result.stdout.strip().splitlines()[-2:]

assert 'tpl-MNI152Lin/tpl-MNI152Lin_res-01_T1w.nii.gz' in lines[0]

path = Path(lines[0])

stat_res = path.stat()
assert stat_res.st_size == 10669511


def test_get_multi(runner):
result = runner.invoke(cli.main, ['get', 'MNI152Lin', '--res', '1', '-s', 'T1w', '-s', 'T2w'])

# Two result, possible download status before
lines = result.stdout.strip().splitlines()[-3:]

assert 'tpl-MNI152Lin/tpl-MNI152Lin_res-01_T1w.nii.gz' in lines[0]
assert 'tpl-MNI152Lin/tpl-MNI152Lin_res-01_T2w.nii.gz' in lines[1]

paths = [Path(line) for line in lines]

stats = [path.stat() for path in paths]
assert [stat_res.st_size for stat_res in stats] == [10669511, 10096230]
Loading