dandi.dandiarchive
¶
This module provides functionality for parsing URLs and other resource identifiers for Dandisets & assets on Dandi Archive servers and for fetching the objects to which the URLs refer. See Resource Identifiers for a list of accepted URL formats.
Basic operation begins by calling parse_dandi_url()
on a URL in order to
acquire a ParsedDandiURL
instance, which can then be used to obtain the
Dandiset and/or assets specified in the URL. Call an instance’s
get_dandiset()
and/or get_assets()
to get
the assets, passing in a DandiAPIClient
for the appropriate
Dandi Archive API instance; an unauthenticated client pointing to the correct
instance can be acquired via the get_client()
method. As a
convenience, one can acquire a client, the Dandiset, and an iterator of all
assets by using the navigate()
context manager like so:
parsed_url = parse_dandi_url("https://...")
with parsed_url.navigate() as (client, dandiset, assets):
...
# The client's session is closed when the context manager exits.
As a further convenience, a URL can be parsed and navigated in one fell swoop
using the navigate_url()
function.
- class dandi.dandiarchive.ParsedDandiURL(instance: DandiInstance, dandiset_id: str | None, version_id: str | None)[source]¶
Bases:
ABC
Parsed representation of a URL pointing to a Dandi Archive resource (Dandiset or asset(s)). Subclasses must implement
get_assets()
.Most methods take a
client: DandiAPIClient
argument, which must be aDandiAPIClient
object for queryinginstance
(This is not checked). Such a client instance can be obtained by callingget_client()
, or an appropriate pre-existing client instance can be passed instead.- instance: DandiInstance¶
The Dandi Archive instance that the URL points to
- dandiset_id: str | None¶
The ID of the Dandiset given in the URL
- version_id: str | None¶
The version of the Dandiset, if specified. If this is not set, the version will be defaulted using the rules described under
DandiAPIClient.get_dandiset()
.
- property api_url: AnyHttpUrl¶
The base URL of the Dandi API service, without a trailing slash
- get_client() DandiAPIClient [source]¶
Returns an unauthenticated
DandiAPIClient
forinstance
- get_dandiset(client: DandiAPIClient, lazy: bool = True) RemoteDandiset | None [source]¶
Returns information about the specified (or default) version of the specified Dandiset. Returns
None
if the URL did not contain a Dandiset identifier.If
lazy
is true, a “lazy”RemoteDandiset
instance is returned, with no requests made until any data is actually required.
- abstract get_assets(client: DandiAPIClient, order: str | None = None, strict: bool = False) Iterator[BaseRemoteAsset] [source]¶
Returns an iterator of asset structures for the assets referred to by or associated with the URL. For a URL pointing to just a Dandiset, this is the set of all assets in the given or default version of the Dandiset. For a URL that specifies a specific asset or collection of assets in a Dandiset, this is all of those assets.
When multiple assets are returned, they can be sorted by a given field by passing the name of that field as the
order
parameter. The accepted field names are"created"
,"modified"
, and"path"
. Prepend a hyphen to the field name to reverse the sort order.If
strict
is true, then fetching assets for a URL that refers to a nonexistent resource will raise aNotFoundError
; if it is false, the method will instead return an empty iterator.
- get_asset_ids(client: DandiAPIClient) Iterator[str] [source]¶
Returns an iterator of IDs of the assets referred to by or associated with the URL
A context manager that returns a triple of a
DandiAPIClient
(with an open session that is closed when the context manager closes), the return value ofget_dandiset()
, and the return value ofget_assets()
.If
strict
is true, thenget_dandiset()
is called withlazy=False
andget_assets()
is called withstrict=True
; ifstrict
is false, the opposite occurs.If
authenticate
is true, thendandi_authenticate()
will be called on the client before returning it. If it isNone
(the default), the method will only be called if the URL requires authentication (e.g., if the resource(s) are embargoed).Changed in version 0.35.0:
authenticate
added
- class dandi.dandiarchive.DandisetURL(instance: DandiInstance, dandiset_id: str | None, version_id: str | None)[source]¶
Bases:
ParsedDandiURL
Parsed from a URL that only refers to a Dandiset (possibly with a version)
- get_assets(client: DandiAPIClient, order: str | None = None, strict: bool = False) Iterator[BaseRemoteAsset] [source]¶
Returns all assets in the Dandiset
- class dandi.dandiarchive.SingleAssetURL(instance: DandiInstance, dandiset_id: str | None, version_id: str | None)[source]¶
Bases:
ParsedDandiURL
Superclass for parsed URLs that refer to a single asset
- class dandi.dandiarchive.MultiAssetURL(instance: DandiInstance, dandiset_id: str | None, version_id: str | None, path: str)[source]¶
Bases:
ParsedDandiURL
Superclass for parsed URLs that refer to multiple assets
- path: str¶
- class dandi.dandiarchive.BaseAssetIDURL(instance: DandiInstance, asset_id: str)[source]¶
Bases:
SingleAssetURL
Parsed from a URL that refers to an asset by ID and does not include the Dandiset ID
- dandiset_id: None = None¶
The ID of the Dandiset given in the URL
- version_id: None = None¶
The version of the Dandiset, if specified. If this is not set, the version will be defaulted using the rules described under
DandiAPIClient.get_dandiset()
.
- asset_id: str¶
- get_assets(client: DandiAPIClient, order: str | None = None, strict: bool = False) Iterator[BaseRemoteAsset] [source]¶
Yields the asset with the given ID. If the asset does not exist, then a
NotFoundError
is raised ifstrict
is true, and nothing is yielded ifstrict
is false.
- get_asset_ids(client: DandiAPIClient) Iterator[str] [source]¶
Yields the ID of the asset (regardless of whether it exists)
A context manager that returns a triple of a
DandiAPIClient
(with an open session that is closed when the context manager closes), the return value ofget_dandiset()
, and the return value ofget_assets()
.If
strict
is true, thenget_dandiset()
is called withlazy=False
andget_assets()
is called withstrict=True
; ifstrict
is false, the opposite occurs.If
authenticate
is true, thendandi_authenticate()
will be called on the client before returning it. If it isNone
(the default), the method will only be called if the URL requires authentication (e.g., if the resource(s) are embargoed).Changed in version 0.35.0:
authenticate
added
- class dandi.dandiarchive.AssetIDURL(instance: DandiInstance, dandiset_id: str | None, version_id: str | None, asset_id: str)[source]¶
Bases:
SingleAssetURL
Parsed from a URL that refers to an asset by ID and includes the Dandiset ID
- asset_id: str¶
- get_assets(client: DandiAPIClient, order: str | None = None, strict: bool = False) Iterator[BaseRemoteAsset] [source]¶
Yields the asset with the given ID. If the Dandiset or asset does not exist, then a
NotFoundError
is raised ifstrict
is true, and nothing is yielded ifstrict
is false.
- get_asset_ids(client: DandiAPIClient) Iterator[str] [source]¶
Yields the ID of the asset (regardless of whether it exists)
- class dandi.dandiarchive.AssetPathPrefixURL(instance: DandiInstance, dandiset_id: str | None, version_id: str | None, path: str)[source]¶
Bases:
MultiAssetURL
Parsed from a URL that refers to a collection of assets by path prefix
- get_assets(client: DandiAPIClient, order: str | None = None, strict: bool = False) Iterator[BaseRemoteAsset] [source]¶
Returns the assets whose paths start with
path
. Ifstrict
is true and there are no such assets, raisesNotFoundError
.Changed in version 0.54.0:
NotFoundError
will now be raised ifstrict
is true and there are no such assets.
- class dandi.dandiarchive.AssetItemURL(instance: DandiInstance, dandiset_id: str | None, version_id: str | None, path: str)[source]¶
Bases:
SingleAssetURL
Parsed from a URL that refers to a specific asset by path
- path: str¶
- get_assets(client: DandiAPIClient, order: str | None = None, strict: bool = False) Iterator[BaseRemoteAsset] [source]¶
Yields the asset whose path equals
path
. If there is no such asset:If
strict
is true, aNotFoundError
is raised.If
strict
is false and the path happens to be the path to an asset directory, aValueError
is raised indicating that the user left off a trailing slash.Otherwise, nothing is yielded.
- class dandi.dandiarchive.AssetFolderURL(instance: DandiInstance, dandiset_id: str | None, version_id: str | None, path: str)[source]¶
Bases:
MultiAssetURL
Parsed from a URL that refers to a collection of assets by folder path
- get_assets(client: DandiAPIClient, order: str | None = None, strict: bool = False) Iterator[BaseRemoteAsset] [source]¶
Returns all assets under the folder at
path
. If the folder does not exist andstrict
is true, raisesNotFoundError
; otherwise, if the folder does not exist andstrict
is false, yields nothing.Changed in version 0.54.0:
NotFoundError
will now be raised ifstrict
is true and there is no such folder.
- class dandi.dandiarchive.AssetGlobURL(instance: DandiInstance, dandiset_id: str | None, version_id: str | None, path: str)[source]¶
Bases:
MultiAssetURL
Added in version 0.54.0.
Parsed from a URL that refers to a collection of assets by a path glob
- get_assets(client: DandiAPIClient, order: str | None = None, strict: bool = False) Iterator[BaseRemoteAsset] [source]¶
Returns all assets whose paths match the glob pattern
path
. Ifstrict
is true and there are no such assets, raisesNotFoundError
.Changed in version 0.54.0:
NotFoundError
will now be raised ifstrict
is true and there are no such assets.
A context manager that takes a URL pointing to a DANDI Archive and returns a triple of a
DandiAPIClient
(with an open session that is closed when the context manager closes), the Dandiset identified in the URL (if any), and the assets specified by the URL (or, if no specific assets were specified, all assets in the Dandiset).Changed in version 0.35.0:
authenticate
added- Parameters:
url (str) – URL which might point to a Dandiset, folder, or asset(s)
strict (bool) – If true, then
get_dandiset()
is called withlazy=False
andget_assets()
is called withstrict=True
; if false, the opposite occurs.authenticate – If true, then
dandi_authenticate()
will be called on the client before returning it. IfNone
(the default), the method will only be called if the URL requires authentication (e.g., if the resource(s) are embargoed).
- Returns:
Context manager that yields a
(client, dandiset, assets)
tuple;client
will have a session established for the duration of the context
- dandi.dandiarchive.parse_dandi_url(url: str, *, map_instance: bool = True, glob: bool = False) ParsedDandiURL ¶
Parse a Dandi Archive URL and return a
ParsedDandiURL
instance. See Resource Identifiers for the supported URL formats.Added in version 0.54.0:
glob
parameter added- Parameters:
glob (bool) – if true, certain URL formats will be parsed into
AssetGlobURL
instances- Raises:
UnknownURLError – if the URL is not one of the above
- dandi.dandiarchive.follow_redirect(url: str) str ¶
Resolve the given URL by following all redirects.
- Raises:
NotFoundError – if a 404 response is returned
FailedToConnectError – if a response other than 200, 400, 404, or one of the statuses in
RETRY_STATUSES
is returned