Skip to content

Commit

Permalink
api: add api.artifacts_show
Browse files Browse the repository at this point in the history
  • Loading branch information
pmrowla committed Aug 11, 2023
1 parent b4459d9 commit 54823b2
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
4 changes: 2 additions & 2 deletions dvc/api/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from dvc.fs.dvc import _DVCFileSystem as DVCFileSystem

from . import artifacts
from .artifacts import artifacts_show
from .data import open # pylint: disable=redefined-builtin
from .data import get_url, read
from .experiments import exp_save, exp_show
Expand All @@ -11,7 +11,7 @@
"all_branches",
"all_commits",
"all_tags",
"artifacts",
"artifacts_show",
"exp_save",
"exp_show",
"get_url",
Expand Down
47 changes: 47 additions & 0 deletions dvc/api/artifacts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
from typing import Any, Dict, Optional

from dvc.repo import Repo


def artifacts_show( # noqa, pylint: disable=redefined-builtin
name: str,
version: Optional[str] = None,
stage: Optional[str] = None,
repo: Optional[str] = None,
) -> Dict[str, str]:
"""
Return path and Git revision for an artifact in a DVC project.
The resulting path and revision can be used in conjunction with other dvc.api
calls to open and read the artifact.
Args:
name (str): name of the artifact to open.
version (str, optional): version of the artifact to open. Defaults to
the latest version.
stage (str, optional): name of the model registry stage.
repo: (str, optional): path or URL for the DVC repo.
Returns:
Dictionary of the form:
{
"rev": ...,
"path": ...,
}
Raises:
dvc.exceptions.ArtifactNotFoundError: The specified artifact was not found in
the repo.
"""
if version and stage:
raise ValueError("Artifact version and stage are mutually exclusive.")

repo_kwargs: Dict[str, Any] = {
"subrepos": True,
"uninitialized": True,
}
with Repo.open(repo, **repo_kwargs) as _repo:
rev = _repo.artifacts.get_rev(name, version=version, stage=stage)
with _repo.switch(rev):
path = _repo.artifacts.get_path(name)
return {"rev": rev, "path": path}

0 comments on commit 54823b2

Please sign in to comment.