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

Add get_asset_metadata() function #693

Merged
merged 1 commit into from
Jul 6, 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
22 changes: 22 additions & 0 deletions dandi/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,28 @@ def process_ndtypes(asset, nd_types):
return asset


def get_asset_metadata(
filepath, relpath, digest=None, digest_type=None, allow_any_path=True
) -> models.BareAsset:
try:
metadata = nwb2asset(filepath, digest=digest, digest_type=digest_type)
except Exception as e:
lgr.info(
"Failed to extract NWB metadata from %s: %s: %s",
filepath,
type(e).__name__,
str(e),
)
if allow_any_path:
metadata = get_default_metadata(
filepath, digest=digest, digest_type=digest_type
)
else:
raise
metadata.path = str(relpath)
return metadata


def nwb2asset(
nwb_path, digest=None, digest_type=None, schema_version=None
) -> models.BareAsset:
Expand Down
26 changes: 10 additions & 16 deletions dandi/upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def upload(
f"convention {dandiset_identifier_regex!r}."
)

from .metadata import get_default_metadata, nwb2asset
from .metadata import get_asset_metadata
from .pynwb_utils import ignore_benign_pynwb_warnings
from .support.pyout import naturalsize
from .utils import find_dandi_files, find_files, path_is_subpath
Expand Down Expand Up @@ -242,21 +242,15 @@ def process_path(path, relpath):
# ad-hoc for dandiset.yaml for now
yield {"status": "extracting metadata"}
try:
asset_metadata = nwb2asset(
path, digest=file_etag, digest_type="dandi_etag"
)
except Exception as exc:
lgr.exception("Failed to extract metadata from %s", path)
if allow_any_path:
yield {"status": "failed to extract metadata"}
asset_metadata = get_default_metadata(
path, digest=file_etag, digest_type="dandi_etag"
)
else:
yield skip_file("failed to extract metadata: %s" % str(exc))
return
metadata = asset_metadata.json_dict()
metadata["path"] = str(relpath)
metadata = get_asset_metadata(
path,
relpath,
digest=file_etag,
digest_type="dandi_etag",
allow_any_path=allow_any_path,
).json_dict()
except Exception as e:
yield skip_file("failed to extract metadata: %s" % str(e))

#
# Upload file
Expand Down