Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restructure parse_dandi_url() return type #605

Merged
merged 6 commits into from
Apr 30, 2021
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: 2 additions & 2 deletions dandi/cli/cmd_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ def download(
if dandi_instance is not None:
if url:
yarikoptic marked this conversation as resolved.
Show resolved Hide resolved
for u in url:
_, server_url, _, _ = parse_dandi_url(u)
if known_instances_rev.get(server_url.rstrip("/")) != dandi_instance:
parsed_url = parse_dandi_url(u)
if known_instances_rev.get(parsed_url.api_url) != dandi_instance:
raise click.UsageError(
f"{u} does not point to {dandi_instance!r} instance"
)
Expand Down
28 changes: 8 additions & 20 deletions dandi/cli/cmd_ls.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import click

from .base import lgr, map_to_click_exceptions
from ..dandiarchive import _dandi_url_parser
from ..dandiarchive import DandisetURL, _dandi_url_parser, parse_dandi_url
from ..utils import is_url

# TODO: all the recursion options etc
Expand Down Expand Up @@ -110,11 +110,11 @@ def ls(paths, schema, metadata, fields=None, format="auto", recursive=False, job
def assets_gen():
for path in paths:
if is_url(path):
from ..dandiarchive import navigate_url
Copy link
Member

Choose a reason for hiding this comment

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

could you please add a test which would cover this code path? I have added raising an exception here and none of the tests flipped anyhow (directly via python or may be through external invocation of dandi ls)

Copy link
Member Author

Choose a reason for hiding this comment

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

Should the test just be a smoke test that calls ls with a URL (pointing to dandiarchive.org or the Docker instance?), or should it also expect something from the output?

Copy link
Member

Choose a reason for hiding this comment

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

I guess with -f json_pp you could expect nicely formatted record you could loads and if ran on a dandiset with assets -- could check that there is at least one asset (without getting into details)

Copy link
Member

Choose a reason for hiding this comment

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

by -f json_pp I didn't mean "run in command line"... format="json_pp" ;)

Copy link
Member Author

Choose a reason for hiding this comment

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

Tests added.


with navigate_url(path) as (client, dandiset, assets):
if dandiset:
dandiset_id = dandiset.get("dandiset", {}).get("identifier")
parsed_url = parse_dandi_url(path)
with parsed_url.navigate(
include_metadata=metadata in ("all", "assets")
) as (client, dandiset, assets):
if isinstance(parsed_url, DandisetURL):
rec = {
"path": dandiset.pop("dandiset", {}).get(
"identifier", "ERR#%s" % id(dandiset)
Expand All @@ -124,20 +124,8 @@ def assets_gen():
# rec.update(dandiset.get('metadata', {}))
rec.update(dandiset)
yield rec
else:
dandiset_id = None
if recursive and assets:
if metadata in ("all", "assets"):
for a in assets:
if "metadata" not in a:
a["metadata"] = client.get_asset(
dandiset_id,
dandiset["version"],
a["asset_id"],
)
yield a
else:
yield from assets
if not isinstance(parsed_url, DandisetURL) or recursive:
yield from assets
else:
# For now we support only individual files
yield path
Expand Down
37 changes: 37 additions & 0 deletions dandi/cli/tests/test_ls.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,40 @@ def load(s):
assert metadata.pop("nwb_version").startswith("2.")
for f in ["session_id", "experiment_description"]:
assert metadata[f] == simple1_nwb_metadata[f]


def test_ls_dandiset_url():
r = CliRunner().invoke(
ls, ["-f", "yaml", "https://api.dandiarchive.org/api/dandisets/000027"]
)
assert r.exit_code == 0, r.output
data = yaml_load(r.stdout, "safe")
assert len(data) == 1
assert data[0]["path"] == "000027"


def test_ls_dandiset_url_recursive():
r = CliRunner().invoke(
ls, ["-f", "yaml", "-r", "https://api.dandiarchive.org/api/dandisets/000027"]
)
assert r.exit_code == 0, r.output
data = yaml_load(r.stdout, "safe")
assert len(data) == 2
assert data[0]["path"] == "000027"
assert data[1]["path"] == "sub-RAT123/sub-RAT123.nwb"


def test_ls_path_url():
r = CliRunner().invoke(
ls,
[
"-f",
"yaml",
"https://api.dandiarchive.org/api/dandisets/000027/versions/draft"
"/assets/?path=sub-RAT123/",
],
)
assert r.exit_code == 0, r.output
data = yaml_load(r.stdout, "safe")
assert len(data) == 1
assert data[0]["path"] == "sub-RAT123/sub-RAT123.nwb"
Loading