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

Give ls a --metadata option #536

Merged
merged 1 commit into from
Apr 7, 2021
Merged
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
26 changes: 24 additions & 2 deletions dandi/cli/cmd_ls.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,24 @@
default=6, # TODO: come up with smart auto-scaling etc
show_default=True,
)
@click.option(
"--metadata",
type=click.Choice(["api", "all", "assets"]),
default="api",
)
@click.option(
"--schema",
help="Convert metadata to new schema version",
metavar="VERSION",
)
@click.argument("paths", nargs=-1, type=click.Path(exists=False, dir_okay=True))
@map_to_click_exceptions
def ls(paths, schema, fields=None, format="auto", recursive=False, jobs=6):
def ls(paths, schema, metadata, fields=None, format="auto", recursive=False, jobs=6):
"""List .nwb files and dandisets metadata."""
# TODO: more logical ordering in case of fields = None
from .formatter import JSONFormatter, PYOUTFormatter, YAMLFormatter
from ..consts import metadata_all_fields
from ..dandiapi import DandiAPIClient

# TODO: avoid
from ..support.pyout import PYOUT_SHORT_NAMES_rev
Expand Down Expand Up @@ -98,6 +104,7 @@ def assets_gen():

with navigate_url(path) as (client, dandiset, assets):
if dandiset:
dandiset_id = dandiset.get("dandiset", {}).get("identifier")
rec = {
"path": dandiset.pop("dandiset", {}).get(
"identifier", "ERR#%s" % id(dandiset)
Expand All @@ -107,8 +114,23 @@ def assets_gen():
# rec.update(dandiset.get('metadata', {}))
rec.update(dandiset)
yield rec
else:
dandiset_id = None
if recursive and assets:
yield from assets
if isinstance(client, DandiAPIClient) and 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
else:
# For now we support only individual files
yield path
Expand Down