Skip to content

Commit 4e21e7c

Browse files
committed
Improve the collection parameter handling
It should now properly warn if a collection passed in a comma separated string 404s.
1 parent b505f61 commit 4e21e7c

File tree

4 files changed

+44
-6
lines changed

4 files changed

+44
-6
lines changed

isic_cli/cli/image.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from rich.console import Console
1919
from rich.progress import Progress
2020

21-
from isic_cli.cli.types import CommaSeparatedIdentifiers, SearchString
21+
from isic_cli.cli.types import CommaSeparatedCollectionIds, SearchString
2222
from isic_cli.cli.utils import _extract_metadata, get_attributions, suggest_guest_login
2323
from isic_cli.io.http import (
2424
download_image,
@@ -78,7 +78,7 @@ def image(ctx):
7878
@click.option(
7979
"-c",
8080
"--collections",
81-
type=CommaSeparatedIdentifiers(),
81+
type=CommaSeparatedCollectionIds(),
8282
default="",
8383
help=(
8484
"Filter the images based on a comma separated string of collection"

isic_cli/cli/metadata.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@
1818
from rich.progress import Progress, track
1919
from rich.table import Table
2020

21-
from isic_cli.cli.types import CommaSeparatedIdentifiers, SearchString, WritableFilePath
21+
from isic_cli.cli.types import (
22+
CommaSeparatedCollectionIds,
23+
SearchString,
24+
WritableFilePath,
25+
)
2226
from isic_cli.cli.utils import _extract_metadata, suggest_guest_login
2327
from isic_cli.io.http import get_images, get_num_images
2428

@@ -146,7 +150,7 @@ def validate(csv_file: io.TextIOWrapper): # noqa: C901, PLR0915, PLR0912
146150
@click.option(
147151
"-c",
148152
"--collections",
149-
type=CommaSeparatedIdentifiers(),
153+
type=CommaSeparatedCollectionIds(),
150154
default="",
151155
help=(
152156
"Filter the images based on a comma separated list of collection ids e.g. 2,17,42. "

isic_cli/cli/types.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,34 @@ def convert(self, value, param, ctx):
3636
return value
3737

3838

39-
class CommaSeparatedIdentifiers(click.ParamType):
40-
name = "comma_separated_identifiers"
39+
class CommaSeparatedCollectionIds(click.ParamType):
40+
name = "comma_separated_collection_ids"
4141

4242
def convert(self, value, param, ctx):
4343
value = super().convert(value, param, ctx)
4444

4545
if value != "" and not re.match(r"^(\d+)(,\d+)*$", value):
4646
self.fail(f'Improperly formatted value "{value}".', param, ctx)
47+
48+
collection_ids = value.split(",") if value else []
49+
50+
for collection_id in collection_ids:
51+
try:
52+
get_collection(ctx.obj.session, collection_id)
53+
except HTTPError as e: # noqa: PERF203
54+
if e.response.status_code == 404:
55+
append = ""
56+
if not ctx.obj.user:
57+
append = "Logging in may help (see `isic user login`)."
58+
59+
self.fail(
60+
f"Collection {collection_id} does not exist or you don't have access to it. {append}", # noqa: E501
61+
param,
62+
ctx,
63+
)
64+
else:
65+
raise
66+
4767
return value
4868

4969

tests/test_cli_image.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from pathlib import Path
55

66
import pytest
7+
from requests import HTTPError
78

89
from isic_cli.cli.image import cleanup_partially_downloaded_files
910

@@ -51,6 +52,19 @@ def test_image_download(cli_run, outdir):
5152
assert Path(f"{outdir}/licenses/CC-0.txt").exists()
5253

5354

55+
@pytest.mark.usefixtures("_isolated_filesystem", "_mock_images")
56+
def test_image_download_no_collection(mocker, cli_run, outdir):
57+
mocker.patch(
58+
"isic_cli.cli.types.get_collection",
59+
side_effect=HTTPError(response=mocker.MagicMock(status_code=404)),
60+
)
61+
62+
result = cli_run(["image", "download", outdir, "--collections", "462"])
63+
64+
assert result.exit_code == 2, result.exception
65+
assert "does not exist or you don't have access to it." in result.output
66+
67+
5468
@pytest.mark.usefixtures("_isolated_filesystem", "_mock_images")
5569
def test_image_download_metadata_newlines(cli_run, outdir):
5670
result = cli_run(["image", "download", outdir])

0 commit comments

Comments
 (0)