Skip to content
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
44 changes: 28 additions & 16 deletions dvc/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,44 @@


class UrlNotDvcRepoError(DvcException):
"""Thrown if given URL is not a DVC repository.

Args:
url (str): URL to the repository
"""
"""Thrown if the given URL is not a DVC repository."""

def __init__(self, url):
super().__init__("'{}' is not a DVC repository.".format(url))


def get_url(path, repo=None, rev=None, remote=None):
"""Returns URL to the storage location of a data artifact tracked
by DVC, specified by its path in a repo.
"""
Returns the URL to the storage location of a data file or directory tracked
in a DVC repo. For Git repos, HEAD is used unless a rev argument is
supplied. The default remote is tried unless a remote argument is supplied.

NOTE: There's no guarantee that the file actually exists in that location.
Raises UrlNotDvcRepoError if repo is not a DVC project.

NOTE: This function does not check for the actual existence of the file or
directory in the remote storage.
"""
with _make_repo(repo, rev=rev) as _repo:
_require_dvc(_repo)
if not isinstance(_repo, Repo):
raise UrlNotDvcRepoError(_repo.url)
out = _repo.find_out_by_relpath(path)
remote_obj = _repo.cloud.get_remote(remote)
return str(remote_obj.checksum_to_path_info(out.checksum))


def open(path, repo=None, rev=None, remote=None, mode="r", encoding=None):
"""Context manager to open a tracked file as a file object."""
"""
Open file in the supplied path tracked in a repo (both DVC projects and
plain Git repos are supported). For Git repos, HEAD is used unless a rev
argument is supplied. The default remote is tried unless a remote argument
is supplied. It may only be used as a context manager:

with dvc.api.open(
'path/to/file',
repo='https://example.com/url/to/repo'
) as fd:
# ... Handle file object fd
"""
args = (path,)
kwargs = {
"repo": repo,
Expand Down Expand Up @@ -63,7 +76,11 @@ def _open(path, repo=None, rev=None, remote=None, mode="r", encoding=None):


def read(path, repo=None, rev=None, remote=None, mode="r", encoding=None):
"""Returns the contents of a tracked file."""
"""
Returns the contents of a tracked file (by DVC or Git). For Git repos, HEAD
is used unless a rev argument is supplied. The default remote is tried
unless a remote argument is supplied.
"""
with open(
path, repo=repo, rev=rev, remote=remote, mode=mode, encoding=encoding
) as fd:
Expand All @@ -81,8 +98,3 @@ def _make_repo(repo_url=None, rev=None):
pass # fallthrough to external_repo
with external_repo(url=repo_url, rev=rev) as repo:
yield repo


def _require_dvc(repo):
if not isinstance(repo, Repo):
raise UrlNotDvcRepoError(repo.url)
Comment thread
jorgeorpinel marked this conversation as resolved.
3 changes: 1 addition & 2 deletions dvc/command/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,7 @@ def add_parser(subparsers, parent_parser):
metrics_show_parser.add_argument(
"targets",
nargs="*",
help="Metric files or directories (see -R) to show "
"(leave empty to display all)",
help="Metric files or directories (see -R) to show",
)
metrics_show_parser.add_argument(
"-t",
Expand Down
7 changes: 3 additions & 4 deletions dvc/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,9 +201,8 @@ class Config(dict):
validate (bool): optional flag to tell dvc if it should validate the
config or just load it as is. 'True' by default.


Raises:
ConfigError: thrown when config has an invalid format.
ConfigError: thrown if config has an invalid format.
"""

APPNAME = "dvc"
Expand Down Expand Up @@ -272,7 +271,7 @@ def load(self, validate=True):
"""Loads config from all the config files.

Raises:
dvc.config.ConfigError: thrown if config has invalid format.
ConfigError: thrown if config has an invalid format.
"""
conf = {}
for level in self.LEVELS:
Expand Down Expand Up @@ -332,7 +331,7 @@ def _map_dirs(conf, func):
@contextmanager
def edit(self, level="repo"):
if level in {"repo", "local"} and self.dvc_dir is None:
raise ConfigError("Not inside a dvc repo")
raise ConfigError("Not inside a DVC repo")

conf = self.load_one(level)
yield conf
Expand Down
4 changes: 2 additions & 2 deletions dvc/scm/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
from dvc.scm.git import Git


# just a sugar to point that this is an actual implementation for a dvc
# project under no SCM control
# Syntactic sugar to signal that this is an actual implementation for a DVC
# project under no SCM control.
class NoSCM(Base):
pass

Expand Down